Generating an object file in a separate directory using g ++ - compiler - C ++

I am using the following code to compile a cpp file into an object file.

g++ -c main.cpp 

Above the code, .o fles is generated in the same directory as main.cpp.

  • Suppose I have a folder called obj and you need to create object files there, how can I write it?
  • How to see compiler switches supported by g ++ and it uses?

Any help would be great

+4
source share
3 answers

Suppose I have a folder called obj and you need to generate object files there, how can I write?

Using:

 g++ -c main.cpp -o obj/main.o 

How to see compiler switches supported by g ++ and it uses?

If you are using a * nix system, use:

 man g++ 

or use info g++

+8
source

If you type

 $man g++ 

Here is the online help page . You probably have some good information. You can also try

 $g++ --help 

To your question. If you used the -o switch, you can specify the output file to use. So you could do something like

 $g++ -c main.cpp -o obj/main.obj 
+9
source

Another answer to question 2: More information on compiler switches can be found in the online manual .

+1
source

All Articles