Accessing Makefile variables in code?

懵懂的女人 提交于 2019-12-13 05:47:46

问题


UPDATED PROGRESS

I am sorry I forgot to specify this question as an Arduino question. 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

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

回答1:


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.




回答2:


According to the literature at Arduino's Site:

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


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



来源:https://stackoverflow.com/questions/24963654/accessing-makefile-variables-in-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!