g++ Undefined Symbol Error using shared library

2019-09-20 14:05发布

问题:

The symbol is in the file. I verified it using nm & grep but I still get the undefined symbol error when I run the compiled application. Other symbols in the shared library are working just fine. The header file declares it as extern and it is not in a conditional pre-processor block. I'm new to c/c++ so any experience with something like this would be appreciated.

There are a ton of related questions on StackOverflow, but everyone is listing source files and headers that don't mean anything to the actual context of the question. This is a general programming question: Why can't g++ link the symbol into my compiled application? Header definition checks ok, the symbol is in the shared library, and other symbols are working properly.

回答1:

You need to set your LD_LIBRARY_PATH environment variable to include the path to your development version of the library. This will ensure the run time linker (ld.so) will find it when you run your application.

Your build settings are fine but they don't affect what happens at run time. There are ways to "bake" paths into the executable at build time if you really want to. See the man page for ld.so to see how it searches for libraries.



标签: c++ linux g++