Dynamic variable cpp compilation [duplicate]

筅森魡賤 提交于 2019-12-10 17:22:22

问题


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

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