gccg++shared-librariesobject-files

g++ -c vs. g++ -shared difference and when to use which?


I am just really wondering, what is difference between g++ -c and g++ -shared? If I understand it correctly, it has something to do with the linking. So what does the -shared flag introduce that is not there without it. I did read the documentation, but I am afraid I did not quite understand. It is probably because I do not have enough experience with these technical terms. So could maybe someone explain it simply?

And most importantly, when to use which ?

thanks in advance!

I tried both running g++ -c and g++ -shared and in both cases the files compiled


Solution

  • See https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#index-c and https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-shared

    In short - they're unrelated. -c tells GCC to compile but not link the file in question, while -shared tells it at link time to build a shared library.

    EDIT: wrt. when to use which: you'd use -c to compile multiple files to later link them together, and you'd use -shared when linking those files to combine them into a shared library.