Building Static and Dynamic Libraries with CMake

Clickthe blue text

Follow us

In a previous article, we discussed how to use CMake to compile “hello world”. This article will cover building static and dynamic libraries.

Link to the previous article: “CMake, a Build Tool for Large Projects”

For an understanding of static and dynamic libraries, you can refer to the previous articles:

Static Library: “Implementation of Static Libraries”

Dynamic Library: “Implementation of Dynamic Libraries”

In the previous article, we talked about the difference between internal and external builds; for the examples below, we will use external builds.

We will create a CMakeLists.txt, a lib directory, and a build directory in the project directory.

The content of CMakeLists.txt is as follows:

PROJECT(RICE)
ADD_SUBDIRECTORY(lib lib) # Specify the output location for compilation

In the lib directory, create the source files rice.c and rice.h, and create a CMakeLists.txt file.

rice.c content is as follows:

#include "rice.h"
void rice_func(){        printf("rice func\n");}

rice.h content is as follows:

#ifndef __RICE_H
#define __RICE_H
#include <stdio.h>
void rice_func();
#endif

CMakeLists.txt content is as follows:

SET(LIBRICE_SRC rice.c)
ADD_LIBRARY(rice STATIC ${LIBRICE_SRC})

After preparing the project template for the library, compile it in the build directory as follows:

$ cmake .. # omitted...
$ make # omitted...
$ cd lib
$ ls CMakeFiles  cmake_install.cmake  librice.a  Makefile

After compilation, the static library librice.a was generated in the lib directory. In the CMakeLists.txt above, there is an additional instruction ADD_LIBRARY:<span>:</span>

Instruction ADD_LIBRARY
Syntax ADD_LIBRARY(libname [SHARED|STATIC|MODULE] [EXCLUDE_FROM_ALL] source1 source2 … sourceN)
Description There are three types: SHARED, dynamic library STATIC, static library MODULE, effective on systems using dyld; if dyld is not supported, it is treated as SHARED. EXCLUDE_FROM_ALL means this library will not be built by default unless there are other components that depend on it or it is built manually.

Based on the description of the ADD_LIBRARY instruction, it is clear that the operation in the above example generates a static library.

Now, if we replace <span>ADD_LIBRARY(rice STATIC ${LIBRICE_SRC})</span> with <span>ADD_LIBRARY(rice <span>SHARED</span> ${LIBRICE_SRC})</span>, the result will be a dynamic library.

Installing Shared Libraries and Header Files: Simply add the install instruction to the CMakeLists.txt in the lib folder as follows:

SET(LIBRICE_SRC rice.c)
ADD_LIBRARY(rice SHARED ${LIBRICE_SRC})
INSTALL(TARGETS rice LIBRARY DESTINATION lib)    # Install shared library
INSTALL(FILES rice.h DESTINATION include/rice)   # Install header file

Execute the command:

$ cmake -DCMAKE_INSTALL_PREFIX=/usr .. # omitted...
$ make # omitted...
$ make install # omitted...

# Check if shared library is installed successfully
/usr/lib$ ls librice.so librice.so
/usr/lib$
# Check if header file is installed successfully
/usr/include/rice$ ls rice.h
/usr/include/rice$

As you can see, it has been installed in the specified directory. If there are any inaccuracies, feel free to discuss with the author.Building Static and Dynamic Libraries with CMakeBuilding Static and Dynamic Libraries with CMakeBuilding Static and Dynamic Libraries with CMake

I will continue to update articles and learning materials.

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

Building Static and Dynamic Libraries with CMake

Author’s WeChat: wueroo1314

To get into the discussion group, please add the author’s WeChat

Rice DIY

WeChat: Rice_DIY

Technology | Open Source | Sharing

Long press to follow…

Building Static and Dynamic Libraries with CMake

Leave a Comment