Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile a .cpp file just to object file without calling linker

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 compile I end up with the object file going to the same directory as the source, and even worse it goes on and links the object file to the executable when it is supposedly doing a compile.

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.

Edit1

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

like image 287
Peter Nimmo Avatar asked May 22 '13 17:05

Peter Nimmo


2 Answers

Use the -c option.

-c

Run all of the above, plus the assembler, generating a target ".o" object file.

like image 55
Collin Avatar answered Oct 17 '22 18:10

Collin


g++ -c *.cpp it compiles .cpp into .o

like image 43
dcnocomment Avatar answered Oct 17 '22 17:10

dcnocomment