LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-20-2006, 07:41 AM   #1
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Rep: Reputation: 30
basic linking and makefile question


3 file, main calls a function which calls another function.

How do I do the makefile?

main.c:
Code:
#include "dd.h"

int main()
{
  function1();
}
a.c:
Code:
#include "dd.h"

void function1()
{
  function2();
}
b.c:
Code:
#include "dd.h"

void function2()
{
  printf("function2");
}
dd.h:
Code:
void function1(void);
void function2(void);
 
Old 07-20-2006, 07:46 AM   #2
blackzone
Member
 
Registered: Jun 2004
Posts: 256

Original Poster
Rep: Reputation: 30
I dont' know how to do linking. This is my makefile which dont' work.

Code:
main: a.o dd.h
        gcc a.o -o main
a.o: b.o dd.h
        gcc -L b.o -c a.c
b.o: b.c dd.h
        gcc -c b.c
The error is:
Code:
gcc a.o -o main
/usr/lib/gcc/i386-redhat-linux/4.1.0/../../../crt1.o: In function `_start': undefined reference to `main'
a.o: In function `function1':a.c:(.text+0x7): undefined reference to `function2'
collect2: ld returned 1 exit status
make: *** [main] Error 1
 
Old 07-20-2006, 07:47 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
try this:
Code:
TARGET=dummy_name
# ---------------------------------
# Standard makefile template
# just specify the target name above
# and the make file should sort it
# all out (hopefully)
# ---------------------------------

INCLUDEFILE = include.mk


# this should appear above the 'include' directive below
# calls for the target and then the cleanup
# =========================================
all: ${TARGET} cleanup 


-include ${INCLUDEFILE}

# ${INCLUDEFILE} should not exist when it's included.
# As it does not exist it is treated as a target.
# Therefore it is created anew each time,
# by the appropriate rule, below
# Which finds .c and .h files and calls makedepend for them
# =========================================================

${INCLUDEFILE}:
 @echo "### creating ${INCLUDEFILE}"
 @echo "### creating ${INCLUDEFILE}" > ${INCLUDEFILE}
 -@ echo  *.c|xargs echo SRCFILES = >> ${INCLUDEFILE}
 -@ echo  *.h|xargs echo HFILES = >> ${INCLUDEFILE}
 @  makedepend  -f${INCLUDEFILE} *.c


# (SRCFILES defined in INCLUDEFILE)
# =================================
OBJFILES = ${SRCFILES:.c=.o}


# this is the working target
# ==========================

${TARGET}: ${OBJFILES} 
 ${CC} -o $@ ${OBJFILES}

help:print_help cleanup

print_help:
 echo  OBJFILES = ${OBJFILES}
 echo  SRCFILES = ${SRCFILES}
 cat ${INCLUDEFILE}
 

clean:cleanup
 -@ rm -f  *.o 

# we need to delete the INCLUDEFILE otherwise
# new source and header files won't be picked up
# next time
# =========
cleanup:
 @echo "### deleting ${INCLUDEFILE}"
 -@ rm -f  ${INCLUDEFILE}
 
Old 07-20-2006, 12:11 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Howabout this (somewhat less complicated than bigears'):
Code:
main: main.o a.o b.o
        gcc main.o a.o b.o -o main
main.o: main.c dd.h
        gcc -c main.c -o main
a.o: a.c dd.h
        gcc -c a.c
b.o: b.c dd.h
        gcc -c b.c
Or even
Code:
main: a.c b.c main.c dd.h
        gcc main.c a.c b.c -o main
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Makefile.in question lucky6969b Programming 4 12-08-2005 03:32 AM
makefile question ashwin.tanugula Programming 2 10-26-2005 03:08 PM
Help! LFS 6.1 Chapter 6.12- Basic Compiling and Linking Tools don't work! TGWDNGHN Linux From Scratch 2 07-26-2005 06:25 PM
Question about makefile taureanyang Programming 1 03-25-2005 11:57 PM
Basic Concept about linking Kreature Linux - Newbie 1 11-16-2004 06:11 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration