LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   "undefined symbol" when trying to compile c? (https://www.linuxquestions.org/questions/programming-9/undefined-symbol-when-trying-to-compile-c-256994/)

SciYro 11-19-2004 04:34 PM

"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?

Tinkster 11-19-2004 04:50 PM

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

ToniT 11-19-2004 04:54 PM

You have wrong library name.
Code:

gcc -o test filetypes.c -I/usr/include/libxml2 -L/usr/lib -lxml2 -lz

SciYro 11-19-2004 08:26 PM

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 ..?

Tinkster 11-20-2004 12:22 AM

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

SciYro 11-24-2004 08:07 PM

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?

jlliagre 11-25-2004 02:08 AM

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


All times are GMT -5. The time now is 05:45 AM.