LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Dont understand Makefile (https://www.linuxquestions.org/questions/programming-9/dont-understand-makefile-770040/)

paulclark 11-18-2009 04:03 PM

Dont understand Makefile
 
I have been working on a c Program that is now quite large and I want to separate some of the functions out to a separate source file.

I have a working Makefile for the compilation:

Code:

all: park
park: park.c park.h
        gcc -Wall -pedantic -ansi -g -I/usr/include/mysql park.c \
        -L/usr/lib/mysql -lmysqlclient -lcurses -o park

I then set up a new blank database.c file to start moving some functions to. I then changed the Makefile this to:

Code:

all: park
park: park.o database.o
        gcc -o park park.o database.o
park.o: park.c park.h
        gcc -c -Wall -pedantic -ansi -g -I/usr/include/mysql park.c \
        -L/usr/lib/mysql -lmysqlclient -lcurses
database.o: database.c
        gcc -c database.c

But I have obviously misunderstood the examples I have been looking at because this produces errors

Code:

gcc -o park park.o database.o
park.o: In function `database_start':
/usr/local/src/park/park.c:5: undefined reference to `mysql_init'
/usr/local/src/park/park.c:6: undefined reference to `mysql_real_connect'
/usr/local/src/park/park.c:8: undefined reference to `mysql_error'
/usr/local/src/park/park.c:8: undefined reference to `mysql_errno'
park.o: In function `readfirst':
/usr/local/src/park/park.c:19: undefined reference to `mysql_query'

[Over a thousand lines deleted]

/usr/local/src/park/park.c:2162: undefined reference to `stdscr'
/usr/local/src/park/park.c:2162: undefined reference to `wrefresh'
park.o: In function `main':
/usr/local/src/park/park.c:2170: undefined reference to `initscr'
/usr/local/src/park/park.c:2175: undefined reference to `endwin'
collect2: ld returned 1 exit status
make: *** [park] Error 1

I cannot figure out why joining the two object files seems to lose the definition from park.h and what I am doing wrong here.

Paul

ntubski 11-18-2009 04:47 PM

You have to link in the libraries when creating the executable (park). Also, you should probably use the same compilation flags for all object files:
Code:

CFLAGS = -Wall -ansi -pedantic -g
all: park
park: park.o database.o
        gcc -o park park.o database.o -L/usr/lib/mysql -lmysqlclient -lcurses
park.o: park.c park.h
        gcc -c $(CFLAGS) -I/usr/include/mysql park.c       
database.o: database.c
        gcc -c $(CFLAGS) database.c

Make comes with builtin rules that handle compilation of C programs so you could even do
Code:

CFLAGS := -Wall -ansi -pedantic -g
LDFLAGS := -L/usr/lib/mysql -lmysqlclient -lcurses
CC := gcc

all: park
park: park.o database.o
park.o: park.c park.h
park.o: CFLAGS += -I/usr/include/mysql
database.o: database.c


rweaver 11-18-2009 04:49 PM

Quote:

Originally Posted by paulclark (Post 3761862)
I cannot figure out why joining the two object files seems to lose the definition from park.h and what I am doing wrong here.

Looks like you may be missing some includes in your database file.

paulclark 11-18-2009 05:01 PM

Quote:

Originally Posted by ntubski (Post 3761911)
You have to link in the libraries when creating the executable (park).

Thanks for that! My problem was due to referencing a simple example of a Makefile joining Object files that had no libraries.


All times are GMT -5. The time now is 03:14 AM.