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 03-25-2014, 08:28 AM   #1
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Rep: Reputation: Disabled
compiling multiple file code in Linux


Hi;
Can someone explain why, when I use 'make' cmd in processing code composed of list.c, list.h, and p1704.c (note that my 'make' cmd has following switches:
Quote:
clang -ggdb3 -O0 -std=c99 -Wall -Werror p1703.c -lcs50 -lm -o p1703
)
I get the following failed terminal output:
[CODE/home/jharvard/prata/prata17/p1704.c:15: undefined reference to `InitializeList'
/home/jharvard/prata/prata17/p1704.c:16: undefined reference to `ListIsFull'
/home/jharvard/prata/prata17/p1704.c:30: undefined reference to `AddItem'
/home/jharvard/prata/prata17/p1704.c:35: undefined reference to `ListIsFull'
/home/jharvard/prata/prata17/p1704.c:43: undefined reference to `ListIsEmpty'
/home/jharvard/prata/prata17/p1704.c:48: undefined reference to `Traverse'
/home/jharvard/prata/prata17/p1704.c:50: undefined reference to `ListItemCount'
/home/jharvard/prata/prata17/p1704.c:53: undefined reference to `EmptyTheList'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: Nothing to be done for `list.c'.
CODE]
yet, if I compile only with:
Quote:
clang -std=c99 p1703.c list.c -op1703
my code compiles just fine.

I have only a simple guide to Linux, and an internet search of the initial error "output" told me that I leaft out libraries, which was not at all the case.
Thanks
 
Old 03-25-2014, 09:15 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
it is not related to linux but to the compiler you use. This message means you use something named ListIsEmpty or Traverse but you have not defined it. Usually it means you missed to include a header file containing that information. These messages are warnings, the compiler assumes some defaults, therefore it will be able to produce a result. Using -Wall -Werror means you want to stop in case of such warnings therefore your command will fail.
 
1 members found this post helpful.
Old 03-25-2014, 10:05 AM   #3
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Double what pan64 says and I believe if you use -Wall you'll see your warnings, the -Werror will cause the compile to interpret all warnings as failures. I recommend that and a few other flags. I believe that you ought to remove all warnings. To me the undefined references are pretty serious problems; but I do realize it's possible to have those warnings and not have execution problems.

Review the manpage or online documentation for gcc, this is where those flags are described.
 
1 members found this post helpful.
Old 03-25-2014, 10:27 AM   #4
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
it is not related to linux but to the compiler you use. This message means you use something named ListIsEmpty or Traverse but you have not defined it. Usually it means you missed to include a header file containing that information. These messages are warnings, the compiler assumes some defaults, therefore it will be able to produce a result. Using -Wall -Werror means you want to stop in case of such warnings therefore your command will fail.
Thanks for the replies from all. What I still don't understand is that allof the functions are defined in the .h file, with code in the .c file, and function calls in the "main" file. Additionally, I note that the terminal window flagged ALL functions in the code as "undefined reference": not as if I erred with 1 or 2. It's like it wasn't linking the .h file???
 
Old 03-25-2014, 10:39 AM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
You need an include path reference, the -I flag, even if it is just:
Code:
-I.
One of my simple Makefiles looks something more like this: Note the -I. in the CFLAGS, this tells GCC to reference my working directory for include files.
Code:
CFLAGS= -I. -O3 -ggdb -Wformat=2 -Werror -Wall -Wextra -Wswitch-default -Wswitch-enum -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wmissing-noreturn

OBJS= <series of .o file names>

GCC= /usr/bin/gcc

%.o:	%.c
	$(GCC) $(CFLAGS) -c $<

rebuild:
	make clean
	make <appname>
	make install

<appname>:	$(OBJS)
	$(GCC) $(OBJS) $(LFLAGS) -o <app>

install:
	cp -f <appname> $(HOME)/<appname>

clean:
	rm -f $(OBJS)
	rm -f $(HOME)/<appname>
 
1 members found this post helpful.
Old 03-25-2014, 10:40 AM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by atlantis43 View Post
It's like it wasn't linking the .h file???
Well, is it?
One thing I noticed is that your second command is bringing in list.c, while the first command is not.
 
Old 03-25-2014, 10:47 AM   #7
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by suicidaleggroll View Post
Well, is it?
One thing I noticed is that your second command is bringing in list.c, while the first command is not.
The command line used was actually
Code:
make p1704 list.c
What was listed is my compilers compilation equivalent for "make", which seems to have left out the list.c file
 
Old 03-25-2014, 10:58 AM   #8
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
You need an include path reference, the -I flag, even if it is just:
Code:
-I.
I guess that was missing from my pre-configured "make" command so that it didn't work. I'll have to see what happens when I run clang with -Wall & -Werror to see if that fails to compile (with or without -I).
 
Old 03-25-2014, 11:40 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Guess the problem is that you came from java (or similiar). Compilation did use 'list.h', but at linkage you still have to specify 'list.o' as a part of the project:

Code:
cc -o final.exe p1704.o list.o
 
Old 03-25-2014, 11:42 AM   #10
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I've never heard of a compiler linking a header (.h) file. The compiler uses the file for reference so that it can see the 'signatures' of the functions, and compares these to how they are called (in .c files).

Also, earlier it was hinted that an "undefined reference" was due to a header file not being referenced or found; that too is incorrect information. An "undefined reference" means that the linker (not the compiler) was unable to reference a particular function.

Lastly, another piece of information that is slightly incorrect is that one needs to specify a -I. to look for headers in the current directory. This is only true if including a local header file using the angle-brackets (e.g. #include <list.h> ). The OP has not posted any code, but my bet is that he is including the header file using double-quotes (e.g. #include "list.h" ), and thus the -I. option is not required. If his header file were in a different location other than the current directory, then regardless of whether using double-quotes or the angle-brackets, then the -I<path> option would be needed.

In conclusion, what the OP needs to do (in his Makefile) is to generate individual object files for each of his source code files, and then afterwards link them together to form the executable file. He can generate the object file for each source file using the -c option.

EDIT:

Something like this:
Code:
clang -Wall -c list.c

clang -Wall -c p1703.c

clang list.o p1703.o -o p1703 -L. -lcs50 -lm
Note the -L option; this indicates that the library 'cs50' is present in the current directory. If it is not, then modify the path to the correct location.

Last edited by dwhitney67; 03-25-2014 at 11:46 AM.
 
Old 03-25-2014, 11:59 AM   #11
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
If you have further problems, please post your Makefile within [code][/code] tags.

Another recommendation if you ever have a question as to whether an include file or part of a source file is being compiled, put an obvious syntax error within that file and you should see immediately that failure in your next compilation. If you don't, then something is not happening which you thought was. The include file is not "compiled" but it would be parsed by the compiler.

Last edited by rtmistler; 03-25-2014 at 12:01 PM.
 
1 members found this post helpful.
Old 03-25-2014, 12:22 PM   #12
atlantis43
Member
 
Registered: Feb 2013
Posts: 289

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Guess the problem is that you came from java (or similiar). Compilation did use 'list.h', but at linkage you still have to specify 'list.o' as a part of the project:
No java: just trying to learn C from a text (Prata) and an online course (which provided a compiler with a pre-configured "make" file.
Quote:
Originally Posted by dwhitney67 View Post
Also, earlier it was hinted that an "undefined reference" was due to a header file not being referenced or found; that too is incorrect information. An "undefined reference" means that the linker (not the compiler) was unable to reference a particular function.
This info was obtain via internet search for "clang undefined reference". Either not satisfactory search criteria, or inadequate knowledge on my part for understanding the full meaning of the answer.
Quote:
Originally Posted by dwhitney67 View Post
This is only true if including a local header file using the angle-brackets (e.g. #include <list.h> ). The OP has not posted any code, but my bet is that he is including the header file using double-quotes (e.g. #include "list.h" ),
Correct bet!
Quote:
Originally Posted by rtmistler View Post
If you have further problems, please post your Makefile within tags.
no Makefile with this particular code, though I've had other programs with Makefile included. I'll have to learn a lot more about compilation switches before making my own Makefile(s). This was simply code from the text, intended for linking by the compiler

Last edited by atlantis43; 03-25-2014 at 12:25 PM.
 
Old 03-26-2014, 01:35 AM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,850

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
Quote:
Originally Posted by atlantis43 View Post
Thanks for the replies from all. What I still don't understand is that allof the functions are defined in the .h file, with code in the .c file, and function calls in the "main" file. Additionally, I note that the terminal window flagged ALL functions in the code as "undefined reference": not as if I erred with 1 or 2. It's like it wasn't linking the .h file???
Would be nice to post your source files - if possible. Or at least the #include lines ....
 
Old 03-26-2014, 07:24 AM   #14
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Just noticing that your original posting shows that you're compiling p1703.c however the complaints are in p1704.c. So I recommend you post all the code in question. I understand that you're not using a Makefile, so what is the clang command mapped too? Is that a certain compiler? If so, then run that command with a version argument to print out what that exact compiler is or see if it is a symbolic link pointing to something like gcc.

Also, just look at the warnings you have, it says there are undefined references to various symbols. Well, where are those symbols defined?

Please post code in [code][/code] tags.
 
Old 03-26-2014, 07:33 AM   #15
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by rtmistler View Post
Is that a certain compiler?
I had a similar question a few months ago. Here's what I found... http://clang.llvm.org/

When I purchased a Mac OS X system (for Xmas), it included clang with the Xcode package.
 
1 members found this post helpful.
  


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
How to filter the linux kernel code based on config file. removing unnecessary code atulmt Linux - Kernel 4 06-29-2013 11:14 PM
[SOLVED] AWK: split the file into multiple file and request for explanation of a known code cristalp Programming 4 11-23-2011 07:29 AM
[SOLVED] "missing kernel .config file" error while compiling linux source code for android preetb123 Linux - Mobile 3 03-19-2011 07:46 AM
Compiling my code on linux? intelygent Programming 5 10-14-2009 02:21 PM
Compiling multiple .c file using single command in makefile vipulc Linux - General 2 03-18-2006 11:49 PM

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

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