将 #define X 宏值替换为编译命令中指定的另一个值 [英] Replace #define X macro value with another one specified in compilation command

查看:9
本文介绍了将 #define X 宏值替换为编译命令中指定的另一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下代码:

#include <stdio.h>

#ifndef STR
    #define STR "HELLO"
#endif

int main() {
    printf(STR "WORLD 
");
    return 0;
}

表示:如果未定义STR,则将其定义为HELLO",所以输出将是

which says: if STR was not defined, then define it to be "HELLO", so the output will be

HELLO WORLD

使用gcc编译时如何修改STR的值?

How can I modify the value of STR when compiling using gcc?

我试过了

gcc -Wall program.c -DSTR="HI" -o program

但它没有产生预期的输出.

but it didn't produce the expected output.

推荐答案

尝试以下形式:

-DSTR="MyString"

这篇关于将 #define X 宏值替换为编译命令中指定的另一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆