Search code examples
cbuildcompilation

Is it correct to compile source file with object files together?


Imagine I have file1.c, file2.c. First I compile one file with gcc -c file1.c -o file1.o. Is it OK to compile them together with gcc file1.o file2.c -o prog? I tried it and no errors are shown, but should I compile file2.o first? Is it correct to mix .c and .o files?


Solution

  • Yes, you can do gcc file1.o file2.c -o prog. You can also do gcc file1.c file2.c -o prog.

    GCC will handle compiling file2.c behind the scenes.