gpt4 book ai didi

c - gcc: -DVAR=-linux 在 linux 上没有按预期工作

转载 作者:太空狗 更新时间:2023-10-29 12:10:30 25 4
gpt4 key购买 nike

我有以下 C 程序:

测试.c

#include <stdio.h>
#define DEF0(v) #v
#define DEF(v) DEF0(v)

int main()
{
printf("RUNNING... %s\n", DEF(VAR));
}

编译

gcc -DVAR=-linux test.c

运行

./a.out

给予

正在运行...-1

汇编输出

        .file   "test.c"
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "-1"
.LC1:
.string "RUNNING... %s\n"
.text
.p2align 4,,15
.globl main
.type main, @function
main:
.LFB11:
.cfi_startproc
movl $.LC0, %esi
movl $.LC1, %edi
xorl %eax, %eax
jmp printf
.cfi_endproc
.LFE11:
.size main, .-main
.ident "GCC: (GNU) 4.4.7 20120313 (Red Hat 4.4.7-4)"
.section .note.GNU-stack,"",@progbits

-linu-linuxx 不会导致这样的问题,但是 -linux/ 会。用引号包围 -DVAR="-linux" 没有帮助。

问题在 gcc 6 和 7 上也可见。在 cygwin 上一切正常。我想知道这是一个错误还是我做错了什么。

最佳答案

在编译目标文件时,您可以使用 -undef 来禁止系统或 gcc 特定宏的定义:

gcc -undef -c -DVAR=-linux test.c
gcc test.o -o test

然后您的宏应该按您预期的那样工作。但是请注意,您不能使用这些预定义的宏中的任何一个,您应该只在您绝对必须使用的那些目标文件上使用它。

关于c - gcc: -DVAR=-linux 在 linux 上没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46727015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com