Wednesday, November 20, 2013

Solved: Misleading link errors with gcc on linux

Solution Summary: LDFLAGS come in last

Background
I have been monkeying around with an issue of linking my code with xerces-c++ and xalan-c++ library for hours. Wondering what was I doing wrong.

I was issuing the following command for compiling

g++ -o file.out -lxalan-c -lxerces-c program.cpp

I was getting the following errors

program.cpp:(.text+0x75): undefined reference to `xercesc_3_1::XMLUni::fgXercescDefaultLocale'
program.cpp:(.text+0x7a): undefined reference to `xercesc_3_1::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_1::PanicHandler*, xercesc_3_1::MemoryManager*)'
program.cpp:(.text+0x7f): undefined reference to `xalanc_1_11::XalanMemMgrs::getDefaultXercesMemMgr()'
program.cpp:(.text+0x87): undefined reference to `xalanc_1_11::XPathEvaluator::initialize(xercesc_3_1::MemoryManager&)'
program.cpp:(.text+0x8c): undefined reference to `xalanc_1_11::XalanMemMgrs::getDefaultXercesMemMgr()'
program.cpp:(.text+0x9e): undefined reference to `xalanc_1_11::XalanSourceTreeInit::XalanSourceTreeInit(xercesc_3_1::MemoryManager&)'
program.cpp:(.text+0xac): undefined reference to `xalanc_1_11::XalanSourceTreeDOMSupport::XalanSourceTreeDOMSupport()'
program.cpp:(.text+0xb1): undefined reference to `xalanc_1_11::XalanMemMgrs::getDefaultXercesMemMgr()'
program.cpp:(.text+0xcd): undefined reference to `xalanc_1_11::XalanSourceTreeParserLiaison::XalanSourceTreeParserLiaison(xalanc_1_11::XalanSourceTreeDOMSupport&, xercesc_3_1::MemoryManager&)'
program.cpp:(.text+0xea): undefined reference to `xalanc_1_11::XalanMemMgrs::getDefaultXercesMemMgr()'
program.cpp:(.text+0x112): undefined reference to `xalanc_1_11::XalanDOMString::XalanDOMString(char const*, xercesc_3_1::MemoryManager&, unsigned int)'
program.cpp:(.text+0x118): undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'



Thanks to this Stackoverflow Discussion. I found out what I was doing wrong. Although the discussion was about compiling in windows but it gave me the clue.

So basically, while it did work on suse, the actual syntax is (LDFLAGS come in last)

g++ -o exectuable <object files> <ld flags>

So I changed my command to the following and got my issue fixed.

g++ -o file.out  program.cpp -lxalan-c -lxerces-c

I am sharing this so the next guy who has similar issues will hopefully find this post and save him some trouble.

Happy coding!

3 comments:

  1. Get a good chance at finding finance companies at loansforbadcreditwithmonthlypayments.blogspot.com. I like how they disclose all fees

    ReplyDelete
  2. Thank for helping in coding and programming. I love programming and want to know more about it. I will apply some codes in my website regarding 2020 best digital piano in range of $500.

    ReplyDelete
  3. Such a creative and informative blog!

    On Demand Service Apps For Android and iOS are highly in demand as through this online platform. App ideas provide them on-demand service when customers need it.

    ReplyDelete