How to convert value #define'd in a macro to a char* (c++) -


in .cpp, want output file directory created @ compile time (determined time of compilation). have passed value via -dcompiletime=$(stuff time) in makefile. pass value stored in compiletime sprintf can create filepath string use place output file.

i have tried:

#define str(x) #x sprintf(filepath,"\"%s\file\"",str(compiletime)); 

as as

#define str(x) #x #define strname(name) str(name) sprintf(filepath,"\"%s\file\"",strname(compiletime)); 

but ever get

"compiletime/file" 

as output.

your macros fine. here's test program:

#include <stdio.h>  #define str(x) #x #define strname(name) str(name)  int main() {    printf("\"%s/file\"\n",strname(compiletime));    return 0; } 

build command:

cc -wall -o soc soc.c 

output:

"compiletime/file" 

build command:

cc -wall -o soc soc.c -dcompiletime=abcd 

output:

"abcd/file" 

tested under gcc 4.9.2.

the problem facing fopen related to:

sprintf(filepath,"\"%s\file\"",strname(compiletime));                       ^^^^ 

make that

sprintf(filepath,"\"%s\\file\"",strname(compiletime));                       ^^^^ 

otherwise, escaping character f, nothing. should able use forward slash instead of backward slash.

sprintf(filepath,"\"%s/file\"",strname(compiletime));                       ^^^^ 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -