zl程序教程

您现在的位置是:首页 >  后端

当前栏目

cmake命令set、option、find_package、target_link_libraries、add_executable使用方法

set方法命令 Find add package CMake option
2023-09-14 09:15:32 时间

cmake下载:https://cmake.org/download/
set:cmake命令之set使用案例 命令行输入函数接口的参数
option:cmake命令之option使用案例
cmake命令之使用静态库(include_directories,link_directories,target_link_directories)
add_library:cmake命令之add_library案例(生成动态库和静态库)
cmake find_package的基本原理与详细示例

CMake使用总结:
https://blog.csdn.net/a740169405/article/details/82755458?187
https://blog.csdn.net/yntcsb/article/details/83333437?spm=1001.2101.3001.6650
Linux下CMake简明教程

案例1:option

test.cpp

#include <iostream>
 
int main() {
#ifdef TEST_DEBUG
    std::cout<<"123------------------> Hello,Test"<<std::endl;
#endif
    std::cout << "456 Hello------------> World!" << std::endl;
    return 0;
}

CMakeLists.tx:

cmake_minimum_required(VERSION 3.13)
project(cmaks)
 
set(CMAKE_CXX_STANDARD 14)
 
option(TEST_DEBUG "option for TEST" OFF)
if(TEST_DEBUG)
    add_definitions(-DTEST_DEBUG)
endif()
add_executable(cmaks test.cpp)

run.sh:

#!/bin/sh
rm -rf build
ls
mkdir build 
cd build
cmake -DTEST_DEBUG=OFF ..
# cmake --build ..
make 
./cmaks 

cd ../
rm -rf build
mkdir build 
cd build
cmake -DTEST_DEBUG=ON ..
make 
./cmaks 

执行结果:bash run.sh

bash run.sh 
CMakeLists.txt  run.sh  test.cpp
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /data/spleeter_cmake/test2020/cmaks/build
[ 50%] Building CXX object CMakeFiles/cmaks.dir/test.cpp.o
[100%] Linking CXX executable cmaks
[100%] Built target cmaks
456 Hello------------> World!
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /data/spleeter_cmake/test2020/cmaks/build
[ 50%] Building CXX object CMakeFiles/cmaks.dir/test.cpp.o
[100%] Linking CXX executable cmaks
[100%] Built target cmaks
123------------------> Hello,Test
456 Hello------------> World!

案例2:list

cmake_minimum_required(VERSION 2.8)
set(mylist aaa)
list(APPEND mylist bbb ccc 9) #追加
# MESSAGE("${mylist}")
list(LENGTH mylist ll)
#insert with index
list(INSERT mylist 1 xxx 3) #插入指定位置1之后的xxx 3
MESSAGE("${mylist}")  

#移除列表中的元素 
list(REMOVE_ITEM mylist 3)
MESSAGE("${mylist}")

aaa;xxx;3;bbb;ccc;9
aaa;xxx;bbb;ccc;9
-- Configuring done
-- Generating done