ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
)
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
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.
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.
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???
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.
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).
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:
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.
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.
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.
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
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
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
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.
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 ....
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.