Compile multiple C source fles into a unique object file

后端 未结 7 1666
再見小時候
再見小時候 2020-12-13 10:48

I have some C source files and I am using gcc. I basically want to compile all of them and create one single object file. When I try:

gcc -c src         


        
7条回答
  •  孤城傲影
    2020-12-13 11:37

    This is not a usual way to proceed, but you can easily achieve what you want by creating a new final.c file with the following content.

    #include "src1.c"
    #include "src2.c"
    #include "src3.c"
    

    Then you can compile it as follows.

    gcc -c final.c -o final.o
    

    Note that there may be issues, read compilation errors, even if each file compiles successfully when compiled separately. This tend to happen especially with macro definitions and includes, when merging your source files into a single one this way.

提交回复
热议问题