2

我在 .c 文件和 main.c 中分隔了 3 个函数,我想制作 make 文件,我在文件中写道:

# Indicate that the compiler is the gcc compiler

CC=gc

# Indicate to the compiler to include header files in the local folder
CPPFLAGS = -I

main: method1.o
main: method2.o
main: method3.o
main: method4.o
main.o: main.h

而方法 1,2,3,4 是主 .c 的功能,当我在 shell 中键入 make 时出现以下问题:

make
gcc  -I  -c -o method1.o method1.c
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
make: *** [method1.o] Error 1
4

3 回答 3

1

如果您的项目包含以下文件:method1.c method2.c method3.c method4.cmain.c

您可以使用以下make文件

CPPFLAGS=-I/path/to/header/files
CC=gcc
all: main

%.o: %.c
    $(CC) $(CPPFLAGS)  -c -o $@ $^

main: method1.o method2.o method3.o method4.o main.o
    $(CC) -o $@ $^
于 2012-11-30T09:13:03.483 回答
0

问题在于您的CPPFLAGS定义:

# Indicate to the compiler to include header files in the local folder
CPPFLAGS = -I

根据上面的评论,它错过了一个.

CPPFLAGS = -I.

Otherwise, gcc will treat the -c that comes after -I in your command line as the name of a directory where it can search for headers. Thus, as far as gcc is concerned there's no -c option, and it will attempt to link method1.c as a complete application, hence the error message complaining that there's no main function.

于 2012-12-05T18:19:54.153 回答
0
CC=gcc
CPPFLAGS=-I include
VPATH=src include
main: main.o method1.o method2.o method3.o method4.o -lm
    $(CC) $^ -o $@
main.o: main.c main.h
    $(CC) $(CPPFLAGS) -c $<
method1.o: method1.c
    $(CC) $(CPPFLAGS) -c $<
method2.o: method2.c
    $(CC) $(CPPFLAGS) -c $<
method3.o: method3.c
    $(CC) $(CPPFLAGS) -c $<
method4.o: method4.c
    $(CC) $(CPPFLAGS) -c $<

it worked like this

于 2012-12-09T22:24:08.180 回答