0

我有以下方式的来源。

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

我将如何为此创建一个makefile?

4

2 回答 2

1

A simple way to do this would be to add all the source to a variable and use that variable in your make command. 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.

于 2012-11-28T03:13:06.183 回答
1

An excerpt from my Makefile. This searches for cpp files in the src directory and compiles them. 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
于 2012-11-28T03:13:13.973 回答