Shou
Shou

Reputation: 51

duplicate symbol '_FT_New_Memory' while compile freetype2

I'm trying to compile freetype2 by myself. To be simple, I just compile some basic files under base folder first. However, I meet duplicate symbol issue while link the object. I'm unfamiliar with makefile and FreeType. Could someone help me solve this?

CC:=g++
SRCS = Example.c glad.c maths.c 
OBJS := Example.o glad.o maths.o
exe = app 
CFLAGS = -Wno-deprecated -I./dependencies/include
LIBS += ./dependencies/library
OPENGLLIB = ./dependencies/library/libglfw.3.3.dylib 

FREETYPE_BASE = ./freetype-2.12.1/src
BASE_SOURCE := $(FREETYPE_BASE)/base/ftsystem.c \
               $(FREETYPE_BASE)/base/ftinit.c \
               $(FREETYPE_BASE)/base/ftdebug.c \
               $(FREETYPE_BASE)/base/ftbase.c \
               $(FREETYPE_BASE)/base/ftbbox.c \
               $(FREETYPE_BASE)/base/ftglyph.c 
BASE_OBJS :=$(BASE_SOURCE:.c=.o)

CFLAGS += -I./freetype-2.12.1/include \
          -DFT2_BUILD_LIBRARY \

app:$(OBJS) $(BASE_OBJS)
    $(CC) $(CFLAGS) $(OPENGLLIB) -o app $(OBJS) $(BASE_OBJS) -L$(LIBS)

glad.o: glad.c
    $(CC) $(CFLAGS) -c glad.c -o $@

maths.o : maths.c
    $(CC) $(CFLAGS) -c maths.c -o $@

Example.o : glad.o maths.o
    $(CC) $(CFLAGS) -c Example.c -o $@


$(BASE_OBJS): $(BASE_SOURCE)
    $(CC) $(CFLAGS) -c $< -o $@ 

.PHONY:clean
clean:
    rm -rf $(OBJS) $(exe) $(BASE_OBJS)

Errors

...
uplicate symbol '_ft_alloc' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftbbox.o
duplicate symbol '_FT_New_Memory' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
duplicate symbol '_FT_Done_Memory' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
duplicate symbol '_ft_ansi_stream_io' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
duplicate symbol '_FT_Stream_Open' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
duplicate symbol '_ft_ansi_stream_close' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
duplicate symbol '_ft_free' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
duplicate symbol '_ft_realloc' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
duplicate symbol '_ft_alloc' in:
    ./freetype-2.12.1/src/base/ftsystem.o
    ./freetype-2.12.1/src/base/ftglyph.o
ld: 40 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any suggestion would be really appreciated.

Upvotes: 0

Views: 89

Answers (1)

MadScientist
MadScientist

Reputation: 100926

There are a ton of questions on SO with this same problem:

$(BASE_OBJS): $(BASE_SOURCE)
        $(CC) $(CFLAGS) -c $< -o $@

This cannot work. After variable expansion this rule is:

./freetype-2.12.1/src/base/ftsystem.o ./freetype-2.12.1/src/base/ftinit.o <...> \
    : ./freetype-2.12.1/src/base/ftsystem.c ./freetype-2.12.1/src/base/ftinit.c <...>

People seem to think that make will take this rule and try to mix and match the targets and the prerequisites so that the first .o lines up with the first .c, etc. or something like that, but that's not at all what happens. What happens is make creates one rule for each object file, where the prerequisites are all the same:

./freetype-2.12.1/src/base/ftsystem.o : ./freetype-2.12.1/src/base/ftsystem.c ./freetype-2.12.1/src/base/ftinit.c <...>
./freetype-2.12.1/src/base/ftinit.o : ./freetype-2.12.1/src/base/ftsystem.c ./freetype-2.12.1/src/base/ftinit.c <...>
 ...

So every time you compile an object file you're compiling the SAME source file over and over (because the recipe compiles $< which is the first prerequisite).

If you look at the output that make prints out about what it's doing, you'll see this very clearly.

You don't need to even create a rule for this at all. Make knows how to compile a source file into an object file. Just remove the above rule altogether. Same for your other recipes to build object files.

Upvotes: 2

Related Questions