Vlad Havriuk
Vlad Havriuk

Reputation: 1451

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?

Upvotes: 4

Views: 708

Answers (1)

Emil Laine
Emil Laine

Reputation: 42838

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.

Upvotes: 5

Related Questions