使用make将.o文件移动到单独的目录 [英] Using make to move .o files to a separate directory

查看:1242
本文介绍了使用make将.o文件移动到单独的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过多次尝试将我的 .o 文件移动到我的 obj 文件夹,我做,它只是不工作。

I've tried numerous attempts to move my .o files to my obj folder, but no matter what I do, it simply just doesn't work.

从提供的makefile判断,将 .o 文件移动到指定文件夹的最佳方法是什么? / p>

Judging from the makefile provided, what is the best method to move .o files to a specified folder?

BIN = bin/
OBJ = obj/
TARGET = opengl_03
DEPS = main.o  displayinit.o initializer.o algorithms.o matrix3f.o window.o vertex3.o
CC = g++
CFLAGS = -g 
LIBS = -lglut -lGLEW -lGL 
INCLUDEPATH = -L/usr/include/ -L/usr/lib/ -L/usr/lib/x86_64-linux-gnu/

$(TARGET) : $(DEPS)
    $(CC) $(CFLAGS) -o $(BIN)$(TARGET) $(DEPS) $(LIBS) $(INCLUDEPATH) 

displayinit.o : displayinit.cpp displayinit.h
    $(CC) $(LIBS) $(INCLUDEPATH) -c displayinit.cpp && mv displayinit.o $(OBJ)displayinit.o
initializer.o : initializer.cpp initializer.h
    $(CC) $(LIBS) $(INCLUDEPATH) -c initializer.cpp $(OBJ)
algorithms.o : algorithms.cpp algorithms.h
    $(CC) -c algorithms.cpp $(OBJ)
matrix3f.o : matrix3f.cpp matrix3f.h
    $(CC) $(LIBS) $(INCLUDEPATH)  -c matrix3f.cpp $(OBJ)
vertex3.o : vertex3.cpp vertex3.h
    $(CC) $(LIBS) $(INCLUDEPATH)  -c vertex3.cpp $(OBJ)
window.o : window.cpp window.h
    $(CC) $(LIBS) $(INCLUDEPATH) -c window.cpp $(OBJ)
main.o : main.cpp
    $(CC) $(LIBS) $(INCLUDEPATH) -c main.cpp $(OBJ)


推荐答案

要指定对象的创建位置,请使用 -o / strong>

To specify where the object is created use -o

window.o : window.cpp window.h
    $(CC) $(LIBS) $(INCLUDEPATH) -c window.cpp -o $(OBJ)/$@






这里是你可以做的:


Here is what you could do:


  1. 指定目标文件的目录

  1. specify the directory where you want the object files to go

OBJDIR    =   objdir


  • 通过替换文件,从所有 .cpp 文件的列表中创建需要编译的目标文件的列表。 .cpp .o 并添加前缀 $(OBJDIR)/

  • Create a list of object files that need to be compiled, from the list of all .cpp files by replacing .cpp with .o and add the prefix $(OBJDIR)/ to it:

    OBJ = $(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
    

    因此,您的$(OBJ)将如下所示: objdir / window.o objdir / main.o 等等

    So your $(OBJ) will look like: objdir/window.o objdir/main.o and so on

    添加目标以创建目录:

    $(OBJDIR):
        mkdir $(OBJDIR)
    


  • 在设定主要目标之前设定指定目标:

  • Make the directory target before you make your main target:

    all: $(OBJDIR) myapp
    


  • 中的对象文件 $(OBJDIR) >文件在当前目录中:

  • Rule to compile all the .o object files in $(OBJDIR) from .cpp files in the current directory:

    $(OBJDIR)/%.o: %.cpp
        $(GCC) $(CPPFLAGS) -c $< -o $@
    

    这将导致类似的结果:

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


  • 您的主要目标未更改:

  • Your main target is unchanged:

    $(TARGET): $(OBJ)
        $(GCC) $(LDFLAGS) -o $@ $^ 
    

    g++  -o myapp objdir/window.o objdir/main.o 
    


  • 为了完整性,请将 clean 添加到清除对象:

    clean:
        @rm -f $(TARGET) $(wildcard *.o)
        @rm -rf $(OBJDIR) 
    


  • 定义 .PHONY 例如即使存在具有相同名称的目录或文件也将创建这些文件:

  • And define .PHONY targets, e.g. these will be made even if directories or files with the same name exist:

    .PHONY: all clean
    


  • 所以它应该像:

    OBJDIR    =   objdir
    OBJ       =   $(addprefix $(OBJDIR)/, $(patsubst %.cpp, %.o, $(wildcard *.cpp)))
    TARGET    =   my app
    
    .PHONY: all clean
    
    all: $(OBJDIR) $(TARGET)
    
    $(OBJDIR):
        mkdir $(OBJDIR)
    
    $(OBJDIR)/%.o: %.cpp
        $(GCC) $(CPPFLAGS) -c $< -o $@
    
    $(TARGET): $(OBJ)
        $(GCC) $(LDFLAGS) -o $@ $^ 
    
    clean:
        @rm -f $(TARGET) $(wildcard *.o)
        @rm -rf $(OBJDIR) 
    

    如果你有一些文件,例如 main.cpp a.cpp make 会做的事情:

    And if you have files such as main.cpp and a.cpp this is what make would do:

    > ls
    Makefile a.cpp    main.cpp
    
    > make
    mkdir objdir
    g++ -I. -c a.cpp -o objdir/a.o
    g++ -I. -c main.cpp -o objdir/main.o
    g++ -o myapp objdir/a.o objdir/main.o 
    
    > ls
    Makefile a.cpp    main.cpp objdir   myapp
    
    > make clean
    > ls
    Makefile a.cpp    main.cpp
    

    如果您想阅读更多以上任何内容都可以查看 GNU make doc页面

    And if you want to read more details about any of the above have a look at GNU make doc page

    这篇关于使用make将.o文件移动到单独的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆