Mohammad Karmi
Mohammad Karmi

Reputation: 1465

Makefile C in linux

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

Upvotes: 2

Views: 636

Answers (3)

Mohammad Karmi
Mohammad Karmi

Reputation: 1465

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

Upvotes: 0

Virgile
Virgile

Reputation: 10158

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.

Upvotes: 0

MOHAMED
MOHAMED

Reputation: 43578

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 $@ $^

main: method1.o method2.o method3.o method4.o main.o
    $(CC) -o $@ $^

Upvotes: 1

Related Questions