要解决您的问题,请删除所有预处理器
有关BASE_OPERATION_N
的指令并重写源文件,如下所示:
//operations.h
#ifndef OPERATIONS_H
#define OPERATIONS_H
int op_1(int a, int b);
int op_2(int a, int b);
#endif
//base/operations.c
#include <operations.h>
#ifndef CUSTOM_OP_1
int op_1(int a, int b)
{
return a + b;
}
#endif
#ifndef CUSTOM_OP_2
int op_2(int a, int b)
{
return 2 * (a + b);
}
#endif
// custom/operations.c
#ifdef CUSTOM_OP_2
int op_2(int a, int b)
{
return 3 * (a + b);
}
#endif
这样,-DCUSTOM_OP_2
就足以禁用进制op_2
和
启用自定义op_2
的编辑.相反,缺席-DCUSTOM_OP_2
足以禁用自定义op_2
的编译并启用编译
op_2
基数的.
这是一个演示.我的CMake项目是:
$ tree
.
├── base
│ └── operations.c
├── CMakeLists.txt
├── config.cmake
├── custom
│ └── operations.c
├── operations.h
└── test.c
cmake.config
是我的工具链文件.它不需要存在
默认构建.我正在构建一个测试程序和一个库来证明这是有效的.为
同样的目的,我在来源中添加了一些printf
:
$ cat base/operations.c
//base/operations.c
#include <stdio.h>
#include <operations.h>
#ifndef CUSTOM_OP_1
int op_1(int a, int b)
{
printf("%s_%s\n","Base",__func__);
return a + b;
}
#endif
#ifndef CUSTOM_OP_2
int op_2(int a, int b)
{
printf("%s_%s\n","Base",__func__);
return 2 * (a + b);
}
#endif
$ cat custom/operations.c
// custom/operations.c
#include <stdio.h>
#ifdef CUSTOM_OP_1
int op_1(int a, int b)
{
printf("%s_%s\n","Custom",__func__);
return 3 * (a + b);
}
#endif
#ifdef CUSTOM_OP_2
int op_2(int a, int b)
{
printf("%s_%s\n","Custom",__func__);
return 3 * (a + b);
}
#endif
我的CMakeLists.txt
是:
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(operations)
set(BASE_SOURCES ./base/operations.c)
set(SOURCES ${BASE_SOURCES} ${CUSTOM_SOURCES})
include_directories(./)
add_library(
operations
${SOURCES}
)
add_executable(
test
test.c)
target_link_libraries(test operations)
默认构建为:
$ mkdir build
$ cd build
$ cmake ..
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.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 (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/imk/develop/so/cmake_prob/build
$ make
[ 25%] Building C object CMakeFiles/operations.dir/base/operations.c.o
[ 50%] Linking C static library liboperations.a
[ 50%] Built target operations
[ 75%] Building C object CMakeFiles/test.dir/test.c.o
[100%] Linking C executable test
[100%] Built target test
该程序运行:
$ ./test
Base_op_1
Base_op_2
现在这是我的工具链文件v.1:
$ cd ..
$ cat config.cmake
set(CUSTOM_SOURCES ./custom/operations.c)
add_compile_options(-DCUSTOM_OP_2)
我正在禁用Base op_2
,启用自定义op_2
激活并重新生成构建系统:
$ cd build
$ rm -fr *
$ cmake -DCMAKE_TOOLCHAIN_FILE=config.cmake ..
-- The C compiler identification is GNU 13.2.0
-- The CXX compiler identification is GNU 13.2.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 (0.2s)
-- Generating done (0.0s)
-- Build files have been written to: /home/imk/develop/so/cmake_prob/build
重建和测试:
$ make && ./test
[ 20%] Building C object CMakeFiles/operations.dir/base/operations.c.o
[ 40%] Building C object CMakeFiles/operations.dir/custom/operations.c.o
[ 60%] Linking C static library liboperations.a
[ 60%] Built target operations
[ 80%] Building C object CMakeFiles/test.dir/test.c.o
[100%] Linking C executable test
[100%] Built target test
Base_op_1
Custom_op_2
有定制op_2
.
现在我想恢复到op_2
进制并启用自定义op_1
.其工具链文件为v.2:
$ cd ..
$ cat config.cmake
set(CUSTOM_SOURCES ./custom/operations.c)
add_compile_options(-DCUSTOM_OP_1)
现在不需要重新生成构建系统.我可以做:
$ cd build
$ make
-- Configuring done (0.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/imk/develop/so/cmake_prob/build
[ 20%] Building C object CMakeFiles/operations.dir/base/operations.c.o
[ 40%] Building C object CMakeFiles/operations.dir/custom/operations.c.o
[ 60%] Linking C static library liboperations.a
[ 60%] Built target operations
[ 80%] Building C object CMakeFiles/test.dir/test.c.o
[100%] Linking C executable test
[100%] Built target test
$ ./test
Custom_op_1
Base_op_2
有定制op_1
.