c++ - 使用 SCons 定义 C++ 预处理器宏

标签 c++ macros scons

我正在尝试在 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')

ma​​in.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/

相关文章:

c++ - qt creator 项目更改目录

c++ - 无法将 »boost::gregorian::date_duration« 转换为 »int

macros - 如何在其他宏的范围内扩展宏(尝试调试宏)

python - scons:在 scons 之前运行一些脚本

c++ - 如何在 C++ 中绕过 << 调用,就好像 "#ifndef DEBUG"宏一样?

c++ - 名称不同但参数相同的函数

c++ - 如何告诉 scons 使用 C++11 标准

C++ 图形和音频库

c++ - 测量 C++ OpenMP 代码中的执行时间

c++ - 遍历 2D 矩阵的可并行算法,同时了解 col/row-wise 邻域