5

我正在尝试在 Scons 中定义一个预处理器宏来构建一个更大的 C/C++ 项目。

我正在使用的库之一需要定义 ALIGN。更具体地说,如果我添加

#define ALIGN(x) __attribute((aligned(x)))

到所述库的头文件,它编译得很好。但是,我应该能够在构建时指定它,因为这是库打算使用的方式。我知道在 CMake 中,我可以使用类似的东西来定义宏

SET(ALIGN_DECL "__attribute__((aligned(x)))") 

像这样在 Scons 中定义常量

myEnv.Append(CPPDEFINES = ['IAMADEFINEDCONSTANT']) 

工作正常,但以这种方式定义不起作用。是什么赋予了?

编辑:修正错字

4

1 回答 1

8

我能够在 Linux 上使用 g++ 进行如下操作:

征兵

env = Environment()
env.Append(CPPDEFINES=['MAX(x,y)=(x>y ? x:y)'])
env.Program(target = 'main', source = '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
于 2013-10-11T12:06:38.813 回答