CMake: How to Link Shared Libraries

ClickBlue Text

Follow Us

CMake is a simple build tool that saves us from designing complex Makefiles. The author has previously organized two related articles as follows:

  • “CMake, a Build Tool for Large Projects”

  • “CMake Static and Dynamic Library Construction”

Today’s article continues to expand on the project discussed in the previous article. If you are not very familiar with making dynamic and static libraries with CMake, it is recommended to check the description in the previous article first.

Dynamic Library Linking

In the last section, we created a dynamic library called librice.so and installed it, so we will directly use this dynamic library. Next, let’s create an example to explain how to link a dynamic library. The example structure is as follows:

$ tree -L 3.├── build├── CMakeLists.txt└── src    ├── CMakeLists.txt    └── main.c
2 directories, 3 files$

Contents of the CMakeLists.txt file in the project directory:

PROJECT(MAIN_TEST)
ADD_SUBDIRECTORY(src)

Contents of the CMakeLists.txt file in the src directory:

SET(TEST_SRC main.c)
ADD_EXECUTABLE(main ${TEST_SRC})
INCLUDE_DIRECTORIES(/usr/include/rice)TARGET_LINK_LIBRARIES(main librice.so)

Contents of main.c:

#include <rice.h>
int main(int argc, char *argv[]){
    printf("test sample\n");
    rice_func();
    return 0;
}

Compile and run (using external build):

$ cmake ..# omitted....$ make# omitted.....# Check the linking status of main$ ldd src/main         linux-vdso.so.1 =>  (0x00007ffecf33a000)        librice.so => /usr/lib/librice.so (0x00007f5754a69000)        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f575469f000)        /lib64/ld-linux-x86-64.so.2 (0x00007f5754c6b000)$ ./main test samplerice func$

As we can see, the executable file main is linked to the dynamic library rice.

In the above CMakeLists.txt file, there are two new commands: INCLUDE_DIRECTORIES and TARGET_LINK_LIBRARIES. The command descriptions are as follows:

Command INCLUDE_DIRECTORIES
Syntax INCLUDE_DIRECTORIES([AFTER|BEFORE] [SYSTEM] dir1 dir2 …)
Description This command is used to add multiple specific header file search paths to the project, separated by spaces. If the path contains spaces, you can enclose it in double quotes. The default behavior is to append to the end of the current header file search path. You can control the way the search path is added in two ways: 1. By setting the CMake variable CMAKE_INCLUDE_DIRECTORIES_BEFORE to on, you can place the added header file search paths before the existing paths. 2. By using the AFTER or BEFORE parameters, you can also control whether to append or place it first.
Command TARGET_LINK_LIBRARIES
Syntax TARGET_LINK_LIBRARIES(target library1 <debug | optimized> library2 …)
Description This command can be used to add shared libraries that need to be linked for the target.

Static Library Linking

Linking a static library only requires modifying a little content in the example above. As follows:

Modify the CMakeLists.txt file in src:

Change TARGET_LINK_LIBRARIES(main librice.so) to: TARGET_LINK_LIBRARIES(main librice.a)

Compile and check the results:

$ ldd ./src/main         linux-vdso.so.1 =>  (0x00007ffead3ec000)        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f75fcb16000)        /lib64/ld-linux-x86-64.so.2 (0x00007f75fcee0000)$ ./main test samplerice func$

From the above results, we can see that main depends on the static library.

If you have any questions, feel free to add the author on WeChat for discussion…

CMake: How to Link Shared LibrariesCMake: How to Link Shared LibrariesCMake: How to Link Shared Libraries

I will continue to update articles and learning materials.

You can add the author on WeChat to discuss and learn together.

CMake: How to Link Shared Libraries

Author WeChat ID: wueroo1314

To join the discussion group, please add the author’s WeChat

Fan Zai DIY

WeChat ID: Rice_DIY

Technology | Open Source | Sharing

Long press to follow…

CMake: How to Link Shared Libraries

Leave a Comment