LinuxQuestions.org
Review your favorite Linux distribution.
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 11-18-2009, 04:03 PM   #1
paulclark
LQ Newbie
 
Registered: Dec 2006
Posts: 18

Rep: Reputation: 0
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
 
Old 11-18-2009, 04:47 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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
 
Old 11-18-2009, 04:49 PM   #3
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Quote:
Originally Posted by paulclark View Post
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.
 
Old 11-18-2009, 05:01 PM   #4
paulclark
LQ Newbie
 
Registered: Dec 2006
Posts: 18

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
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.
 
  


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
I dont understand blacktattoo Linux - Newbie 1 12-26-2004 08:41 PM
i dont really understand this. Brain Drop General 3 08-18-2003 08:54 PM
i dont understand!!!! Fathonkey Linux - Software 17 05-29-2003 11:37 AM
Dont Understand Sifvion Slackware 27 03-26-2003 01:49 PM
Things dont work when you dont understand withoutaclue Linux - Newbie 3 03-12-2003 09:51 AM

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

All times are GMT -5. The time now is 07:24 AM.

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