我尝试了无数次尝试将我的 .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
文件移动到指定文件夹的最佳方法是什么?
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
window.o : window.cpp window.h
$(CC) $(LIBS) $(INCLUDEPATH) -c window.cpp -o $(OBJ)/$@
<小时>
您可以这样做:
Here is what you could do:
指定目标文件所在的目录
specify the directory where you want the object files to go
OBJDIR = objdir
通过将 .cpp
替换为 .o,从所有
并添加前缀 .cpp
文件的列表中创建需要编译的目标文件列表$(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
如果目录不存在,则添加一个目标以创建该目录:
Add a target to create the directory if it does not exist:
$(OBJDIR):
mkdir $(OBJDIR)
在创建主要目标之前先创建目录目标:
Make the directory target before you make your main target:
all: $(OBJDIR) myapp
从当前目录中的 .cpp
文件编译 $(OBJDIR)
中所有 .o
目标文件的规则:
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
目标添加到清理对象中:
For completeness add clean
target to cleanup objects:
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 制作文档页面
And if you want to read more details about any of the above have a look at GNU make doc page
这篇关于使用 make 将 .o 文件移动到单独的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!