简体   繁体   English

用于将对象和依赖文件自动放置在单独文件夹中的Makefile

[英]Makefile for automatic placement of object and dependency file in separate folder

I would like to place the object files of the compiler in a different folder than the source files. 我想将编译器的目标文件放在与源文件不同的文件夹中。 I made it working, when using the same folder structure as the way my source code is build up, but this is not the solution i am looking for. 当使用与我的源代码构建方式相同的文件夹结构时,它可以正常工作,但这不是我正在寻找的解决方案。

What I would prefer is the following: 我希望以下是:

Sources: 资料来源:

src/main.cpp
src/myClass/myClass.cpp

Object: 宾语:

obj/main.o
obj/myClass.o

Dependencies 依存关系

dep/main.d
dep/main.d

What I have until now is the following 我到目前为止所拥有的是以下内容

VARIABLES 变数

$(OBJLIST)=obj/main.o obj/myClass.o
$(SRCDIR)=src
$(OBJDIR)=obj

LINKER 链接器

$(PROJECT): $(OBJLIST)
    $(CXX) $(OBJLIST) $(LIBS) -o $@

COMPILER and here is my problem 编译器,这是我的问题

This is what is working 这是起作用的

$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
    @mkdir -p $(DEPDIR) $(OBJDIR)
    $(CXX) $(CPPFLAGS) -c $< -o $@
    $(CXX) $(CPPFLAGS) -MM -MT 1_obj/main.o 0_src/main.cpp -o 2_dep/main.dep
$(OBJDIR)/%.o: $(SRCDIR)/myClass/%.cpp
    @mkdir -p $(DEPDIR) $(OBJDIR)
    $(CXX) $(CPPFLAGS) -c $< -o $@
    $(CXX) $(CPPFLAGS) -MM -MT 1_obj/myClass.o 0_src/myClass/myClass.cpp -o 2_dep/myClass.dep

I also have an object called SRCDIRS containing: 我还有一个名为SRCDIRS的对象,其中包含:

SRCDIRS
    ./
    ./myClass

So i hoped I could merge the compile-definitions to something like 所以我希望我可以将编译定义合并到类似

$(OBJDIR)/%.o: $(SRCDIR)/$(SRCDIRS)/%.cpp
    @mkdir -p $(DEPDIR) $(OBJDIR)
    $(CXX) $(CPPFLAGS) -c $< -o $@
    $(CXX) $(CPPFLAGS) -MM -MT $@ @< -o $(DEPDIR)/$(notdir $(basename $<).dep)

I hoped this would generalize for both possibilities, but somehow, it seams it does not loop through SRCDIRS and check for any match. 我希望这可以同时适用于这两种可能性,但是不知何故,它接缝不会在SRCDIRS中循环并检查是否匹配。

You can do it with vpath . 您可以使用vpath做到这一点。 Be aware that VPATH can only be used to find source files. 请注意,VPATH 可用于查找源文件。

So, something like this: 因此,如下所示:

VPATH = $(SRCDIRS)

$(OBJDIR)/%.o : %.c
        ...

is all you need. 是你所需要的全部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 makefile自动src文件检测和依赖项生成 - makefile automatic src file detection and dependency generation GNU Makefile自动依赖关系解析 - GNU Makefile automatic dependency resolve 是否可以在一次Makefile传递中使用gcc / g ++ / nvcc自动依赖项-M而不将依赖项保存到文件中? - Is it possible to use gcc/g++/nvcc automatic dependency -M in a single pass of a Makefile without saving dependencies to a file? 如何使用 C++ 和 makefile 在不同的文件夹中制作目标文件 - How to make an object file in a different folder with c++ and a makefile 在单独的文件夹中生成依赖文件 - Generating dependency files in separate folder 如何更改我的makefile以构建到单独的文件夹中 - How to change my makefile to build into a separate folder 在单独的文件夹中生成带有源文件和头文件的makefile - Build makefile with source and header in separate folder 如何在Makefile.am(auto make)中指定以在编译后将库目标文件(.lo)和目标文件(.o)保存在单独的文件夹中 - How to specify in Makefile.am(auto make) for saving library object files(.lo) and object files(.o) in separate folder after compilation makefile获取特定目录中所有文件的依赖性 - makefile to take dependency of all file in particular directory 在Makefile中添加对未编译文件的依赖 - Adding dependency on a non-compiled file in a Makefile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM