LinuxQuestions.org
Visit Jeremy's Blog.
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 04-09-2013, 01:15 PM   #1
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Rep: Reputation: Disabled
Undefined reference to 'function name' C


I am using a makefile to compile a program that consists of several files. The error that I'm getting is:

/tmp/ccwntKP7.o: In function 'main':
test.driver.c.text+0x17f): undefined reference to 'PrintWhere'
collect2: ld returned 1 exit status
make: *** [test-driver] Error 1

I'm compiling with makefile, so the commandline reads make test-driver. The file is set up to read the command line:
cc -g -o test-driver test-driver.c c-warmup.o

The test-driver file calls the function PrintWhere this way:
#include "c-warmup.h"
#define DB "c-warmup.db"
int main (int argc, char *argv[])
{
float minGpa = 3.5;
float failGpa = 2.0;
float maxGpa = 4.0;
float eqGpa = 3.2;
int testId = 247;
char testName[80];

strcpy (testName, "Frye, John");
printf ("***************************************************************\n");
printf ("Names, ages of students in file %s \n with name > '%s'\n", DB, testName);
printf ("***************************************************************\n");
PrintWhere (DB, STRUCTSIZE, NAME_OFFSET, STRING, AGE_OFFSET, INTEGER, NAME_OFFSET,
STRING, GREATER, testName);
printf ("\n");
printf ("***************************************************************\n");
printf ("Names, id's of students in file %s \n with id > %d\n", DB, testId);
printf ("***************************************************************\n");
PrintWhere (DB, STRUCTSIZE, NAME_OFFSET, STRING,ID_OFFSET, INTEGER, ID_OFFSET,
INTEGER, GREATER, (char *) &testId);
printf ("\n");
printf ("***************************************************************\n");
printf ("Names, gpa's of students in file %s \n with gpa < %3.1f\n", DB, failGpa);
printf ("***************************************************************\n");
PrintWhere (DB, STRUCTSIZE, NAME_OFFSET, STRING, GPA_OFFSET, FLOAT, GPA_OFFSET,
FLOAT, LESS, (char *) &failGpa);
printf ("\n");
printf ("***************************************************************\n");
printf ("Names, ages of students in file %s \n with gpa = %3.1f\n", DB, maxGpa);
printf ("***************************************************************\n");
PrintWhere (DB, STRUCTSIZE, NAME_OFFSET, STRING, AGE_OFFSET, INTEGER, GPA_OFFSET,
FLOAT, EQUAL, (char *) &maxGpa);

return (0);
}


PrintWhere is created in a seperate file called cwarmup.c, which is called in cwarmup.h

int
PrintWhere (
char *fileName, /* name of the file of records */
int recSize, /* size of each record in bytes */
int fieldOffset1, /* offset of field 1 in record */
char fieldType1, /* field 1 type: 'i', 'f', or 's' */
int fieldOffset2, /* offset of field 2 in record */
char fieldType2, /* field 2 type: 'i', 'f', or 's' */
int fieldOffset3, /* offset of field 3 in record */
char fieldType3, /* field 3 type: 'i', 'f', or 's' */
char compareOp, /* comparison type: '<', '=', or '>' */
char *valPtr) /* pointer to comparison value */

Last edited by vwinnecke; 04-09-2013 at 01:18 PM.
 
Old 04-09-2013, 01:28 PM   #2
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
Is it cwarmup or c-warmup?
 
Old 04-09-2013, 01:33 PM   #3
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
PrintWhere is created in a seperate file called cwarmup.c, which is called in cwarmup.h


Should be PrintWhere is created in a seperate file called c-warmup.c, which is called in c-warmup.h
 
Old 04-09-2013, 01:38 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
1) Please use CODE tags around code and computer output etc.

2) I don't see a copy of your makefile in your post. You posted code, but described a problem likely caused by an error in the makefile, not an error in the code.

Quote:
Originally Posted by vwinnecke View Post
PrintWhere is created in a seperate file called cwarmup.c, which is called in cwarmup.h
What do you mean by "called in" or was that just a typo?

The other .c file needs to include the c-warmup.h file, which is probably OK (but see the comment in post #2).

But that is not enough to get c-warmup.c included at link time. You need something in the makefile to cause c-warmup.o to be created from c-warmup.c and then used.

Last edited by johnsfine; 04-09-2013 at 01:40 PM.
 
Old 04-09-2013, 01:38 PM   #5
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
Quote:
Originally Posted by vwinnecke View Post
PrintWhere is created in a seperate file called cwarmup.c, which is called in cwarmup.h


Should be PrintWhere is created in a seperate file called c-warmup.c, which is called in c-warmup.h
I think so.
 
Old 04-09-2013, 01:48 PM   #6
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
CC= cc
CFLAGS= -g

#Targets

all: driver test-driver

info:
@echo "Type 'make <target>', where <target> is one of: "
@echo " 'makedata' to build data file"
@echo " 'driver' to create the driver"
@echo " 'clean' to delete output & object files & excutable"
@echo "You can also type any other target name you see in the Makefile"
@echo "Anything else you create on your own"

makedata: makedata.c c-warmup.h
$(CC) $(CFLAGS) -o makedata makedata.c

c-warmup.o: c-warmup.c c-warmup.h
$(CC) $(CFLAGS) -c c-warmup.c

test-driver: test-driver.c c-warmup.o makedata
$(CC) $(CFLAGS) cc -g -o test-driver test-driver.c c-warmup.o

driver: driver.c c-warmup.o makedata
$(CC) $(CFLAGS) -o driver driver.c c-warmup.o

clean:
rm -f driver driver.o test-driver test-driver.o \
c-warmup.o c-warmup.db makedata core
 
Old 04-09-2013, 01:49 PM   #7
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
What does the error 'Undefined reference' mean?

I guess I don't need the cc and -g on the code line since it is defined.

Last edited by vwinnecke; 04-09-2013 at 01:50 PM.
 
Old 04-09-2013, 02:07 PM   #8
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by vwinnecke View Post
What does the error 'Undefined reference' mean?
It means the indicated .o file (test_driver) used that symbol, and no .o file defined that symbol.

Having read more carefully, I see you did link with c-warmup.o and that should have defined the symbol.

So the cause of the problem is not obvious.

Quote:
Originally Posted by vwinnecke View Post
I guess I don't need the cc and -g on the code line since it is defined.
Now that I see your makefile, I don't understand why the extra cc on that command line doesn't cause a problem (but I'm not at all an expert in makefiles).

The extra -g might be redundant, but that would not be a problem.

I forget the command for dumping the symbols in a .o file, but that would seem to be the next step in diagnosing the problem:

If the symbol is in c-warmup.o, something is wrong with the command used to link.

If the symbol is not in c-warmup.o, something is wrong with either c-warmup.c or the command used to compile it.

Last edited by johnsfine; 04-09-2013 at 02:12 PM.
 
Old 04-09-2013, 02:13 PM   #9
vwinnecke
LQ Newbie
 
Registered: Mar 2013
Posts: 14

Original Poster
Rep: Reputation: Disabled
what symbol are you referring to?

Also, I'm wondering why it shows in the error
driver.c: (.text+0x17f):

the part in () instead of showing a line number. Could that indicate a problem?

Last edited by vwinnecke; 04-09-2013 at 02:18 PM.
 
Old 04-09-2013, 02:17 PM   #10
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
PrintWhere
 
Old 04-09-2013, 02:18 PM   #11
manu-tm
Member
 
Registered: May 2008
Location: France
Distribution: Ubuntu, Debian
Posts: 343

Rep: Reputation: 43
Just wanted to know, have you still both cwarmup and c-warmup and, if not, have you tried again to compile?

Last edited by manu-tm; 04-09-2013 at 02:58 PM.
 
Old 04-09-2013, 02:25 PM   #12
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by vwinnecke View Post
lso, I'm wondering why it shows in the error
driver.c: (.text+0x17f):

the part in () instead of showing a line number. Could that indicate a problem?
It compiles before linking. You gave a single cc command to both compile the main source file and link. But cc still does that as two separate operations.

After being compiled, you main source file is a binary file. Its external references are not tied to line numbers in the original source code.

The position of the reference within the binary file is useless information to almost all users. So including that information in the error message may have been bad design for that GNU tool. But that is the way it works, so users of GNU binary tools learn to ignore that.
 
  


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
Undefined reference to function hjazz6 Programming 2 06-21-2011 07:12 AM
[SOLVED] How to debug undefined reference to library function (yad 0.5.1)? catkin Linux - Software 3 10-17-2010 07:08 AM
Dlopen function - UNDEFINED REFERENCE mamthababu Programming 1 07-19-2005 10:20 AM
Undefined reference to function error Quest101 Linux - Newbie 0 12-30-2004 05:01 PM
undefined reference to a function included in a library i made!!! keos Programming 5 02-21-2004 04:02 PM

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

All times are GMT -5. The time now is 02:54 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