简体   繁体   English

如何在子目录中生成包含源的Makefile?

[英]How to generate a Makefile with sources in sub-directories?

I have source located in the following manner. 我有以下列方式来源。

/src/main.cpp
/src/hearts/hearts.cpp
/src/spades/spades.cpp
/src/oldmaid/oldmaid.cpp

How would I go about creating a makefile for this? 我该如何为此创建一个makefile?

A simple way to do this would be to add all the source to a variable and use that variable in your make command. 一种简单的方法是将所有源添加到变量中,并在make命令中使用该变量。 Here's a snippet with the relevant sections. 这是一个相关部分的片段。

APP_SRC=src/main.cpp \
    src/hearts/hearts.cpp \
    src/spades/spades.cpp \
    src/oldmaid/oldmaid.cpp

CC=g++
CFLAGS= -Wall (and any other flags you need)


#
# Rules for building the application and library 
#
all: 
make bin


bin: 
$(CC)  $(CFLAGS) $(APP_SRC)

And here's a link to a good book to get started learning Make. 这里有一本好书的链接 ,可以开始学习Make。

An excerpt from my Makefile. 我的Makefile的摘录。 This searches for cpp files in the src directory and compiles them. 这将搜索src目录中的cpp文件并进行编译。 You can add new files and make picks them automatically. 您可以添加新文件并自动选择它们。

CC = g++  

all: compile
   find src -name '*.o' -print0 | xargs -0 $(CC) -o myExecutable

compile:
    find src -name '*.cpp' -print0 | xargs -0 $(CC) -c

clean:
    find src -iname '*.o' -print0 | xargs -0 rm

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

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