gpt4 book ai didi

c++ - 根据 cmake 目标启用宏

转载 作者:行者123 更新时间:2023-11-30 02:57:04 24 4
gpt4 key购买 nike

只有当相应的源文件被特定的 cmake 目标使用时,我才尝试启用 C 宏。假设我有以下设置:

tests/test.cpp
src/code.cpp
include/code.hpp
CMakeList.txt

代码.hpp

class MyClass
{
public:
void normal_stuff();
#ifdef TEST
int debug;
void _dangerous_function()
{
debug++;
}
#endif
}

代码.cpp

#include "code.hpp"

MyClass::normal_stuff()
{
// boring code
}

测试.cpp

#include "code.hpp"

void some_test()
{
MyClass foo;
foo._dangerous_function();
}

CMakeList.txt

project(foo)
include_directories(include)
file(GLOB_RECURSE foo_source src/*.cpp)
file(GLOB_RECURSE test_source test/*.cpp)
add_executeable(foo ${foo_source})
add_executeable(test ${test_source} ${foo_source})

我只想在为测试目标构建 code.cpp 时设置 TEST,而不是为 foo 目标构建。当然,我可以在包含 code.hpp 之前在 test.cpp 中编写“#define TEST”,但是 code.cpp 将具有与 test.cpp 不同的 MyClass View 。

有人知道我该怎么做吗?我知道,我不应该这样做,但我想知道我是否能让它运行。

最佳答案

您可以设置目标特定属性来添加这样的定义:

set_target_properties(test PROPERTIES COMPILE_DEFINITIONS "TEST")

阅读更多 herehere

关于c++ - 根据 cmake 目标启用宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14917188/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com