带有生成和源子目录的Makefile [英] Makefile with Build and Source Sub-Directories

查看:144
本文介绍了带有生成和源子目录的Makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试应用这个设置一个makefile来编译一个C ++程序,以便目标文件进入特定的目录并且源文件位于它们自己的独立目录中。主文件夹有三个子文件夹测试(包含一些我想编译的cpp文件),include(包含一些测试使用的头文件)和build(空文件夹,我希望.o文件去)。

I am trying to apply this and this to set up a makefile to compile a C++ program so that the object files go to a particular directory and that the source files are in their own separate directory. The main folder has three subfolders test (has some cpp files I want to compile), include (has some headers used by tests), and build (empty folder where I want the .o files to go).

这是make文件。

This is the make file.

CC = g++
CFLAGS = -g -Wall

INC_DIR1 = include
INC_DIR2 = C:/armadillo-4.200.0/include
INC_DIR = $(INC_DIR1) $(INC_DIR2)
INCLUDES = $(foreach d, $(INC_DIR), -I$d)

BUILD_DIR = build

SRC_DIR = test
SRC = $(wildcard */*.cpp)

OBJS = $(SRC:.cpp=.o)
#OBJS = $(addprefix $(BUILD_DIR)/, $(notdir $(SRC:.cpp=.o)))
#create objects as the build directory plus the non-directory component of
#source

MAIN = armadillo_extra_functions_test

.PHONY: depend clean

all: $(BUILD_DIR) $(MAIN)
    @echo  amradillo_extra_functions_test has been compiled

$(BUILD_DIR):
    mkdir -p $@

%.o: %.cpp
    $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
#$(BUILD_DIR)/%.o: %.cpp
#   $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@
#put .os in build directory instead of test. 

$(MAIN): $(OBJS) 
    $(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS)

clean:
    $(RM) *.o *~ $(MAIN) $(BUILD_DIR)/*.o $(SRC_DIR)/*.o

depend: $(SRC)
    makedepend $(INCLUDES) $^

上面的代码编译成功,exe运行正常。但是,它将.cpps放在测试文件夹中。注释代码(而不是上面的代码)是根据上述链接进行调整的,以便将.o's放置在构建目录中。然而,我得到错误

The above code compiles successfully and the exe works properly. However, it puts the .cpps in the test folder. The commented code (instead of the code above it) is adapted from the above links to try to put the .o's in the build directory. However, I get the error

make: *** No rule to make target 'build/armadillo_extra_functions_test.o', neede
d by 'armadillo_extra_functions_test'.  Stop.

当我从第一个版本的代码中取得.o文件时,将它们放到build文件夹中,删除了exe,并运行了注释版本,它编译得很好。

When I took the .o files from the first version of the code, put them in the build folder, deleted the exe, and ran the commented version, it compiled just fine.

因此,我认为这个问题与%.o:%.cpp部分有关。 .o文件被命名为/build/blah.o,源文件被命名为/test/blah.cpp。我尝试了几种不同的组合,例如上面的$(BUILD_DIR)/%。o:%.cpp以及前缀版本,但没有运气。

As such, I think the issue is related to the %.o: %.cpp part. The .o files are named something like /build/blah.o and the source files are named something like /test/blah.cpp. I tried a few different combinations, such as the prefix version above with the $(BUILD_DIR)/%.o: %.cpp and without, but no luck.

我不想依赖向make文件添加移动命令,而是宁愿让%.o:%。cpp部分正常工作。

I don't want to have to rely adding a move command to the make file and would rather get the %.o:%.cpp part working properly.

推荐答案

设置内部make变量 VPATH 应该有助于找到'源代码树构建的源代码。只需将其设置为:

Setting the intrinsic make variable VPATH should help to find sources for 'out of source tree builds'. Just set it like this:

VPATH = $(SRC_DIR)

这篇关于带有生成和源子目录的Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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