To create an build target that creates an library, use the add_library
command:
add_library(my_lib lib.cpp)
The CMake variable BUILD_SHARED_LIBS
controls whenever to build an static (OFF
) or an shared (ON
) library, using for example cmake .. -DBUILD_SHARED_LIBS=ON
. However, you can explicitly set to build an shared or an static library by adding STATIC
or SHARED
after the target name:
add_library(my_shared_lib SHARED lib.cpp) # Builds an shared library
add_library(my_static_lib STATIC lib.cpp) # Builds an static library
The actual output file differs between systems. For example, an shared library on Unix systems is usually called libmy_shared_library.so
, but on Windows it would be my_shared_library.dll
and my_shared_library.lib
.
Like add_executable
, add EXCLUDE_FROM_ALL
before the list of source files to exclude it from the all
target:
add_library(my_lib EXCLUDE_FROM_ALL lib.cpp)
Libraries, that are designed to be loaded at runtime (for example plugins or applications using something like dlopen
), should use MODULE
instead of SHARED
/STATIC
:
add_library(my_module_lib MODULE lib.cpp)
For example, on Windows, there won't be a import (.lib
) file, because the symbols are directly exported in the .dll
.