简体   繁体   中英

gcc: -DVAR=-linux not working as expected on linux

I have following C program:

test.c

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

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

Compilation

gcc -DVAR=-linux test.c

Running

./a.out

Gives

RUNNING... -1

Asm output

        .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 or -linuxx does not cause such problem, but -linux/ does. Surrounding with quotes -DVAR="-linux" does not help.

Problem is also visible on gcc 6 and 7. On cygwin everythings works FINE. I wonder if it is a bug or I do something wrong.

You can inhibit the definition of system- or gcc-specific macros by using -undef , when compiling your object file:

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

Then your macros should work as you expect. Note however, that you cannot use any of these predefined macros then, you should probably only use it on those object files you absolutely have to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM