I want to only compile not compile and link

With writing this: g++ file.cpp -o file.exe
I found myself with an .exe, and I assume that this is compilation+linkage.

1: Am I wrong/right on this one?
2: How to only compile using g++ compiler?
g++ -std=c++20 -O3 -Wall -Wextra -pedantic-errors -c file.cpp

-c
Compile or assemble the source files, but do not link. The linking stage simply is not done.

https://gcc.gnu.org/onlinedocs/gcc/Overall-Options.html#Overall-Options

Thank you very much @JLBorges, you solved my problem

One last thing if you do not mind, is there a way to set the file.o destination say for example to d:/ drive ?

also I want to change the file name

I did g++ -c main.cpp jj.o and I got error and a warning

warning: jj.o: linker input file unused because linking not done
Error: jj.o: linker input file not found: no such file or directory
Last edited on
Use the -o flag.

g++ -c main.cpp -o jj.o

Note: When linking you can use the same flag to decide the name of the executable file.
I see I did g++ main.cpp -o jj.o, but it gave a jj.o file with a size equal to the one jj.exe, so I said maybe there are different same

Now it gives me a different size, thank you very much @Peter)))))
Last edited on
Note: When linking you can use the same flag to decide the name of the executable file.
you mean like:
g++ -c main.cpp -o jj.exe

or

g++ -c jj.o -o jj.exe

One other thing, is the order here matters, that is

is g++ -c main.cpp -o jj.o same as g++ -c -o main.cpp jj.o
Last edited on
I mean when you don't use the -c flag (which is when it will link and produce an executable file ).

In one step:
g++ file.cpp -o file.exe

In two steps:
g++ -c file.cpp -o file.o
g++ file.o -o file.exe

The order is only important with -o and the filename that comes after. All the other arguments that has been mentioned in this thread can be in any order.

Note that if you have multiple .cpp files you can either compile all files in one go:
g++ file1.cpp file3.cpp file3.cpp -o MyProgram.exe

Or you could compile one file at a time and link everything afterwards:
g++ -c file1.cpp -o file1.o
g++ -c file2.cpp -o file2.o
g++ -c file3.cpp -o file3.o
g++ -o MyProgram.exe file1.o file2.o file3.o

Compiling each file separately has the advantage that you don't have to recompile all files when you only have made changes to some of them. This is very important for larger programs which can take minutes, sometimes hours, to compile from scratch. It's normally too difficult and error-prone to do this manually but it's essentially what IDEs and many Makefiles do.

Note that you can often use "wildcards" to simplify these commands (assuming you want to list all files with the same file extension in the same directory):
g++ *.cpp -o MyProgram.exe
Last edited on
Amazing explanation, case closed))

Thank you very much @Peter)
There are flags you can use at every step of the compilation process to stop after that step.

https://riptutorial.com/cplusplus/example/26378/the-cplusplus-compilation-process

Want to stop after preprocessing? -E

Want to stop after compilation? -S (human readable code vs. machine code when using -o)

Want to stop after assembling? -c

Please note the case of the flags does matter.
Thank you @George
Topic archived. No new replies allowed.