Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Multiple definition of main in Makefile

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading