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

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

问题描述

假设我有这样的code:

Suppose I have this code:

#include<stdio.h>

#ifndef STR
  #define STR "HELLO"
#endif

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

这基本上转化为:如果STR没有定义,则其定义为HELLO,
所以程序的输出将

Which basically translates to: if STR was not defined, then define it to be "HELLO", so the program output will be

HELLO WORLD

HELLO WORLD

现在,我的问题是:我怎么能使用gcc编译时修改此STR价值?
我想改变HELLO,以HI,例如没有在源(program.c)文件进行任何更改。

Now, my question is: how can I modify this STR value when compiling using gcc? I want to change "HELLO" to "HI", for example, without changing anything in the source (program.c) file.

什么是gcc的语法这样做呢?

What is the gcc syntax to do so?

我试过

GCC -Wall program.c -DSTR =HI-o程序

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

但它并没有产生预期的输出。

but it didn't produce the expected output.

有什么建议?

谢谢!

推荐答案

尝试的形式:

-DSTR=\"MyString\"

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

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