[英]Set Makefile to build in another directory
Lets say my Makefile is located in the same directory as the sources and I want to store object files in obj/ subdirectory and the target executable in bin/ subdirectory. 可以说我的Makefile与源文件位于同一目录中,我想将目标文件存储在obj /子目录中,将目标可执行文件存储在bin /子目录中。
src/
main.cpp
test.cpp
test.h
/many other *.cpp files and headers/
Makefile
obj/
bin/
The problem with my Makefile is that I cannot get the OBJECTS variable to contain a list of *.o files with the samy names as *.cpp files, but in OBJDIR subdirectory. 我的Makefile的问题在于,我无法使OBJECTS变量包含* .o文件的列表,这些文件的samy名称为* .cpp文件,但位于OBJDIR子目录中。
Currently it only works if I name all object files one by one. 当前,仅当我一一命名所有目标文件时,它才有效。
CXX=g++
CXXFLAGS=-c -Wall
LDFLAGS=
SOURCES=$(wildcard *.cpp) # very convenient wildcard
BINDIR=bin
OBJDIR=obj
OBJECTS=$(OBJDIR)/main.o $(OBJDIR)/test.o # how do I make a wildcard here?
TARGET=$(BINDIR)/my_executable
all: $(SOURCES) $(TARGET)
$(TARGET): $(OBJECTS)
$(CXX) $(LDFLAGS) $(OBJECTS) -o $@
$(OBJECTS): $(OBJDIR)/%.o: %.cpp
$(CXX) $(CXXFLAGS) -c $< -o $@
clean:
rm -rf $(OBJDIR)/$(OBJECTS) $(BINDIR)/$(TARGET)
Please help Thank you! 请帮忙谢谢!
Similar question to yours already exists on this site here: 此站点上已经存在与您类似的问题:
Can I compile all .cpp files in src/ to .o's in obj/, then link to binary in ./? 我可以将src /中的所有.cpp文件编译为obj /中的.o,然后链接到./中的二进制文件吗?
either way something like this should solve your problem: 无论哪种方式都可以解决您的问题:
OBJ_FILES := $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.