C++ 如何打印预定义的CFLAG

C++ 如何打印预定义的CFLAG,c++,printf,cflags,C++,Printf,Cflags,在Unix环境中,我有一个简单的cpp文件,如下所示: #include <stdio.h> #ifndef HELLO #define HELLO "hello" #endif int main() { printf("HELLO = %s \n", HELLO); return 0; } 当我运行程序时,我得到一个分段错误 如何在代码中打印-DHELLO?-DHELLO相当于#定义HELLO 1,这解释了编译错误,它抱怨int,而不是char*

在Unix环境中,我有一个简单的cpp文件,如下所示:

#include <stdio.h>
#ifndef HELLO
    #define HELLO    "hello"
#endif

int main()
{
    printf("HELLO = %s \n", HELLO);
    return 0;
}
当我运行程序时,我得到一个分段错误


如何在代码中打印
-DHELLO

-DHELLO
相当于
#定义HELLO 1
,这解释了编译错误,它抱怨
int
,而不是
char*

试着这样编译:

g++ -Wall -g -DHELLO="hello"

-DHELLO
相当于
#define HELLO 1
,因此您得到的代码不正确。您需要执行
'-DHELLO=“hello”
(您可以通过直接调用预处理器
cpp
echo'hello'| cpp-DHELLO
来检查这一点)
g++ -Wall -g -DHELLO="hello"