Reputation: 1115
I am trying to compile a single .cpp file separately from the build process.
If I do a full build, then the compile step outputs to the directory configured when creating the project.
However, if I just ask for a compilation, I end up with the object file going to the same directory as the source. Even worse, it goes on and links the object file to the executable when it is supposedly doing a compilation.
Note I am compiling with clang++ for C++11, but I don't think that has any barring on why it is calling Clang++.exe a second time for linking which has not been requested.
When building, it does this:
-------------- Build: Debug in GOTW (compiler: GNU GCC Compiler)---------------
clang++.exe -Wall -fexceptions -g -std=c++11 -stdlib=libstdc++ -c
C:\Work\test\2010\C++11\CLang\GOTW\gotw.cpp -o obj\Debug\GOTW.o
clang++.exe -o bin\Debug\GOTW.exe obj\Debug\GOTW.o
Output size is 203.50 KB
Process terminated with status 0 (0 minutes, 11 seconds)
0 errors, 6 warnings (0 minutes, 11 seconds)
Yet when Compile Current File
is performed, it does this:
clang++.exe -std=c++11 -stdlib=libstdc++ -c GOTW.cpp -o GOTW.o
1 warning generated.
clang++.exe -o GOTW.exe GOTW.o
I don't understand why it is outputting the second step, and also how to get it to use the obj and bin directories like the build does.
Upvotes: 28
Views: 32016
Reputation: 534
You can use the -c
option to compile a source file into an object file without linking, like this:
g++ -c main.cpp -o main.o
As stated in the GCC documentation:
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
If you want only to preprocess the source file (without compiling it), you can use the -E
option:
g++ -E main.cpp -o main.i
As stated in the GCC documentation:
-E
Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.
Upvotes: 0
Reputation: 12287
-c
Run all of the above, plus the assembler, generating a target ".o" object file.
Upvotes: 32