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

怎甘沉沦 提交于 2019-12-08 09:32:29

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!

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.)

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!