我正在尝试在 Scons 中定义预处理器宏以构建更大的 C/C++ 项目。
我正在使用的库之一需要定义 ALIGN。更具体地说,如果我添加
#define ALIGN(x) __attribute((aligned(x)))
到所述库的头文件,编译正常。但是,我应该能够在构建时指定它,因为这是库打算使用的方式。我知道在 CMake 中,我可以使用类似
的东西来定义宏SET(ALIGN_DECL "__attribute__((aligned(x)))")
像这样在 Scons 中定义常量
myEnv.Append(CPPDEFINES = ['IAMADEFINEDCONSTANT'])
工作正常,但以这种方式定义一个宏是行不通的。 给了什么?
编辑:修正错别字
最佳答案
我能够在 Linux 上使用 g++ 完成它,如下所示:
征兵
env = Environment()
env.Append(CPPDEFINES=['MAX(x,y)=(x>y ? x:y)'])
env.Program(target = 'main', source = 'main.cc')
main.cc
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
int a = 3;
int b = 5;
// MAX() will be defined at compile time
cout << "Max is " << MAX(a, b) << endl;
}
编译
$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o main.o -c "-DMAX(x,y)=(x>y ? x:y)" main.cc
g++ -o main main.o
scons: done building targets.
执行
./main
Max is 5
关于c++ - 使用 SCons 定义 C++ 预处理器宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19308401/