Re: [xml] The order of arguments when compiling



Hi spam,

On Thu, Feb 16, 2012 at 4:01 PM,   wrote:
Hello,

On a first computer, when I compile a C program using the libxml2 library I do :
$ gcc `xml2-config --cflags` `xml2-config --libs` main.c
or
$ gcc main.c `xml2-config --cflags` `xml2-config --libs`
And it works (but the first method is better because it respects the order of arguments in the gcc manual).

On a second computer, if I compile like this, there is an error :
$ gcc `xml2-config --cflags` `xml2-config --libs` main.c
/tmp/cc7uNwed.o: In function `parseDoc':
main.c:(.text+0xd): undefined reference to `xmlParseFile'
main.c:(.text+0x51): undefined reference to `xmlCleanupParser'
main.c:(.text+0x63): undefined reference to `xmlFreeDoc'
main.c:(.text+0x68): undefined reference to `xmlCleanupParser'
collect2: ld returned 1 exit status
But if I compile like this, there is no error :
$ gcc main.c `xml2-config --cflags` `xml2-config --libs`

I thought this was a gcc problem but someone on the gcc mailing-list tell me that probably I have compiled 
libxml2 on the second computer "statically". And I should compile it "dynamically". It's true that on the 
second computer, I have compiled myself the library (configure, make, make install). On the first computer, 
this is my distro package.

How should I compile libxml2 on the 2nd computer to have the same behavior on the two computers?

You shouldn't; just use the last compilation command (with
`xml2-config --libs` at the end).
Most Unix linkers are one-pass; because of this, an object which needs
a symbol must appear before the object (or library) which supplies
that symbol.

See for example http://webpages.charter.net/ppluzhnikov/linker.html

GNU make's built-in rule for linking boils down to:

%: %.c
#  commands to execute (built-in):
         $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH)      $^
    $(LOADLIBES) $(LDLIBS) -o $@

%: %.o
        $(CC) $(LDFLAGS) $(TARGET_ARCH)      $^      $(LOADLIBES)
$(LDLIBS) -o $@

As you can see, the list of source or object files ($^) is always
_before_ the list of libraries (in $(LDLIBS) or $(LOADLIBES) ).

Hope this helps,
Csaba
-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
The Tao of math: The numbers you can count are not the real numbers.
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]