How to set link directories in CMake

For most common libraries, CMake has inbuilt modules which find and include them for compilation and linking. But, there are situations where you need to use library files (say *.so) that are in a custom directory somewhere that need to be linked with the code you are building.

One or more of such paths which should be requested to search for library files while linking can be specified like this:

link_directories(
    /home/joe/bellringer/lib64
    /home/joe/globespin/libs
)

The library files in these directories can now be specified in link_libraries or target_link_libraries using the usual -l syntax.

Reference: link_directories

Tried with: CMake 2.8.12.2 and Ubuntu 14.04

Advertisements

How Boost automatically includes libraries

Boost is mostly a template based library, but using certain functionality requires linking with its libraries. On Windows, when you include a Boost header file in your code, it automatically generates a linking dependency on its corresponding library file. This library path is used by the linker to complete its linking operation.

For example, if I use the threading features of Boost, it generates a dependency on the library file: libboost_thread-vc110-mt-gd-1_53.lib.

This filename has various components: the Boost module, the Visual Studio version, the build type and the Boost version. These are obtained from various definitions in the boost/config/auto_link.hpp header file.

If you find that your code is looking for a Boost library filename that looks wrong in any way, then you might have to look into the above header file and configure some of the definitions.

For example, I built Boost 1.53 on Visual Studio 2013, even though it is not officially supported. When I used this Boost with my code, the linker complained that it was looking for libboost_thread-vc110-mt-gd-1_53.lib. This is strange, since I have built both Boost and my code using Visual Studio 2013 (vc120), but it is looking for Visual Studio 2012 (vc110). I found that the variable BOOST_LIB_TOOLSET was set to vc110 in auto_link.hpp. Changing this fixed the error.

Tried with: Boost 1.53, Visual Studio 2013 and Windows 7 x64

How to add library in Eclipse CDT

The source code of a C/C++ project may need to be linked with an external shared library file. When compiling from the commandline, this is typically linked using the compiler option -l. For example, to link with a library file named libfoobar.so, which is placed in one of the standard library paths, you use the option -lfoobar. As you can see, the lib prefix and the file extension .so do not need to be specified.

Similarly, to add a library file to be linked in Eclipse CDT:

  1. Right-click on the project name in Project Explorer, choose Properties > C/C++ Build > Settings > Tool Settings

  2. Go to Cross G++ Linker > Libraries > Libraries.

  3. Click the + button and add the name of the library file, omit the file extension. For example, to add the library file libfoobar.so, just add foobar. To add multiple library files, add them separately like this.

Tried with: Eclipse 3.7.2, Eclipse CDT 8.0.2 and Ubuntu 12.04 LTS

How to list definitions exported by Visual C++ library file

On Windows, library files (.lib) are commonly produced by compilation using the Visual C++ compiler. Sometimes, you may want to know which library file exports the definition of a certain function or global variable.

The definitions exported by a library file can be listed by using the dumpbin tool. This ships with Visual Studio and can be invoked by opening the Developer Command Prompt for Visual Studio.

To list the symbols exported by a library file use:

C:\> dumpbin /exports foo.lib

Note that C function definitions will have an underscore as prefix. But, C++ definitions will be much more mangled. However, if you know the function name or even a substring of it, you could investigate using the output of this program.

Tried with: Visual Studio 2012 and Windows 7 x64