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

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
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源代码杀手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值