gcc/g++ option to place all object files into separate directory

后端 未结 10 528
无人及你
无人及你 2020-11-30 19:44

I am wondering why gcc/g++ doesn\'t have an option to place the generated object files into a specified directory.

For example:

mkdir builddir
mkdir          


        
相关标签:
10条回答
  • 2020-11-30 20:00

    Meanwhile I found a "half-way" solution by using the -combine option.

    Example:

    mkdir builddir
    mkdir builddir/objdir
    cd srcdir
    
    gcc -combine -c file1.c file2.c file3.c -o ../builddir/objdir/all-in-one.o
    

    this "combines" all source files into one single object file.

    However, this is still "half-way" because it needs to recompile everything when only one source file changes.

    0 讨论(0)
  • 2020-11-30 20:00

    I think that telling pass gcc doesn't have an separate option to say where to put object file, since it already has it. It's "-c" - it says in what directory to put object.

    Having additional flag for directory only must change meening of "-c". For example:

    gcc -c file.c -o /a/b/c/file.o --put-object-in-dir-non-existing-option /a1/a2/a3
    

    You can not put /a/b/c/file.o under /a1/a2/a3, since both paths are absolute. Thus "-c" should be changed to name object file only.

    I advise you to consider a replacement of makefile, like cmake, scons and other. This will enable to implement build system as for for simple project as well as for bigger one too.

    See for example how it's easy to compile using cmake your example. Just create file CMakeList.txt in srcdir/:

    cmake_minimum_required(VERSION 2.6)
    project(test) 
    
    add_library(test file1.c file2c file3.c) 
    

    And now type:

    mkdir -p builddir/objdir
    cd builddir/objdir
    cmake ../../srcdir
    make
    

    That's all, object files will reside somewhere under builddir/objdir.

    I personaly use cmake and find it very convinient. It automatically generates dependencies and has other goodies.

    0 讨论(0)
  • 2020-11-30 20:03

    This is the chopped down makefile for one of my projects, which compiles the sources in 'src' and places the .o files in the directory "obj". The key bit is the the use of the patsubst() function - see the GNU make manual (which is actually a pretty good read) for details:

    OUT = lib/alib.a
    CC = g++
    ODIR = obj
    SDIR = src
    INC = -Iinc
    
    _OBJS = a_chsrc.o a_csv.o a_enc.o a_env.o a_except.o \
            a_date.o a_range.o a_opsys.o
    OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS))
    
    
    $(ODIR)/%.o: $(SDIR)/%.cpp 
        $(CC) -c $(INC) -o $@ $< $(CFLAGS) 
    
    $(OUT): $(OBJS) 
        ar rvs $(OUT) $^
    
    .PHONY: clean
    
    clean:
        rm -f $(ODIR)/*.o $(OUT)
    
    0 讨论(0)
  • 2020-11-30 20:04

    Personally for single files I do this,

    rm -rf temps; mkdir temps; cd temps/ ; gcc -Wall -v --save-temps  ../thisfile.c ; cd ../ ; geany thisfile.c temps/thisfile.s temps/thisfile.i
    

    temps folder will keep all the object, preprocessed and assembly files.

    This is a crude way of doing things and I would prefer above answers using Makefiles.

    0 讨论(0)
  • 2020-11-30 20:10

    How about changing to the directory and running the compile from there:

    cd builddir/objdir
    gcc ../../srcdir/file1.c ../../srcdir/file2.c ../../srcdir/file3.c
    

    That's it. gcc will interpret includes of the form #include "path/to/header.h" as starting in the directory the file exists so you don't need to modify anything.

    0 讨论(0)
  • 2020-11-30 20:14

    A trivial but effective workaround is to add the following right after the gcc call in your Makefile:

    mv *.o ../builddir/objdir
    

    or even a soft-clean (possibly recursive) after the compilation is done, like

    rm -f *.o
    

    or

    find . -name \*.o -exec rm {} \;
    
    0 讨论(0)
提交回复
热议问题