How to pass a string from a make file into a c++ program

simgineer

It seems on a program I've inherited we can pass in make variables that are numbers but I'd like to pass in a string now. When they are passed in as numbers it is as the following:

CFLAGS += -DBUILD_NUMBER=$(BUILD_NUMBER)

and i can access it as an int like so:

fprintf(stderr, "\n%s v%d.%d.%d.%d\n", APPNAME, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION, BUILD_NUMBER, MYBRANCH);

but I do not seem to be achieving the same with a string.

CFLAGS += -DMYBRANCH=$(BRANCH)

fprintf(stderr, "\n%s v%d.%d.%d.%d.%s\n", APPNAME, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION, BUILD_NUMBER, MYBRANCH);

I get this error with the above:

main.cpp: In function ‘int args_parse_cmd_line(int, char**)’:
<command-line>:0:10: error: ‘FIL’ was not declared in this scope
main.cpp:72:114: note: in expansion of macro ‘MYBRANCH’
 %s\n", APPNAME, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION, BUILD_NUMBER, MYBRANCH);
                                                                            ^~~~~~~~

Can someone give me a tip on what I'm doing wrong? "FIL-1234" is the example of our branch name that I'm trying to add to our build meta data for tractability.

john

This preprocessor trick should work

#define xstr(s) str(s)
#define str(s) #s

fprintf(stderr, "\n%s v%d.%d.%d.%d.%s\n", APPNAME, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION, BUILD_NUMBER, xstr(MYBRANCH));

It's clear from your error that MYBRANCH is being expanded into FIL-1234 when, not unreasonably, the compiler looks for a variable FIL. You need to turn the macro expansion into a string literal and the code above should do that. Although I haven't tested anything.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I make a program in C that will read ad print specific lines from a file?

How can I make a C++ program hide the source file path from the "strings" command?

How to pass an input string from user to a MIPS program

How to pass arguments and redirect stdin from a file to program run in gdb?

How can I make an EXE file from a Python program?

How do you make a c++ program search for a string?

How to store the character '\b' and make it be recognized by the file of c program

c++ Reading doubles from file, but recording if program encounters a string

searching for EXACT string with spaces in . txt file from C program

How do I make syscalls from my C program

How do i run a program from another program and pass data to it via stdin in c or c++?

Pass a string with multiple contiguous spaces as a parameter to a jar file using Windows command prompt called from a java program

How to pass regular expression matching string from a file in awk?

How to pass a string value from resource file to a ConverterParameter?

How to pass xml string from .cs file to javascript function?

How to pass string value from another class file for selenium script

Cgo pass a string to C file

How to start external program and pass in CSV file?

How to make a button in C# Make a text file and add the result of a textbox to it and then show it in the program?

how do i make a python program check if a there is a letter from a string in a string

How to scan the output string values run from another c program?

How to delete a file with 0 bytes, but read "þa" from it in a c program

C - How to pipe to a program that read only from file

How to create a fake device file that accepts input from a C program?

How To Read data from Microsoft Excel File by C++ Program

How to get wifi password from gnome keyring file with a C program?

How C program load data from txt file?

Make file to compile two C program files

Pass values from C program to Assembly language