CMakeLists.txt syntax introduction

1, Common commands 1. Specify the minimum version //Set the minimum version number required by cmake cmake_minimum_requi...
1, Common commands

1, Common commands

1. Specify the minimum version

//Set the minimum version number required by cmake cmake_minimum_required(VERSION 3.6)

2. Set project name

//Name of the project generating test.sln project(test)

3. Basic instructions

//Generate executable program add_executable(test, main.cpp) //Generate static library add_library(libstatic STATIC libstatic.cpp) //Generate dynamic library add_library(dlldemo SHARED libdemo.cpp) //Traverse CMakeLists.txt in subfolders add_subdirectory(subdir) //Set included directories include_directories($/include) //Search all cpp files aux_source_directory(. SRC_LIST) //Custom search file(GLOB SRC "*.cpp" "subdir1/*cpp") //Connection Library target_link_libraries(test libadd.lib)

4. Set variables

//Add C++11 support to the compilation options set(CMAKE_CXX_FLAGS "$ -std=c++11") //Output the currently compiled static library or dynamic library to the lib subdirectory of the current project folder set(LIBRARY_OUTPUT_PATH $/lib) //Set the directory of executable binaries set( EXECUTABLE_OUTPUT_PATH $/bin) //Set the value of a variable and replace multiple variables with one variable set(SRC main.cpp test.cpp)

5. Print message

//Output normal message(STATUS "build type:") //Output warning message(WARNING "Enter cmake $") //Output error message(FATAL_ERROR "Opencv Not Found!")

6. Common variables

Variable namemeaningPROJECT_NAMEThe project name written in the project commandCMAKE_VERSIONVersion of CMake currently in useCMAKE_SOURCE_DIRThe top-level directory of the project, that is, the path where the entry CMakeLists file is locatedPROJECT_SOURCE_DIRThe top-level directory of the project, that is, the path where the entry CMakeLists file is locatedCMAKE_BINARY_DIRThe directory where the project compilation occurs, that is, the directory where the cmake command is executed to configure the project, is generally buildPROJECT_BINARY_DIRSame as CMAKE_BINARY_DIRCMAKE_CURRENT_SOURCE_DIRThe path of the currently processed CMakeLists.txtCMAKE_INSTALL_PREFIXSpecify the package installation path when the make install command is executedLIBRARY_OUTPUT_PATHRedefine the storage location of the target link library fileEXECUTABLE_OUTPUT_PATHRedefine the storage location of the target binary executableCMAKE_MODULE_PATHDefines the path where the cmake module (XXX.cmake) is located

7. Switch options

Variable namemeaningCMAKE_BUILD_TYPECompile options, Release or Debug, such as set(CMAKE_BUILD_TYPE "Release")CMAKE_CXX_FLAGSSet C++11 compilation, set(CMAKE_CXX_FLAGS "$ -std=c++11")CMAKE_C_FLAGSSet C compilation optionsCMAKE_CXX_COMPILERSpecify C compilerCMAKE_C_COMPILERSpecifying the C + + compilerBUILD_SHARED_LIBSControls the default library compilation method. If not set, use ADD_LIBRARY does not specify the library type. By default, the libraries generated by compilation are static libraries. If set(BUILD_SHARED_LIBS ON), the libraries generated by default are dynamic libraries

8. Link external libraries

To use a third-party library in CMakeLists.txt, you need to know the header file, lib and dll.

find_ The function of package is to find the header file location, library file location and library file name of the library, set them as variables, and return them to other parts of CMakeLists.txt for use.

find_ The signature of the pacage () method is as follows:

find_package(<package> [version] [EXACT] [QUIET] [MODULE] [REQUIRED] [[COMPONENTS] [components...]] [OPTIONAL_COMPONENTS components...] [NO_POLICY_SCOPE])

The quit parameter refers to masking the output of message() when a lookup error occurs. The REQUIRED parameter indicates that the process is terminated when the package cannot be found.

Now use XXX to represent the package name to be searched

find_ Package (XXX required) sets a series of variables.

XXX_FOUND Represents whether the library was found successfully XXX_INCLUDE_DIRS Represents the path to the header file XXX_LIBRARIES Represents the path to the library file

There are two modes to search the Library:

  • Module mode: searching cmake_ MODULE_ The FindXXX.cmake file in the path specified by path, execute the file to find the XXX library. Specifically, find the library and give it to XXX_INCLUDE_DIRS and XXX_ The assignment of the two variables of libraries is completed by the FindXXX.cmake module.

Take opencv as an example, and the lib needs to be set in the environment variable

//Find opencv find_package(OpenCV REQUIRED)//(name and minimum version of package) find the package that needs library and header files later if (OpenCV_FOUND) message(STATUS "OpenCV library status:") message(STATUS " version: $") message(STATUS " include path: $" \n) #include headers include_directories($) #target generation add_executable(OpenCV_test main.cpp) #link libraries target_link_libraries(OpenCV_test $) else () message(FATAL_ERROR "Could not locate OpenCV" \n) endif()
  • Config mode: search XXX_ The XXXConfig.cmake or XXX-config.cmake file in the path specified by dir. Execute the file to find the XXX library. Specifically, find the library and give it to XXX_INCLUDE_DIRS and XXX_ The assignment of two variables of libraries is completed by XXXConfig.cmake module.
1.FA Yi set(libtest_DIR E:/test/3rdparty) find_package(libtest_DIR REQUIRED) 2.Method II //Find 3rdparty/libtest find_package(libtest REQUIRED HINTS "$/3rdparty") if(NOT libtest_FOUND) message(FATAL_ERROR "libtest Not Found!") endif() include_directories($) target_link_libraries(main "$/libtest.lib")

Create a libtestConfig.cmake file in the 3rdparty/libtest folder, as follows:

include(FindPackageHandleStandardArgs)

9. Examples

9.1 single cpp

cmake_minimum_required(VERSION 3.6) # Name the project project(LEETCODE) if(MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif() add_executable(LeetCode src/LeetCode.cpp) message($) message($) # Install the executeable program in the bin folder of the # current project directory. install(TARGETS LeetCode DESTINATION $/bin)

9.2 multiple CPPs and external libraries

cmake_minimum_required(VERSION 3.6) # Name the project project(test) set(CMAKE_CXX_FLAGS "$ -std=c++11") if(MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif() find_package(add REQUIRED HINTS "$/3rdparty") if(NOT add_FOUND) message(FATAL_ERROR "lib add Not Found") endif() include_directories($/include $) #add_executable(test src/main.cpp src/swap/swap.cpp) #Traverse all files in the directory #file(GLOB_RECURSE SRC_LIST "*.cpp" "*.c") #message(STATUS $) add_executable(test src/main.cpp src/swap/swap.cpp) target_link_libraries(test $/libadd.lib) install(TARGETS test DESTINATION $/bin)
//libswap generates a dll at compile time cmake_minimum_required(VERSION 3.6) # Name the project project(test) set(CMAKE_CXX_FLAGS "$ -std=c++11") if(MSVC) add_definitions(-D_CRT_SECURE_NO_WARNINGS) endif() find_package(add REQUIRED HINTS "$/3rdparty") if(NOT add_FOUND) message(FATAL_ERROR "lib add Not Found") endif() include_directories($/include $) add_subdirectory(./src/swap) add_executable(test src/main.cpp) #libswap generated by win is in the lib/Debug folder, and the Release mode is in the Release folder target_link_libraries(test $/libadd.lib $/lib/Debug/libswap.lib) install(TARGETS test DESTINATION $/bin)
//CMakeLists.txt under swap file aux_source_directory(. SUB_SRC) add_library(libswap SHARED $) SET(LIBRARY_OUTPUT_PATH $/lib) message(STATUS $) #install(TARGETS test DESTINATION $/bin)

//main.cpp #include "swap.h" #include "add.h" #include <string> int main() { CTest obj(2,8); obj.swap(); dllTest::CAdd add; std::cout<<"dll sum = "<< add.sum(1, 6); dllTest::show(); }
#pragma once #ifdef libswap_EXPORTS #ifdef __GNUC__ #ifndef __linux__ #define DLL_TEST_API __attribute__((dllexport)) #else #define DLL_TEST_API __attribute__((visibility("default"))) #endif #else #define DLL_TEST_API __declspec(dllexport) #endif #else #ifdef __GNUC__ #ifndef __linux__ #define DLL_TEST_API __attribute__((dllimport)) #else #define DLL_TEST_API __attribute__((visibility("default"))) #endif #else #define DLL_TEST_API #endif #endif #include <iostream> class DLL_TEST_API CTest { public: CTest(int a, int b) { this->a = a; this->b = b; } void swap(); private: int a; int b; }; //swap.cpp #include "swap.h" void CTest::swap() { std::cout<<"before:"<<std::endl; std::cout << "a = " << a << std::endl; std::cout << "b = " << b << std::endl; int temp; temp = a; a = b; b = temp; std::cout<<"after:"<<std::endl; std::cout << "a = " << a << std::endl; std::cout << "b = " << b << std::endl; }

19 September 2021, 13:27 | Views: 2064

Add new comment

For adding a comment, please log in
or create account

0 comments