版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1338356
至于问题1:
我们可能想到直接在头文件中定义,这样也可以,但是如果直接在cmake中定义,然后传递给程序读取生成更加灵活。
至于问题2:
可以在cmake中通过option函数
以及add_definitions函数
来实现。
首先定义demo11Config.h.in头文件接受CMakeLists.txt传递来的版本号参数,文件内容如下:
// the configured options and settings for demo11
#define demo11_VERSION_MAJOR @demo11_VERSION_MAJOR@
#define demo11_VERSION_MINOR @demo11_VERSION_MINOR@
demo11.cpp代码如下:
#include <stdio.h>
// demoConfig.h文件是cmake生成的
#include "demoConfig.h"
int main(int argc, char* argv[]){
printf("%s version: %d.%d \n", argv[0], demo11_VERSION_MAJOR, demo11_VERSION_MINOR);
}
注意的是demoConfig.h是生成的在项目结构中并不存在,项目目录如下:
CMakeLists.txt
如下:
cmake_minimum_required(VERSION 3.5)
project(demo11)
# The version number
set(demo11_VERSION_MAJOR 1)
set(demo11_VERSION_MINOR 0)
# configure a header file to pass some of the CMake settings to
# source code(demo11Config.h.in), the file demoConfig.h does't exists
configure_file(
"${PROJECT_SOURCE_DIR}/demo11Config.h.in"
"${PROJECT_BINARY_DIR}/demoConfig.h"
)
# add the binary tree to the search path for include files
# so that we will find demoConfig.h
include_directories("${PROJECT_BINARY_DIR}")
# add the exectuable
add_executable(demo11 demo11.cpp)
cmake 编译如下:
root@xy:~/cmake_practice/cmake_build/build_demo11# cmake ../../cmake_tuorial/demo11/
-- The C compiler identification is GNU 5.4.0
可以看到生产了demoConfig.h:
drwxr-xr-x 3 root root 4096 Jun 29 08:42 ./
........
-rw-r--r-- 1 root root 112 Jun 29 08:42 demoConfig.h
-rw-r--r-- 1 root root 4865 Jun 29 08:42 Makefile
demoConfig.h内容如下:
// the configured options and settings for demo11
#define demo11_VERSION_MAJOR 1
#define demo11_VERSION_MINOR 0
make & run,输出如下:
root@xy:~/cmake_practice/cmake_build/build_demo11# ./demo11
./demo11 version: 1.0
本实例在cmake中添加macro定义到源文件中,从而选择使用不同的lib 模块函数.
项目目录如下:demo12.cpp根据cmake定义的macro USE_SPECIFIED决定使用模块SpecifiedFun(特定的函数),还是模块CommonFun(一般的函数).
两个模块都只有一个函数showSelectedFun,主要是输出调用了哪个函数.
模块CommonFun,内容如下:
void showSelectedFun(){
std::cout<<"Select to use common function"<<std::endl;
}
对应的cmake如下:
add_library(common_fun CommonFun.cpp)
模块SpecifiedFun,内容如下:
void showSelectedFun(){
std::cout<<"Select to use specified function"<<std::endl;
}
对应的cmake如下:
add_library(specified_fun SpecifiedFun.cpp)
主CMakeLists.txt内容如下, 主要是通过option USE_SPECIFIED ON/OFF
来决定使用添加macro add_definitions(-DUSE_SPECIFIED)
:
cmake_minimum_required(VERSION 3.5)
# should we use specified function
# if not set, then will use common function
option(USE_SPECIFIED "select to use specified function" ON)
# select to use specified function, or common function
message("USE_SPECIFIED is ${USE_SPECIFIED}")
if(USE_SPECIFIED)
add_definitions(-DUSE_SPECIFIED)
include_directories("${PROJECT_SOURCE_DIR}/SpecifiedFun")
add_subdirectory(SpecifiedFun)
set(EXTRA_LIBS ${EXTRA_LIBS} specified_fun)
message("select to use specified function, lib is: ${EXTRA_LIBS}")
else()
include_directories("${PROJECT_SOURCE_DIR}/CommonFun")
add_subdirectory(CommonFun)
set(EXTRA_LIBS ${EXTRA_LIBS} common_fun)
message("select to use common function, lib is: ${EXTRA_LIBS}")
endif(USE_SPECIFIED)
add_executable(demo12 demo12.cpp)
target_link_libraries(demo12 ${EXTRA_LIBS})
首先编译不使用USE_SPECIFIED
即 USE_SPECIFIED=OFF
root@xy:~/tmp/demo12/build# cmake -DUSE_SPECIFIED=OFF ..
..........
USE_SPECIFIED is OFF
select to use common function, lib is: common_fun
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xy/tmp/demo12/build
编译make:
root@xy:~/tmp/demo12/build# make
Scanning dependencies of target common_fun
[ 25%] Building CXX object CommonFun/CMakeFiles/common_fun.dir/CommonFun.cpp.o
[ 50%] Linking CXX static library libcommon_fun.a
[ 50%] Built target common_fun
Scanning dependencies of target demo12
[ 75%] Building CXX object CMakeFiles/demo12.dir/demo12.cpp.o
[100%] Linking CXX executable demo12
[100%] Built target demo12
运行:
root@xy:~/tmp/demo12/build# ./demo12
Select to use common function
使用macro USE_SPECIFIED直接使用cmake .. 即可
接下来的步骤一样就不重复了