使用make和gcc编译和链接来自不同目录的源

时间:2015-05-26 09:48:53

标签: c++ c gcc makefile

我有一个带有一些子目录的项目。我想用每个目录的一个makefile编译它们,并将所有输出文件链接到一个可执行文件中。任何人都可以解释一下,我应该使用哪些命令(-c或-g或额外调用)?

makefile在我的root目录中:

LIBS = 

# The test will be called in this file. There is no need to change this.
#SRCS = main_test.c
SRCS = min.c max.c

###############################################################################
#                                                                             #
#   Normally it is not needed to edited below this.                           #
#                                                                             #
###############################################################################

#SRCS =     main_test.c Automated.c Basic.c Console.c CUCurses.c CUError.c  Cunit_intl.c \
#       MyMem.c TestDB.c TestRun.c Util.c wxWidget.c main_test.c \
#       $(TST_SRCS)

# define the C object files 
#
# This uses Suffix Replacement within a macro:
#   $(name:string1=string2)
#         For each word in 'name' replace 'string1' with 'string2'

OBJ = $(SRCS:%.c=%.o)

# define the C compiler to use
CC = gcc
# define any compile-time flags
ODIR = out
#compile and link
#CFLAGS = -O0 -g -Wall -fmessage-length=0 -fprofile-arcs -ftest-coverage            
#compile without link
CFLAGS = -O0 -c -Wall -fmessage-length=0 -fprofile-arcs -ftest-coverage         
#TODO Linkerflags
LFLAGS = --coverage 

#VPATH=test
#VPATH=source
# define the executable file,

TARGET = CUnit

export COVERAGE = $(TST_SRCS)
export STUBS = $(STUB_SRCS)

all: 

    $(MAKE) -C stub
    $(MAKE) -C source   

    @echo --- build finished ---
    @echo.
    @echo TODO
    @echo --- start linking ---
    @echo.

和子目录的makefile

# define any directories containing header files other than /usr/include
# TODO 

# define any libraries to link into executable:
#   if I want to link in libraries (libx.so or libx.a) I use the -llibname 
#   option, something like (this will link in libmylib.so and libm.so:
LIBS = 

#  TODO define the C source files
SRCS = $(COVERAGE) 

# define the C object files 
#
# This uses Suffix Replacement within a macro:
#   $(name:string1=string2)
#         For each word in 'name' replace 'string1' with 'string2'

OBJ = $(SRCS:%.c=%.o)

# define the C compiler to use
CC = gcc
# define any compile-time flags
ODIR = ../out
#compile without link
CFLAGS = -O0 -c -fmessage-length=0 -fprofile-arcs -ftest-coverage           
#TODO Linkerflags
LFLAGS = --coverage 

# define the executable file,


all: $(TARGET) $(OBJ)
    $(CC) $(OBJ) $(CFLAGS) -o $(TARGET) $(LFLAGS) $(OBJ) 

    @echo +++ build of source finished +++


clean:
        $(RM) *.o *~ $(MAIN)

# DO NOT DELETE THIS LINE -- make depend needs it

如果所有输出文件都在同一目录中,则链接器更容易。与ODIR一起使用?

1 个答案:

答案 0 :(得分:1)

使用编译器选项-c -g,我可以编译每个子制作并将目标文件链接在一起。

CFLAGS = -O0 -Wall -c -g -fmessage-length=0 -fprofile-arcs -ftest-coverage
相关问题