使用 SCon 定义C++预处理器宏

Defining C++ Preprocessor macros with SCons

本文关键字:处理器 预处理 C++ SCon 定义 使用      更新时间:2023-10-16

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

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

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

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

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

像这样定义 Scons 中的常量

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

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

编辑:修正错别字

我能够在 Linux 上使用 g++ 执行此操作,如下所示:

SConscript

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