问题描述:

I have six files and they all referencr to some other file, how to compile them under linux? how to write the make file?

  1. FileA.h

    FileA.cpp

    define the main() and f(), it also references to b()

  2. FileB.h.

    FileB.cpp

    define the b(), and reference to c()

  3. FileC.h

    FileC.cpp

    define the c() and reference to f()

网友答案:

This simple Makefile can be used as a start:

CXXFLAGS=-I.

all: FileA.o FileB.o FileC.o
    $(CXX) -o app $^

FileA.o: FileA.cpp FileA.h
FileB.o: FileB.cpp FileB.h
FileC.o: FileC.cpp FileC.h

clean:
    rm app -f *.o

Test (GNU Make 3.81):

$ make
g++ -I.   -c -o FileA.o FileA.cpp
g++ -I.   -c -o FileB.o FileB.cpp
g++ -I.   -c -o FileC.o FileC.cpp
g++ -o app FileA.o FileB.o FileC.o

Here is a more general version:

CPPFLAGS=-I.

SRCS=$(wildcard *.cpp)
OBJS=$(patsubst %.cpp,%.o,$(SRCS))
TGT=app

$(TGT): $(OBJS)
    $(CXX) -o [email protected] $^

%.o : %.cpp %.h
    $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) -o [email protected] $<

clean:
    rm -f $(TGT) $(OBJS)

Test (GNU Make 3.81):

$ make
g++ -c -I.  -o FileA.o FileA.cpp
g++ -c -I.  -o FileB.o FileB.cpp
g++ -c -I.  -o FileC.o FileC.cpp
g++ -o app FileA.o FileB.o FileC.o
$ touch FileB.h
$ make
g++ -c -I.  -o FileB.o FileB.cpp
g++ -o app FileA.o FileB.o FileC.o
$ make clean
rm -f app FileA.o FileB.o FileC.o
网友答案:

For this compilation you need simple makefile like

XX = g++
CFLAGS = -g
INC := -I./include

TGT = ./test

%.o:%.cpp
    $(XX) $(CFLAGS) $(INC) -c $< -o [email protected]

SOURCES = $(wildcard *.c *.cpp)
OBJS = $(patsubst %.cpp,%.o,$(SOURCES)))

$(TGT) : $(OBJS)
    $(XX) $(OBJS) $(INC) -o $(TGT) 
    chmod 777 $(TGT)
clean:
    rm -rf *.o *~
相关阅读:
Top