问题描述:

I have 3 functions separated in .c files and the main.c I would like to make the make file, I wrote in the file:

# 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

Whereas method 1,2,3,4 is the functions of the main .c and I have the following problem when I type make in the shell:

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

网友答案:

if your project contains the following files: method1.c method2.c method3.c method4.c and main.c

you can use the following make file

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

%.o: %.c
    $(CC) $(CPPFLAGS)  -c -o [email protected] $^

main: method1.o method2.o method3.o method4.o main.o
    $(CC) -o [email protected] $^
网友答案:

The issue is in your CPPFLAGS definition:

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

According to the comment above it, it misses a .:

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.

网友答案:
CC=gcc
CPPFLAGS=-I include
VPATH=src include
main: main.o method1.o method2.o method3.o method4.o -lm
    $(CC) $^ -o [email protected]
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

相关阅读:
Top