自动生成文件,源文件和目标文件位于不同目录中 [英] Automatic makefile with source and object files in different directories

查看:89
本文介绍了自动生成文件,源文件和目标文件位于不同目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个makefile放在一起,该makefile将一个目录(例如src)中的源文件,将它们编译成另一个目录(例如build)中的目标文件,然后将这些文件和在主目录中从它们创建一个静态库.

I'm attempting to put together a makefile that will take source files from a directory (eg. src), compile them into object files in another directory (eg. build), and then take those files and create a static library from them in the main directory.

到目前为止,这是我的努力:

Here's my effort so far:

LIBNAME := test
LIBNAME := lib$(LIBNAME).a

CC = g++
CFLAGS := -O0 -Wall -g -fPIC
INCLUDE := include
SOURCE := src
BUILD := build

CPPFILES := $(foreach dir, $(SOURCE)/, $(notdir $(wildcard $(SOURCE)/*.cpp)))
OBJFILES := $(addprefix $(BUILD)/, $(CPPFILES:.cpp=.o))

all: $(LIBNAME) $(OBJFILES)

$(LIBNAME): $(OBJFILES)
    ar rcs $(LIBNAME) $(OBJFILES)

.cpp.o:
    $(CC) $(CFLAGS) -I$(INCLUDE) -c $< -o $@

clean:
    rm -rf $(BUILD)

哪个给我这个:

make: *** No rule to make target `build/point.o', needed by `libtest.a'.  Stop

推荐答案

您似乎仍然需要GNU make(foreach函数),因此将老式后缀规则.cpp.o重写为

You seem do be requiring GNU make anyway (foreach function), so rewriting the old-style suffix rule .cpp.o to

$(BUILD)/%.o : $(SOURCE)/%.cpp

应该可以解决问题.您也可以尝试使用VPATH变量或vpath指令,请参见这些手册.

should do the trick. You might also try using the VPATH variable or the vpath directive, see the make manual for these.

通常,您可以使用automake解决此问题,这将为您完成大部分工作.

In general, you might just tackle this problem with automake, which does most of this stuff for you.

这篇关于自动生成文件,源文件和目标文件位于不同目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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