4

I am trying to compile a project that depends on the Xerces XML Parser. The project compiles for Windows without any difficulty, but I'm having some trouble compiling it with g++ in Cygwin.

In order to use Xerces, I am trying to compile my code against the static library libxerces-c.a. But when I do so, I get errors that look like this:

/tmp/cc2QGvMh.o:test.cpp:(.text+0x3a): undefined reference to `xercesc_2_8::DOMImplementationRegistry::getDOMImplementation(unsigned short const*)'

I've inspected the static library using ar, and confirmed that it contains the DOMImplementationRegistry.o file that defines the function that I am calling.

ar -t libxerces-c.a
...
DOMImplementationImpl.o
DOMImplementationRegistry.o
DOMLocatorImpl.o
...

I've also extracted the object files from the library, and used 'nm' to make sure that the function I am calling actually exists:

ar -x libxerces-c.a
nm --demangle DOMImplementationRegistry.o
...
00000080 T xercesc_2_8::getDOMImplSrcVectorMutex()
00000300 T xercesc_2_8::DOMImplementationRegistry::getDOMImplementation(unsigned short const*)
000002a0 T xercesc_2_8::DOMImplementationRegistry::addSource(xercesc_2_8::DOMImplementationSource*)
...

Since I can compile everything for Windows but not with g++, I thought that the error could be in the linker order (similar to the problem described in this question). However, even after changing the linker order, I am still getting the same compiler error. I have tried both

g++ -o test.exe test.cpp -Llib -lxerces-c

and

g++ -o test.exe test.cpp lib/libxerces-c.a

Any ideas?

Community
  • 1
  • 1
theisenp
  • 8,639
  • 5
  • 39
  • 47
  • This could be possibly problem of different name mangling schema used by different compilers. Could you supply results of nm on your object file and on library without --demangle option? – Kamil Yurtsever Jul 12 '12 at 22:07
  • are you using the correct distribution of the library? xerces-c_2_8_0-x86-linux-gcc_3_4.tar.gz ? or did you build the library yourself on gcc? – carlsborg Jul 15 '12 at 09:47

3 Answers3

7

Your project uses method from xercesc_2_6 namespace as pointed by compiler error message but your library offers xercesc_2_8 version. Problem is probably caused by mismatch between headers you use and library object file.

  • Thanks, good catch! I fixed the project to use both the headers and the library from xerces_2_8, but I am still getting the same error. I've updated my question to reflect the new version. – theisenp Jul 06 '12 at 18:51
4

You didn't say the source of the archive. If it isn't compiled with cygwin, it could be a name mangling problem. Compiling the library from source might well fix this.

It could also be that the archive is built incorrectly so that it has internal resolution problems. Try giving the library name twice.

g++ -o test.exe test.cpp lib/libxerces-c.a lib/libxerces-c.a

If this works, the archive is broken and you should look for or build a new one.

Gene
  • 46,253
  • 4
  • 58
  • 96
  • Thanks for the suggestion. The archive that I was using was not compiled with Cygwin. I compiled everything from source and it resolved the linker errors. Thanks for the help! – theisenp Jul 15 '12 at 16:46
1

Try the linker option --enable-stdcall-fixup (see 'man ld'). It will care for name mangling and calling conventions:

g++ -o test.exe test.o -Wl,--enable-stdcall-fixup -Llib -lxerces-c
pdug
  • 45
  • 3