用多个目录中的文件制作文件的问题 [英] Issue with Make file with files in multiple directories

查看:84
本文介绍了用多个目录中的文件制作文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下make文件.当我只有一个目录(SOURCEDIR)中有文件时,此方法工作正常.现在,我添加了PROTOSOURCES和PROTOSRCDIR.

I have the below make file. This was working fine when I had files only in one directory which is SOURCEDIR. Now I added PROTOSOURCES and PROTOSRCDIR.

我将在SRCDIR中执行makefile.

I will be executing the makefile in SRCDIR.

VERSION = 0.0.1
CC      = /usr/bin/g++
CFLAGS  = -Wall
LDFLAGS = `pkg-config --cflags --libs protobuf`
SHFLAGS = -shared
SOURCEDIR = .
PROTOSRCDIR = ~/depot/Projects/src1/obj/cpp
SOURCES = $(wildcard $(SOURCEDIR)/*.cpp)
PROTOSOURCES = $(wildcard $(PROTOSRCDIR)/*.cc)
SRCOBJECTS = $(patsubst $(SOURCEDIR)/%.cpp, $(SOURCEDIR)/%.o, $(SOURCES))
PROTOOBJECTS = $(patsubst $(PROTOSRCDIR)/%.cc, $(PROTOSRCDIR)/%.o, $(PROTOSOURCES))

LIBRARY = libprotointf.so

.PHONY: all clean

all: $(LIBRARY)

$(LIBRARY): 
    $(CC) $(SHFLAGS) $(CFLAGS) $(LDFLAGS) $(SRCOBJECTS) $(PROTOOBJECTS) -o    $(LIBRARY)

$(SOURCEDIR)/%.o: $(SOURCEDIR)/%.cpp
    $(CC) $(CFLAGS) $(LDFLAGS) -c -fPIC $< -o $@

$(PROTOSRCDIR)/%.o: $(PROTOSRCDIR)/%.cc
    $(CC) $(CFLAGS) $(LDFLAGS) -c -fPIC $< -o $@

clean:
    rm -f libprotointf.so

我遇到以下问题.

问题1:SOURCEOBJECTS符合预期,并列出了SOURCEDIR中的所有目标文件.但是,PROTOOBJECTS仅列出* .cc文件而不是* .o文件.我希望它与patsubst一起使用后,在PROTOOBJECTS中具有目标文件的所有列表.顺便说一句,SOURCEDIR具有* .cpp文件,而PROTOSRCDIR具有.pb.cc文件.这些是由Google Protocol Buffer编译器生成的.

Issue 1: The SOURCEOBJECTS is as expected and lists all the object files in SOURCEDIR. However the PROTOOBJECTS just list *.cc files instead of *.o files. I expect it to have all the list of object files in PROTOOBJECTS after the use with patsubst. Btw, SOURCEDIR has *.cpp files where as PROTOSRCDIR has .pb.cc files. These are generated by Google Protocol Buffer compiler.

问题2:
这将是我的make文件的最终目标.
1)编译SRCDIR&生成.o文件
2)在PROTOSRCDIR&中编译文件.生成.o文件
3)用上面两个步骤中的所有对象文件生成一个共享库libprotointf.so文件.

Issue 2:
This will be the final goal of my make file.
1) Compile the files in SRCDIR & generate the .o files
2) Compile the files in PROTOSRCDIR & generate the .o files
3) generate a shared library libprotointf.so file with all the objects files from above two steps.

当我只有SRCDIR中的文件时,我的库链接和编译命令如下所示.

My Library linking and compiling commands are as below when I had files only in SRCDIR, which works fine.

$(LIBRARY): 
    $(CC) $(SHFLAGS) $(CFLAGS) $(LDFLAGS) $(SRCOBJECTS) -o $(LIBRARY)
$(SOURCEDIR)/%.o: $(SOURCEDIR)/%.cpp
    $(CC) $(CFLAGS) $(LDFLAGS) -c -fPIC $< -o $@

我需要对Makefile进行哪些修改才能制作共享库?

What are the modifications that i need to do to my Makefile in order to make the shared library ?

推荐答案

我建议使用make的VPATH功能.这告诉make也要在其他目录中查找源".所有目标文件以及共享库都将最终位于构建目录中.

I would recommend to use the VPATH feature of make. This tells make to look for "sources" in other directories too. All the object files would end up in the build directory, along with the shared library.

# Note: GNU make syntax
VERSION := 0.0.1
CXX     := /usr/bin/g++
CXXFLAGS:= -Wall -fPIC
LDFLAGS := $(shell pkg-config --cflags --libs protobuf)
SHFLAGS := -shared
SOURCEDIR := .
PROTOSRCDIR := ~/depot/Projects/src1/obj/cpp

VPATH := $(SOURCEDIR):$(PROTOSRCDIR)

SOURCES := $(wildcard $(SOURCEDIR)/*.cpp) # full paths
SOURCEFILES := $(notdir $(SOURCES)) # just (source) filenames
OBJECTS := $(SOURCEFILES:.cpp=.o) # all in the current dir

PROTOSOURCES = $(wildcard $(PROTOSRCDIR)/*.cc) # full paths
PROTOSOURCEFILES := $(notdir $(PROTOSOURCES)) # just (source) filenames
PROTOOBJECTS := $(PROTOSOURCEFILES:.cpp=.o) # all in the current dir

LIBRARY = libprotointf.so

.PHONY: all clean

all: $(LIBRARY)

$(LIBRARY): $(OBJECTS) $(PROTOOBJECTS)
    $(CC) $(SHFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^

注意:没有%.o: %.cc%.o: %.cpp规则. GNU make内置了这些.您只需填写CPPFLAGSCXXFLAGS

Note: there is no %.o: %.cc or %.o: %.cpp rule. GNU make has these built in. All you need to is fill in values for CPPFLAGS, CXXFLAGS

请注意,按照惯例,CC是包含C编译器的变量. C ++编译器的变量为CXX.类似地,CFLAGS用于C编译器. CXXFLAGS是包含用于C ++编译器的标志的变量.

Note that by convention, CC is the variable containing the C compiler. The variable of the C++ compiler is CXX. Similarly, CFLAGS is for the C compiler; CXXFLAGS is the variable containing flags for the C++ compiler.

这是如何工作的:

假设您在SOURCEDIR中有s1.cpp和s2.cpp,在PROTOSRCDIR中有p1.pb.cc,p2.pb.cc,libprotoinf.so取决于s1.o,s2.o,p1.pb.o,p2 .pb.o

Assuming you have s1.cpp and s2.cpp in SOURCEDIR and p1.pb.cc, p2.pb.cc in PROTOSRCDIR, libprotoinf.so depends on s1.o, s2.o, p1.pb.o, p2.pb.o

Make可以使用其内置的%.o: %.cpp规则轻松地从s1.cpp构建s1.o,并从s2.cpp构建s2.o.当它尝试构建p1.pb.o时,在当前目录中找不到p1.pb.cc,但是VPATH变量告诉它也要在PROTOSRCDIR中查找.然后make将通过$(PROTOSRCDIR)/p1.pb.cc构建p1.pb.o,就像当前目录中存在cc文件一样.

Make can easily build s1.o from s1.cpp and s2.o from s2.cpp using its built-in %.o: %.cpp rule. When it tries to build p1.pb.o, it won't find p1.pb.cc in the current directory, but the VPATH variable tells it to also look in PROTOSRCDIR. Then make will build p1.pb.o from $(PROTOSRCDIR)/p1.pb.cc, as if the cc file was present in the current directory.

使用这种技术,您甚至可以在与SOURCEDIR不同的单独的构建目录中进行构建.

With this technique, you can even build in a separate build directory which is different from SOURCEDIR.

这篇关于用多个目录中的文件制作文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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