Alejandro Silvestri Alejandro Silvestri - 3 months ago 15
C++ Question

How does the IDE to find the shared library name .so from the header file .h

I wonder if the IDE/compiler/linker has a method to identify the shared object filename (lib*.so) from the header filename.

I use eclipse CDT, I include stdio and others like everyone, but for some odd libraries I must tell the IDE the library name and path. For:

#include "opencv2/core/core.hpp"


I have to tell the linker to use the library opencv_core (IDE generate a -lopencv_core parameter for the linker, so it looks for libopencv_core.so).

It is very clear how to get the library filename from the library name. I not sure how to get it from the header file.

I couldn't find the answer to this question in stackoverflow nor anywhere else, so I suspect there's no relation between both header and library filenames.

Thank you very much.

Answer

Your suspicions were right on the mark. Given some header file sitting somewhere, there's nothing invisible or hidden in that header file that's telling the compiler "Hey, my library is over there!". At least nothing in the C++ standard. Individual C++ implementations are free to do whatever they want to do, and might have something like that. But as far as the C++ standard goes, the answer is: nope.

As far as your linker goes, it depends on each C++ implementation or operating system. In some cases, the linker has a default list of directories it searches for libraries to link with, and the standard C++ libraries go in there.

In other cases, the linker does not get invoked directly, but it gets invoked by the compiler. This would be the case with gcc. A typical link operation with gcc would be somewhere along the lines of

g++ -o prg module1.o module2.o

gcc knows where it's library is. It invokes the linker, typically ld, and supplies the location of the C++ standard library, in addition to the modules to link together.

Comments