简体   繁体   English

在代码中访问Makefile变量?

[英]Accessing Makefile variables in code?

UPDATED PROGRESS 最新进展

I am sorry I forgot to specify this question as an Arduino question. 对不起,我忘了将此问题指定为Arduino问题。 I just assumed that it's a preprocessor problem which is kind of independent of what platform this is being executed on. 我只是假设这是一个预处理程序问题,与在哪个平台上执行无关。

I am using Arduino-Make and I am trying to pass in USERNAME and PASSWORD 我正在使用Arduino-Make,并且试图传递USERNAME和PASSWORD

BOARD_TAG    = mega2560
CPPFLAGS     = -DUSERNAME="$(USERNAME)" -DPASSWORD="$(PASSWORD)"
include $(ARDMK_DIR)/Arduino.mk

Command line: 命令行:

make USERNAME="HELLO" PASSWORD="WORLD"

Code: 码:

void setup() {
  Serial.begin(9600);

  String auth_raw2(USERNAME : PASSWORD);
  Serial.println(auth_raw2);
}


void loop() {}

I am getting this error: 我收到此错误:

macro.ino:10:29: error: found ‘:’ in nested-name-specifier, expected ‘::’
macro.ino:10:20: error: ‘HELLO’ has not been declared

What you want is 你想要的是

String auth_raw( USERNAME ":" PASSWORD );

That will do the proper literal string concatenation you're looking for. 这将执行您要查找的正确的文字字符串连接。 The compiler will run adjacent string literals together into a single string. 编译器会将相邻的字符串文字一起运行为单个字符串。 So if you have 所以如果你有

char a[] = "The " "quick" " brown " "fox";

it treats it the same as if you wrote 它像对待您一样对待它

char a[] = "The quick brown fox";

I'm not sure about putting " around the values provided on the command line to make. 我不确定要在命令行上提供的值周围加上“”。

According to the literature at Arduino's Site : 根据Arduino网站的文献

Concatenating in a constructor "gives unpredictable results because 'auth_raw' never got an initial value before you started concatenating different data types. For best results, initialize your Strings before you concatenate them." 在构造函数中进行连接“会产生不可预测的结果,因为'auth_raw'在开始连接不同的数据类型之前从未获得初始值。为了获得最佳结果,请在连接字符串之前先对其进行初始化。”


As far as the variables you are passing to your makefile are concerned, the syntax is correct, and the commandline -D to define those variables is as well valid. 就传递给makefile的变量而言,语法是正确的,并且定义这些变量的命令行-D也是有效的。 However, as the documentation points out, you should avoid mixing integers and strings when passing to a constructor, though it would be perfectly valid to construct the String first, and then concatenate the values. 但是,正如文档所指出的那样,在传递给构造函数时,应避免混合使用整数和字符串,尽管先构造String然后再连接值是完全有效的。

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

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