c++cmdhyperlinklinkerexternal

How to avoid multiple -l in C++ compiling when using libraries


I have met lots of problems while using external libraries in C++. The library includes a include file of header files, and a lib file which includes all .la, .so files. I added the library in to /usr/local/include and /usr/local/lib, and also edited the ld.so.conf file. After all these were done, I supposed that my program could be compiled successfully. However, an error occurred:

$ g++ -o b try.cpp
 /usr/bin/ld: /tmp/ccTWcUvt.o: in function `main':
try.cpp:(.text+0x36): undefined reference to `OsiClpSolverInterface::OsiClpSolverInterface()'
/usr/bin/ld: try.cpp:(.text+0x45): undefined reference to `OsiClpSolverInterface::~OsiClpSolverInterface()'

then I typed all the library file names behind -l manually and compiled again with this command:

g++ -o b try.cpp -lCbc -lCbcSolver -lCgl -lClp -lcoinasl -lcoinglpk -lcoinmumps -lCoinUtils -lOsi -lOsiCbc -lOsiClp -lOsiCommonTest -lOsiGlpk

This time, it did compile successfully and the program worked great. I'm wondering whether there is any method to avoid this verbosity? Also, if anyone could explain to me why just adding the path to the lib files are not enough, but must point out the names explicitly, I would be so very grateful as well.


Solution

  • The linker knows which functions you are calling. It does not know which libraries those functions are contained in, and it's not going to go searching through many hundreds or thousands of libraries to find them.

    With gcc, more so than with Visual C++, the order of the libraries can be important, so they don't even support the pragma to specify libraries.

    This is not verbosity. You need to be using Makefiles if it is too much typing for you.