LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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-19-2004, 05:34 PM   #1
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
"undefined symbol" when trying to compile c?


hello, i been having this problem for about a week now, and i cant seem to find out how to fix it

whenever i try to compile a c program, the linker gives that "undefined symbol" stuff

i have 2 separate small projects i tried to compile, one just be trying to learn how to use the xml library, and the other is a multi file c program just for fun, both compile fine with only some small warnings, the usually stuff like "inter to pointer without a cast" or ignorable stuff like that ... all the problem come at compile time, and its always the linker giving that undefined symbol stuff, and it only does that to included files for some reason ... anyways, heres the output from the xml one (just because thats smallest)

Code:
gcc -o test filetypes.c -I/usr/include/libxml2 -L/usr/lib -lxml -lz
compile no problems .. then

Code:
 filetypes.c: In function `filetypearraycreater':
filetypes.c:17: warning: return makes integer from pointer without a cast
/tmp/ccWH8GFs.o(.text+0x110): In function `createfiletypesfile':
: undefined reference to `xmlSaveFormatFile'
/tmp/ccWH8GFs.o(.text+0x125): In function `createfiletypesfile':
: undefined reference to `xmlSaveFormatFile'
collect2: ld returned 1 exit status
heres the program in question (ill remove the first 2 functions, as they are not used in anyway yet) .. all this does is call a function to create a new xml doc's

Code:
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

void createfiletypesfile ()
{

	xmlDocPtr filetypes;
	xmlNodePtr filenode;

	filetypes = xmlNewDoc ("1.0");

	xmlSaveFormatFile ("~/fm/filetypes.xml", filetypes, 1);
	xmlSaveFormatFile ("~/fm/metatypes.xml", filetypes, 1);

}

int main (argc, argv)
int *argc;
char *argv[];
{

	createfiletypesfile ();
	return 1;

}
what im i doing wrong?
 
Old 11-19-2004, 05:50 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
That's a linker problem that most likely indicates that the
file you're trying to link against (libxmls.so) is NOT in
/usr/lib but tucked away somewhere else.

I don't know where it would live in gentoo, in Slack 10
libxml.so is not present at all (by default) libxml2 is
used.



Cheers,
Tink
 
Old 11-19-2004, 05:54 PM   #3
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
You have wrong library name.
Code:
gcc -o test filetypes.c -I/usr/include/libxml2 -L/usr/lib -lxml2 -lz
 
Old 11-19-2004, 09:26 PM   #4
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Original Poster
Rep: Reputation: 51
thanks ToniT, it was the wrong library name *curses the xml-config that said to use -lxml* ...... but what could be the cause of the same errors in the other program i had?.... i know it wasn't a wrong lib's name, all the errors in linking came from trying to use functions i made,, or in same cases variables ... am i supposed to add "-I" or "-l" options to use those functions and variables ..?
 
Old 11-20-2004, 01:22 AM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
That depends on HOW you compiled the various bits
and pieces ...

You should be able to do:
gcc -c file1.c -o my1.o
gcc -c file2.c -o my2.o
gcc -c file3.c -o my3.o
gcc -o test main.c my1.o my2.o my3.o
for instance ...


Cheers,
Tink
 
Old 11-24-2004, 09:07 PM   #6
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Original Poster
Rep: Reputation: 51
sorry, i haven't responded in a while, i haven't been at my computer for some time

.. anyways, i did the compile the exact same way you showed above Tinkster, no compile errors, some warnings (i fix them later i guess) .. but when its time for the linker to do its think, all i get is "undefined" functions and variables (they were defined in the .h files, the compiler didn't mind them) ... any ideas?, or do i need to include something else to get it to want to work?
 
Old 11-25-2004, 03:08 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Quote:
all i get is "undefined" functions and variables (they were defined in the .h files, the compiler didn't mind them) ... any ideas?, or do i need to include something else to get it to want to work?
This is a frequent mistake, already discussed several times in this forum.
Functions are *not* defined in header files, but only declared, meaning your C code knows about their name, return value and parameters count and type to verify you use it the proper way.
Functions *are* defined either in libraries, that can be either object (.o) or shared objects files (.so), and are linked with your own objects to build the executable binary.
You can find out if the function you need is present in the xml2 library by using the command:
Code:
nm  -D /usr/lib/libxml2.so | grep xmlSaveFormatFile
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
PHP5 + Apache 2.x : /usr/local/modules/libphp5.so: Undefined symbol "apr_table_set" Ephracis Linux - Software 6 10-01-2010 01:26 PM
k3b won't start: "undefined symbol" error djzanni Mandriva 2 03-12-2005 11:49 AM
firefox and "undefined symbol:" deadkenny666 Linux - Newbie 0 02-14-2005 05:53 AM
rpm error only says"rpm: relocation error: rpm: undefined symbol: poptAliasOptions" dlrsims Linux - Newbie 2 11-14-2003 10:22 PM
ncurses compile error: "undefined reference" Mr. Eek Linux From Scratch 1 05-12-2003 05:25 PM


All times are GMT -5. The time now is 04:36 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration