26

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

Peter Nimmo
  • 1,045
  • 2
  • 12
  • 25
  • Most build systems can output the exact commands they're executing. Try to find the relevant option, get the command and change it appropriately to get the output where you want it. – Fred Foo May 22 '13 at 17:13
  • Codeblocks is outputting the exact commands, its just it is executing more commands than expected, but also it ignores the build settings to do with output directories – Peter Nimmo May 22 '13 at 17:30
  • Strangely if I add a second source file and ask it to compile that it a) stops the linker from being invoked and b) it compiles to the build directories rather than directly where the source is located. – Peter Nimmo May 22 '13 at 18:00
  • I don't know if Codeblocks is trying to be clever and analysing that the main file has a main() entry point, and therefore calling the linker. I will try and swap the location of the main() entry – Peter Nimmo May 22 '13 at 18:01
  • interesting problem, did you notice any difference after swapping the main() entry? – abasu May 22 '13 at 18:08
  • @abasu - it then has linking trouble (even though I don't want it to link) as it can't find wMain. – Peter Nimmo May 22 '13 at 18:14

2 Answers2

29

Use the -c option.

-c

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

Collin
  • 11,977
  • 2
  • 46
  • 60
4

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

dcnocomment
  • 104
  • 6