问题
I wish I could edit a variable in a .h file since compilation Example:
#include <iostream>
#include <stdlib.h>
#define HOST (char *)"http://localhost/"
#define PATH "insert"
I want to edite HOST from compilation like this:
g++ -o output source.cpp -HOST http://mywebsite/
回答1:
You can easily do that with something like this:
#include <iostream>
#include <stdlib.h>
#ifndef HOST
#define HOST (char*)"http://localhost/"
#endif
#define PATH "insert"
Then, on the command-line, you either specify '-DHOST=(char*)"whatever"' (and it will be used), or do not pass in any -DHOST= flag, and the default in the header will be used.
来源:https://stackoverflow.com/questions/50429180/dynamic-variable-cpp-compilation