CMake设置编译选项 发表于 2021-10-14 更新于 2024-03-29 分类于 cmake CMake中设置编译选项1234567891011option(BUILD_VISUALIZATIONS "Build visualization, default OFF" OFF)message("BUILD_VISUALIZATIONS is ${BUILD_VISUALIZATIONS}")# 可以跟build type关联# if(CMAKE_BUILD_TYPE MATCHES Debug OR CMAKE_BUILD_TYPE MATCHES DEBUG)if(BUILD_VISUALIZATIONS)add_definitions(-DBUILD_VISUALIZATIONS)# target_compile_definitions(${project_name} PRIVATE BUILD_VISUALIZATIONS)endif() 代码中作为宏12345678910111213141516#ifdef BUILD_VISUALIZATIONSvoid visualize_cloud(const cloud_ptr_t& cloud_ptr){std::cout << "visualizations on" << std::endl;// visualize cloud}#elsetemplate <typename... Args>void visualize_cloud(Args...) {std::cout << "pass visualizations" << std::endl;}#endif 编译时传入参数1--cmake-args -DBUILD_VISUALIZATIONS=ON