Home > Net >  Multiple definition of main in Makefile
Multiple definition of main in Makefile

Time:05-19

I'm trying to generate a file ppm2jpeg from my sources in ./src using my objects in ./obj and ./obj_prof. I also want to generate tests that I can execute independently in ./tests.

Here's my Makefile in my main folder :

CC = gcc
LD = gcc


CFLAGS = -Wall -Wextra -std=c99 -Iinclude -Ofast -pg
LDFLAGS = -lm -pg


SRC_FILES=$(wildcard src/*.c)



OBJ_FILES=$(patsubst src/%.c,obj/%.o,$(SRC_FILES))

OBJ_PROF_FILES = obj_prof/htables_prof.o

all: ppm2jpeg tests

ppm2jpeg: $(OBJ_FILES) $(OBJ_PROF_FILES)
    $(LD) $(OBJ_FILES) $(OBJ_PROF_FILES) $(LDFLAGS) -o $@

tests: $(OBJ_FILES) $(OBJ_PROF_FILES)
    make -C tests/

obj/%.o: src/%.c
    $(CC) -c $(CFLAGS) $< -o $@


clean:
    rm -rf ppm2jpeg do_test $(OBJ_FILES)
    make -C tests/ clean
    rm -f $(wildcard images/*.bla)
    rm -f $(wildcard images/*.jpg)

And here's my Makefile in ./tests:

CC = gcc
LD = gcc


CFLAGS = -Wall -Wextra -std=c99 -I../include -O0 -g
LDFLAGS = -lm -pg

OBJ_FILES = $(wildcard ../obj/*.o)
OBJ_PROF_FILES = ../obj_prof/htables_prof.o


SRC_TESTS = $(wildcard *-test.c)
OBJ_TESTS = $(patsubst %.c,%.o,$(SRC_TESTS))
TESTS = $(patsubst %-test.c,%-test,$(SRC_TESTS))

all: $(TESTS)

%-test: %-test.o
    $(CC) -g $(OBJ_FILES) $(OBJ_PROF_FILES) $(OBJ_TEST) $^ -o $@ $(LDFLAGS)

%-test.o: %-test.c
    $(CC) $(CFLAGS) -lm -c $^

clean:
    rm -rf *.o *~ $(TESTS)
CC = gcc
LD = gcc


CFLAGS = -Wall -Wextra -std=c99 -I../include -O0 -g
LDFLAGS = -lm -pg

OBJ_FILES = $(wildcard ../obj/*.o)
OBJ_PROF_FILES = ../obj_prof/htables_prof.o

# Fichiers de test
SRC_TESTS = $(wildcard *-test.c)
OBJ_TESTS = $(patsubst %.c,%.o,$(SRC_TESTS))
TESTS = $(patsubst %-test.c,%-test,$(SRC_TESTS))

all: $(TESTS)

%-test: %-test.o
    $(CC) -g $(OBJ_FILES) $(OBJ_PROF_FILES) $(OBJ_TEST) $^ -o $@ $(LDFLAGS)

%-test.o: %-test.c
    $(CC) $(CFLAGS) -lm -c $^

clean:
    rm -rf *.o *~ $(TESTS)
CC = gcc
LD = gcc


CFLAGS = -Wall -Wextra -std=c99 -I../include -O0 -g
LDFLAGS = -lm -pg

OBJ_FILES = $(wildcard ../obj/*.o)
OBJ_PROF_FILES = ../obj_prof/htables_prof.o

# Fichiers de test
SRC_TESTS = $(wildcard *-test.c)
OBJ_TESTS = $(patsubst %.c,%.o,$(SRC_TESTS))
TESTS = $(patsubst %-test.c,%-test,$(SRC_TESTS))

all: $(TESTS)

%-test: %-test.o
    $(CC) -g $(OBJ_FILES) $(OBJ_PROF_FILES) $(OBJ_TEST) $^ -o $@ $(LDFLAGS)

%-test.o: %-test.c
    $(CC) $(CFLAGS) -lm -c $^

clean:
    rm -rf *.o *~ $(TESTS)

When I try to do that

/tests/example-test.c:5: multiple definition of `main'; ../obj/ppm2jpeg.o:ppm2jpeg.c:(.text.startup 0x0): first defined here
collect2: error: ld returned 1 exit status

CodePudding user response:

That error message means that you are trying to link together two object files each containing a main() function.

Analyzing the error message, I would guess both example-test.c and ppm2jpeg.c define main(), which means you should not link example-test.o and ppm2jpeg.o in the same executable.

In your Makefile, you should probably make sure OBJ_FILES only lists files that don't define main(), so you should make sure it leaves out ppm2jpeg.o.

  • Related