Question about placing output to a specific directory

Louie McCrady louie@otsys.com
Tue Apr 11 16:50:00 GMT 2006


I'm working on a fairly large project.  When header files are touched,
it results in 40 to several hundred files being compiled.  This can take
more than a few minutes.  Since this invokes g++ separately for
each .cpp file I thought I might be able to reduce the total compile
time if I invoked g++ once with the entire list of .cpp files.  There
might be other overhead savings if g++ retains information about std C
headers or pre-compiled headers between files.  I don't know enough
about g++ yet to tell if it does retain information, but I would like to
find out.

My problem is that if I pass all of the .cpp files to g++ at once, I
have no way to tell g++ where to put the .o files.  It wants to put them
in the current working directory.  I was hoping that g++ has some
command line option that I am overlooking which allows the user to
specify an output directory.  Other command line compilers, Borland,
CodeWarrior, and MSVC, have a command line option to specify the output
directory for the .o files.  I am looking for a similar feature in g++.

This project has many different make files for sub-libraries, test
programs, and interfaces.  Our make system also likes to differentiate
between debug, release, and profiled compilations so that it can put
the .o files in appropriate directories.  Therefore, dumping the .o
files to the current working directory is not a good option.  I can
write a command to move the files to the appropriate directory, (which
is probably what I will end up doing), but since I'm trying to eliminate
steps here, I was hoping g++ could just put them where I wanted them to
be placed.

We also generate dependency files, so I will have to do something
similar for the .d files as well.

Im still kind of a newbee with g++.  From what I have read, it seems as
though g++ may invoke gcc separately for each file.  If this is true, it
might be better to use gcc rather than g++.  I will be trying this out
and doing some timings to see what I can do to improve compile times.
I'll also be looking into pre-compiled headers and incremental linkage.
If anyone has been down this road and has some insights or tips, I would
appreciate hearing from you.

Sincerely,

Louie

On Mon, 2006-04-10 at 13:01 -0500, Perry Smith wrote:
> How about writing a shell script front end that would compile each  
> file separately?
> 
> It may help us to help you if we understood why compiling separately  
> or moving the .o's does not work for you.
> 
> HTH,
> Perry
> 
> On Apr 10, 2006, at 12:40 PM, Louie McCrady wrote:
> 
> > g++ can take a collection of .cpp file names and compile them.  I have
> > not found a way to direct the placement of the resulting .o files to a
> > specific directory.  Is there an option that allows you to specify the
> > directory where the .o files will be placed?
> >
> > When I compile a collection of .cpp files at one time, the .o files go
> > to the current working directory.  I would like to have them placed in
> > an /obj directory somewhere else.
> >
> > I do not want to compile each .cpp file separately specifying both the
> > input and output file locations.
> >
> > I do not want to compile to the current directory, then move the .o
> > files to a separate /obj directory with a mv command.
> >
> > I've tried changing the current directory to the ./obj directory, but
> > this throws off the location to other source, header, and lib files in
> > other relative directories.  This does not appear to be a practical
> > solution.
> >
> >
> > I need something like this:
> >
> > g++ -c foo1.cpp foo2.cpp foo3.cpp -o ./obj/
> >
> > where foo1.o foo2.o foo3.o get placed in ./obj/ off the current  
> > working
> > directory.
> >
> > Any help would be greatly appreciated.
> >
> > Thank you,
> >
> > Louie.
> >
> >
> 
> 



More information about the Gcc-help mailing list