Dynamic variable cpp compilation [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

This question already has an answer here:

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.



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