Tutorial on the usage of BUILD_SHARED_LIBS and CMAKE_BUILD_TYPE in CMake

Original link: https://ifmet.cn/posts/29f14999/

**Brief description: ** Originated from #2 , it was found that the debug compilation failed, but the release compilation succeeded, because the default generated product did not match the expectation, so a PR was mentioned. Found two variable knowledge points related to CMAKE_BUILD_TYPE and BUILD_SHARED_LIBS in CMake.


This article was originally published on ” Xie Zang’s Small Station “, and is reproduced here simultaneously.

💻 win10 21H2 📎 Qt 5.12.11 📎 Visual Studio 2019


Is a global flag of add_library(), when add_library() does not specify which one [STATIC | SHARED | MODULE] is, it can decide to generate dynamic library or static library .

Typical values:

  • ON : let add_library() generate .dll dynamic library, corresponding to SHARED
  • OFF : let add_library() generate .lib static library, corresponding to STATIC; default value

The CMake code is as follows, and the corresponding generated product is shown in the figure:

 set ( BUILD_SHARED_LIBS ON ) # default is OFF add_library ( ${ PROJECT_NAME } ${ SRCS_MAIN } ) 



Specifies the build type of the build product .

Typical values:

  • Debug : Detailed debugging information
  • Release : no debug info
  • RelWithDebInfo : Release with debug information, still possibly slightly optimized
  • MinSizeRel : not used


For projects that generate .dll/.lib:

  1. It is recommended to use add_library() without specifying STATIC | SHARED, but controlled by the BUILD_SHARED_LIBS variable.
  2. At the same time, it is determined by custom macros, and the generated .dll adopts __declspec(dllexport) or __declspec(dllimport) .

This part, as well as how to use .dll, will be explained in detail later.

Attachment: The modified source code corresponding to this article is in this CMakeLists.txt

Serial address


Welcome to star ⭐ and fork 🍴 this series of CMake learning, with a directory of learning from shallow to deep.

This article is reproduced from: https://ifmet.cn/posts/29f14999/
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment

Your email address will not be published.