xjq233p_1
xjq233p_1

Reputation: 8070

Accessing Makefile variables in code?

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

Upvotes: 0

Views: 960

Answers (2)

polarysekt
polarysekt

Reputation: 572

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.

Upvotes: 1

Rob K
Rob K

Reputation: 8926

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.

Upvotes: 1

Related Questions