CMAKE/CMAKEList.txt 学习

1、cmake_minimum_required(VERSION 2.8)

cmake minimum version, if the current version of CMake lower than the specified version, it will stop processing project files and reports an error

2、project(<projectname> [languageName1 languageName2 ...])

projectname: Project name

languageName: Specifies the project can support the language. The project (Hello, C CXX), CXX C ++ that is

3、set( CMAKE_CXX_FLAGS "-std=c++11" )

Add c ++ 11 standard support

4, find_package (the name of the package and minimum version)

Find the back of the package requires libraries and header files

例如find_package(OpenCV 2.4.3 REQUIRED)

5, include_directories ( "path")

such as:

include_directories(

      $ {} PROJECT_SOURCE_DIR

      ${PROJECT_SOURCE_DIR}/include

      $ {} EIGEN3_INCLUDE_DIR)

Where $ {} is a method references

6、set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)

Set path (path generating shared library below), i.e. the generated shared library lib files in the project folder in

7, add_library ($ {PROJECT_NAME} SHARED src / cpp ...... filename)

Create a shared library (the cpp files in the project are to create a shared library file, easy to call through the header file)

This is when you need only cpp, do not need a main function

$ {PROJECT_NAME} is generated library name indicates the resulting shared library file called lib name .so project  

Cmakelists also be specifically written to compile a program without main function to generate a shared library for use by other programs

8、target_link_libraries(${PROJECT_NAME} /usr/lib/i386-linux-gnu/libboost_system.so )

Libraries, the just generated $ {PROJECT_NAME} libraries and other libraries needed to link the

9、set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)

Setting an executable file path

10, add_executable (executable file name to be generated from the project directory to write the main function played by file name)

An executable file, all you want to compile the source file needs listed.

add_executable(hello_world src/hello_world.cpp src/main.cpp)

11, target_link_libraries (executable file name $ {PROJECT_NAME})

This adds the required executable libraries (in general is just generated project library), for example, we used the libm.so (naming rules: lib + name + so), then add the name of the library "m"

12、set(<variable> <value> [[CACHE <type> <docstring> [FORCE]] | PARENT_SCOPE]

)

The CMAKE variable to a given value

SET(CMAKE_BUILD_TYPE Release)

Set to Release compiled way, if there are relatively high requirements of Germany operation mode is recommended to select Release

Such as:

IF(NOT CMAKE_BUILD_TYPE)

        SET(CMAKE_BUILD_TYPE Release)

13、AUX_SOURCE_DIRECTORY(src DIR_SRCS)

Source directory

 

14、cmake_policy

CMake management policy settings

 

 

Create a C ++ project as follows:

$mkdir hellotest

$cd hellotest

$ Mkdir bin # executable file storage path

$ Mkdir lib # resulting .so file storage path

$ Mkdir include # header file storage path

$ Mkdir src # source file storage path

$ Mkdir build # compiler file storage path

$touch CMakeLists.txt

$cd build

$cmake ..

$make

 

 

Little detailed version: https://www.cnblogs.com/52php/p/5681745.html

 

Guess you like

Origin www.cnblogs.com/doggod/p/12610098.html