CMake:Modules
lastmod 2023-03-02created 2016-04-199 edits
Notes on modules written in CMake script.
Category
- CMake
- CMakeLists.txt
- CMake:Variables
- CMake:Test (CTest)
- CMake:Pack (CPack)
- CMake:Policies
- CMake:Example
- CMake:Troubleshooting
CMakeParseArguments
Helps parse CMake-style arguments.
FindProtobuf
- Stackoverflow: CMake and FindProtobuf
- Stackoverflow: How to link google protobuf libraries via cmake on linux?
- Day 2: Protobuf, cmake and eclipse
Example:
include(FindProtobuf)
find_package(Protobuf REQUIRED)
include_directories(${PROTOBUF_INCLUDE_DIR})
# ...
target_link_libraries(complex
${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${PROTOBUF_LIBRARY}
)
FindwxWidgets
Not found wxWidgets in MinGW
If it cannot be found properly in MinGW, check the file paths. Refer to the code below.
if(WX_ROOT_DIR)
# Select one default tree inside the already determined wx tree.
# Prefer static/shared order usually consistent with build
# settings.
set(_WX_TOOL "")
set(_WX_TOOLVER "")
set(_WX_ARCH "")
if(MINGW)
set(_WX_TOOL gcc)
elseif(MSVC)
set(_WX_TOOL vc)
if(MSVC14)
set(_WX_TOOLVER 140)
elseif(MSVC12)
set(_WX_TOOLVER 120)
elseif(MSVC11)
set(_WX_TOOLVER 110)
elseif(MSVC10)
set(_WX_TOOLVER 100)
elseif(MSVC90)
set(_WX_TOOLVER 90)
endif()
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(_WX_ARCH _x64)
endif()
endif()
## ...
find_path(wxWidgets_LIB_DIR
NAMES
msw/wx/setup.h
mswd/wx/setup.h
mswu/wx/setup.h
mswud/wx/setup.h
mswuniv/wx/setup.h
mswunivd/wx/setup.h
mswunivu/wx/setup.h
mswunivud/wx/setup.h
PATHS
${WX_ROOT_DIR}/lib/${_WX_TOOL}${_WX_TOOLVER}${_WX_ARCH}_dll # prefer shared
${WX_ROOT_DIR}/lib/${_WX_TOOL}${_WX_ARCH}_dll # prefer shared
${WX_ROOT_DIR}/lib/${_WX_TOOL}${_WX_TOOLVER}${_WX_ARCH}_lib
${WX_ROOT_DIR}/lib/${_WX_TOOL}${_WX_ARCH}_lib
DOC "Path to wxWidgets libraries"
NO_DEFAULT_PATH)
## ...
endif ()
In the end, the wx-base library must be located at a path such as C:\wxWidgets-3.1.0\lib\gcc_dll\libwxbase31u.a.
Also, for MinGW, the order of Components matters.
# Note that for MinGW users the order of libs is important!
find_package(wxWidgets COMPONENTS net gl core base)
ExternalProject
- Building External Projects with CMake 2.8
- CMake » 3.0.2 Documentation » cmake-modules(7) » ExternalProject
Applies an external project. It is applied according to the overall build order.
Example
Below is an example that downloads and installs zlib.
include (ExternalProject)
set (DEP_PREFIX_DIR "${CMAKE_BINARY_DIR}/dep/source")
set (DEP_INSTALL_DIR "${CMAKE_BINARY_DIR}/dep/local")
ExternalProject_Add (zlib
PREFIX "${DEP_PREFIX_DIR}"
#--Download step--------------
URL "http://www.zlib.net/zlib-1.2.11.tar.gz"
URL_MD5 "1c9f62f0778697a09d36121ead88e08e"
#--Configure step-------------
CMAKE_ARGS "-DCMAKE_MACOSX_RPATH=TRUE"
"-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
"-DCMAKE_INSTALL_PREFIX=${DEP_INSTALL_DIR}"
#--Output logging-------------
LOG_DOWNLOAD 1
)
ProcessorCount
You can check the number of processes as follows.
CMakeParseArguments
As an example here a my_install() macro, which takes similar arguments as the real install() command:
function(MY_INSTALL)
set(options OPTIONAL FAST)
set(oneValueArgs DESTINATION RENAME)
set(multiValueArgs TARGETS CONFIGURATIONS)
cmake_parse_arguments(MY_INSTALL "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )
...
Assume my_install() has been called like this:
After the cmake_parse_arguments() call the macro will have set the following variables:
MY_INSTALL_OPTIONAL = TRUE
MY_INSTALL_FAST = FALSE (this option was not used when calling my_install()
MY_INSTALL_DESTINATION = "bin"
MY_INSTALL_RENAME = "" (was not used)
MY_INSTALL_TARGETS = "foo;bar"
MY_INSTALL_CONFIGURATIONS = "" (was not used)
MY_INSTALL_UNPARSED_ARGUMENTS = "blub" (no value expected after "OPTIONAL"
You can then continue and process these variables.
GenerateExportHeader
- Stackoverflow - Export all symbols when creating a DLL
- Create dlls on Windows without declspec() using new CMake export all feature
- vlasovmaksim/cmake_windows_export_all_symbols
Windows Symbol export
To try this out on an existing project, run
on a CMake project. This should turn all add_library calls that do not explicitly specify build type into shared builds. If there are no global data variables in the project, all libraries will be built as DLLs with no errors. If you run into undefined symbols, check for global data like static data members of classes. The easiest way to handle the global data is to use the CMake GenerateExportHeader module like this:
add_library(mylibrary ${mylibrary_SOURCES})
# add these lines
include(GenerateExportHeader)
generate_export_header(mylibrary)
FindwxWidgets
Find a wxWidgets (a.k.a., wxWindows) installation.
Could NOT find wxWidgets in Windows
If you installed wxWidgets on Windows using MSYS, try applying the following.
set (wxWidgets_ROOT_DIR "C:\\wxWIdgets")
set (CMAKE_CROSSCOMPILING YES) # Force setting: wxWidgets_FIND_STYLE = unix
FetchContent
Examples
How to create a module
- CMake:How To Find Libraries
- CMake » 3.0.2 Documentation » cmake-modules(7) » FindPackageHandleStandardArgs
To search for files, use the following commands.
- find_file
- find_library
- find_package
- find_path
- find_program
Then create the cache with the mark_as_advanced command.