October 24, 2024
Chicago 12, Melborne City, USA
C#

When is 'gcc -c' not enough to compile into a binary but needs 'gcc -o'?


I used to compile C programs with a command line like this:

gcc -c myexecutable mysource.c

After that I could run ‘myexecutable’ from shell. In the past this always worked for me.

With a tool that I am currently trying to compile, this does not work.

It says:

gcc: error: myexecutable: No such file or directory

When I compile it using
gcc -o myexecutable mysource.c

it works and I can run myexecutable.

When I compile it using
gcc -c mysource.c

it creates the mysource.o object file which is not executable.

I read that the ‘-c’ option does not link and probably in earlier projects I did not have to link.

My questions are:

  • In which situations is it needed to link?
  • How can I check the source file for these attributes?
  • Was it just coincidence that the programs before did not need to be linked , so I did not have this problem earlier, or was it not needed in earlier gcc version (2.95 or so)?
  • Should I compile with ‘-o’ from now on in general?

I only have one single source file and no dependencies from external files or libraries.

The environment has gcc-9 if that is relevant.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video