2

I'm trying to compile the following sample code available at XERCES site:

#include <xercesc/util/PlatformUtils.hpp>
// Other include files, declarations, and non-Xerces-C++ initializations.
XERCES_CPP_NAMESPACE_USE

int main(int argc, char* argv[])
{
  try {
      XMLPlatformUtils::Initialize();

  }
  catch (const XMLException& toCatch) {
    // Do your failure processing here
    return 1;
  }

  // Do your actual work with Xerces-C++ here.

  XMLPlatformUtils::Terminate();

  // Other terminations and cleanup.
  return 0;
}

with,

g++ -g -Wall -pedantic -L/usr/lib -lxerces-c -o xercesTest xercesTest.cpp

giving me the following linking error:

/tmp/ccYIHCfR.o: In function `main':
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLUni::fgXercescDefaultLocale'
/home/cjmv/temp/xercesTest.cpp:8: undefined reference to `xercesc_2_8::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_2_8::PanicHandler*, xercesc_2_8::MemoryManager*, bool)'
/home/cjmv/temp/xercesTest.cpp:18: undefined reference to `xercesc_2_8::XMLPlatformUtils::Terminate()'
/tmp/ccYIHCfR.o:(.gcc_except_table+0x10): undefined reference to `typeinfo for xercesc_2_8::XMLException'
collect2: ld returned 1 exit status

I've installed xerces-c28 and xerces-c2-dev through aptitude on my ubuntu-server 12.04

Any help would be appreciated.

cvicente
  • 132
  • 3
  • 12

2 Answers2

6

Put the library last on the command line:

g++ -g -Wall -pedantic -L/usr/lib -o xercesTest xercesTest.cpp -lxerces-c
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Man... I feel terrible!... The shaaaame... how the hell didn't I see this?!? :-( Thank you!!! – cvicente May 23 '12 at 15:45
  • Thanks, why does this fix the problem? – Mike S Aug 31 '15 at 17:23
  • 1
    @MikeSlutsky When the linker sees a linker-library it will see if any of the currently undefined symbols matches and of the exported symbols in the library, and then discards the library and doesn't search the library again. If there are no undefined symbols, like when you place the library before all object files, then the library is simply discarded. – Some programmer dude Aug 31 '15 at 19:28
0

include the lib path of xerces: try this

g++ -I/<xerces-c 2.8.0 path>/include -c xercesTest.cpp
g++ -L/<xerces-c 2.8.0 path>/lib -lxerces-c xercesTest.o
Dinesh Reddy
  • 775
  • 1
  • 11
  • 25