简体   繁体   English

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

[英]Defining C++ Preprocessor macros with SCons

I'm trying to define a preprocessor macro within Scons for building a larger C/C++ project. 我正在尝试在Scons中定义一个预处理器宏来构建一个更大的C / C ++项目。

One of the libraries I'm using needs ALIGN defined. 我正在使用的其中一个库需要ALIGN定义。 To be more specific, if I add 更具体地说,如果我添加

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

to the header file of said library, it compiles fine. 对于所述库的头文件,它编译得很好。 However, I should be able to specify this at build time, as this is how the library intends on being used. 但是,我应该能够在构建时指定它,因为这是库打算如何使用。 I know in CMake, I would be able to define the macro using something like 我知道在CMake中,我可以用类似的东西来定义宏

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

Defining constants in Scons like this 像这样在Scons中定义常量

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

works fine, but definine a macro in this way doesn't work. 工作正常,但以这种方式定义不起作用。 What gives? 是什么赋予了?

Edit: fixed typo 编辑:修正错字

I was able to do it on Linux with g++ as follows: 我能够在Linux上使用g ++进行如下操作:

SConscript SConscript

env = Environment()
env.Append(CPPDEFINES=['MAX(x,y)=(x>y ? x:y)'])
env.Program(target = 'main', source = 'main.cc')

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;
}

Compilation 汇编

$ 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.

Execution 执行

./main
Max is 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM