简体   繁体   English

创建一个makefile并在子目录中进行编译

[英]Creating a makefile and compiling in subdirectories too

There is this nice makefile (from this tutorial: https://sites.google.com/site/michaelsafyan/software-engineering/how-to-write-a-makefile ). 有一个很好的makefile(来自本教程: https : //sites.google.com/site/michaelsafyan/software-engineering/how-to-write-a-makefile )。 However, also as mentioned on the web site, this makefile compiles all the .c and .cpp files from single directory to an executable. 但是,正如网站上所提到的,此makefile将所有.c和.cpp文件从单个目录编译为可执行文件。

program_NAME := myprogram
program_C_SRCS := $(wildcard *.c)
program_CXX_SRCS := $(wildcard *.cpp)
program_C_OBJS := ${program_C_SRCS:.c=.o}
program_CXX_OBJS := ${program_CXX_SRCS:.cpp=.o}
program_OBJS := $(program_C_OBJS) $(program_CXX_OBJS)
program_INCLUDE_DIRS :=
program_LIBRARY_DIRS :=
program_LIBRARIES :=

CPPFLAGS += $(foreach includedir,$(program_INCLUDE_DIRS),-I$(includedir))
LDFLAGS += $(foreach librarydir,$(program_LIBRARY_DIRS),-L$(librarydir))
LDFLAGS += $(foreach library,$(program_LIBRARIES),-l$(library))

.PHONY: all clean distclean

all: $(program_NAME)

$(program_NAME): $(program_OBJS)
    $(LINK.cc) $(program_OBJS) -o $(program_NAME)

clean:
    @- $(RM) $(program_NAME)
    @- $(RM) $(program_OBJS)

distclean: clean

How is it possible to modify this makefile such that it compiles following program? 如何修改此makefile,使其能够编译以下程序? eg, if my directories have following structure: 例如,如果我的目录具有以下结构:

/project
    Makefile
    main.cpp
    /src
        Class1.cpp
        Class2.cpp
    /obj
        Class1.obj
        Class2.obj
    /bin
        myProgram
    /inc
        Class1.h
        Class2.h
    /lib
        libX.a

Dan. 担。 As others have mentioned in the comments, you really ought to use CMake or some other build system that generates makefiles (and other projects) rather than creating a makefile by hand. 正如其他人在评论中提到的那样,您确实应该使用CMake或其他生成生成文件(和其他项目)的构建系统,而不是手动创建生成文件。

With the makefile that I provided in the tutorial, it's important to understand what each of the various variables represent. 使用我在教程中提供的makefile,了解各个变量代表的内容非常重要。 The program_C_SRCS and program_CXX_SRCS represent the C and C++ source files that are to be built. program_C_SRCSprogram_CXX_SRCS表示要构建的C和C ++源文件。 You can modify the wildcard such that they are picked up from the "src" directory rather than from the current directory. 您可以修改通配符,以便从“ src”目录而不是从当前目录中拾取通配符。 Likewise, the OBJS variables define the list of object files that are produced as output. 同样, OBJS变量定义了作为输出生成的目标文件列表。 You can similarly modify the way that this is constructed so that they get emitted in the "obj" subdirectory (though without further testing, I'm unsure whether the normal inference that xo is generated from x.cpp will survive if they span different directories... you'll have to see for yourself). 您可以类似地修改其构造方式,以使它们在“ obj”子目录中发出(尽管未经进一步测试,我不确定如果xo.cpp跨越不同的目录,则从x.cpp生成的xo的正常推断是否将继续存在) ...您必须自己看看)。

As for including the headers from a different directory, the program_INCLUDE_DIRS is defined for this very purpose. 至于包括来自其他目录的标头,为此定义了program_INCLUDE_DIRS Just use program_INCLUDE_DIRS := inc , and the "inc" folder should be added to the list of directory for which to search for includes. 只需使用program_INCLUDE_DIRS := inc ,“ inc”文件夹应添加到要搜索包含的目录列表中。 Likewise, you can link against the library libX.a library by adding the "lib" directory to program_LIBRARY_DIRS and adding "X" to program_LIBRARIES . 同样,你可以通过添加“lib”目录下,以对库libX.a库链接program_LIBRARY_DIRS并加入“X”来program_LIBRARIES

In any event, knowing how makefiles work is useful both for historical purposes and for debugging / understanding how the tools that generate makefiles work. 无论如何,了解历史记录文件的工作方式对于历史目的以及调试/理解生成文件的工具如何工作都非常有用。 However, it is really inadvisable in this day and age to write your own makefiles. 但是,在当今时代,编写自己的makefile确实是不可取的。 Please, please, please invest some time into learning CMake . 请,请,请花一些时间来学习CMake While there is some upfront time investment in learning a new tool such as CMake, it will save you a lot of time in the long run over writing extremely contorted Makefiles that are fragile and slow to build. 虽然在学习新工具(例如CMake)上有一些前期投资,但从长远来看,通过编写极易扭曲且构建缓慢的极其严重的Makefile,可以节省大量时间。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM