How can I compile C code that has already been C pre-processed with GCC?

前端 未结 3 485
心在旅途
心在旅途 2021-01-15 16:04

I\'m performing some source processing between C preprocessing and C compilation. At the moment I:

  1. gcc -E file.c > preprocessed_file.c.
相关标签:
3条回答
  • 2021-01-15 16:48

    The warnings about restrict are due to the fact that it is a keyword in C99. So, you have to pre-process and compile your code using the same standard.

    The error about _main is because your file doesn't define main()? Doing the following should work:

    gcc -c -std=c99 bar.c
    

    and it will create bar.o. If your bar.c has a main() defined in it, maybe it is not called bar.c? For example, I created a bar.c with a valid main(), and did:

    gcc -E -std=c99 bar.c >bar.E
    gcc -std=c99 bar.E
    

    and got:

    Undefined symbols:
      "_main", referenced from:
          start in crt1.10.6.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    

    In that case, you need the -x c option:

    gcc -x c -std=c99 bar.E
    

    (Or, as Nikolai mentioned, you need to save the pre-processed file to bar.i.)

    0 讨论(0)
  • 2021-01-15 16:52

    Looks like a typo in the GCC docs - try '-x cpp-output' instead.

    gcc -E helloworld.c > cppout
    gcc -x cpp-output cppout -o hw
    ./hw
    Hello, world!
    
    0 讨论(0)
  • 2021-01-15 16:57

    Save the file with the .i suffix after pre-processing. Gcc man page:

           file.i
               C source code which should not be preprocessed.
    
           file.ii
               C++ source code which should not be preprocessed.
    
    
    0 讨论(0)
提交回复
热议问题