Fix linking with C++ files.

When linking with C++ files the linker also needs to link against the C++
libraries. This is done automatically when invoking the compiler upon linking.
Since we don't want C++ dependencies on C-only projects we check if we actually
have C++ files and use either the C or C++ compiler.

Rename CFLAGS since it's now used for both C and C++ compiler and add dedicated
CFLAGS, CXXFLAGS and LDFLAGS variables.

Change-Id: I9cc068a8038f21e8fd96b20173a8f790e6ab4b6e
This commit is contained in:
Dominik Riebeling 2016-12-16 21:50:36 +01:00
parent 78cb7f0cf0
commit 3ee79724f6

View file

@ -31,25 +31,33 @@ endif
# overwrite for releases # overwrite for releases
APPVERSION ?= $(shell $(TOP)/../tools/version.sh $(TOP)/..) APPVERSION ?= $(shell $(TOP)/../tools/version.sh $(TOP)/..)
CFLAGS += -DVERSION=\"$(APPVERSION)\" GCCFLAGS += -DVERSION=\"$(APPVERSION)\"
TARGET_DIR ?= $(abspath .)/ TARGET_DIR ?= $(abspath .)/
CC := gcc CC := gcc
CXX := g++ CXX := g++
# use either CC or CXX to link: this makes sure the compiler knows about its
# internal dependencies. Use CXX if we have at least one c++ file, since we
# then need to link the c++ standard library (which CXX does for us).
ifeq ($(strip $(filter %.cpp,$(SOURCES) $(LIBSOURCES))),)
LD := $(CC)
else
LD := $(CXX)
endif
CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -) CPPDEFINES := $(shell echo foo | $(CROSS)$(CC) -dM -E -)
BINARY = $(OUTPUT) BINARY = $(OUTPUT)
# when building a Windows binary add the correct file suffix # when building a Windows binary add the correct file suffix
ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN) ifeq ($(findstring CYGWIN,$(CPPDEFINES)),CYGWIN)
BINARY = $(OUTPUT).exe BINARY = $(OUTPUT).exe
CFLAGS+=-mno-cygwin GCCFLAGS += -mno-cygwin
COMPILETARGET = cygwin COMPILETARGET = cygwin
else else
ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW) ifeq ($(findstring MINGW,$(CPPDEFINES)),MINGW)
BINARY = $(OUTPUT).exe BINARY = $(OUTPUT).exe
COMPILETARGET = mingw COMPILETARGET = mingw
# use POSIX/C99 printf on windows # use POSIX/C99 printf on windows
CFLAGS += -D__USE_MINGW_ANSI_STDIO=1 GCCFLAGS += -D__USE_MINGW_ANSI_STDIO=1
else else
ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE) ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
COMPILETARGET = darwin COMPILETARGET = darwin
@ -66,7 +74,7 @@ ifeq ($(findstring APPLE,$(CPPDEFINES)),APPLE)
# When building for 10.4+ we need to use gcc. Otherwise clang is used, so use # When building for 10.4+ we need to use gcc. Otherwise clang is used, so use
# that to determine if we need to set arch and isysroot. # that to determine if we need to set arch and isysroot.
ifeq ($(findstring __clang__,$(CPPDEFINES)),__clang__) ifeq ($(findstring __clang__,$(CPPDEFINES)),__clang__)
CFLAGS += -mmacosx-version-min=10.5 GCCFLAGS += -mmacosx-version-min=10.5
else else
# when building libs for OS X 10.4+ build for both i386 and ppc at the same time. # when building libs for OS X 10.4+ build for both i386 and ppc at the same time.
# This creates fat objects, and ar can only create the archive but not operate # This creates fat objects, and ar can only create the archive but not operate
@ -75,7 +83,7 @@ ARCHFLAGS += -arch ppc -arch i386
# building against SDK 10.4 is not compatible with gcc-4.2 (default on newer Xcode) # building against SDK 10.4 is not compatible with gcc-4.2 (default on newer Xcode)
# might need adjustment for older Xcode. # might need adjustment for older Xcode.
CC = gcc-4.0 CC = gcc-4.0
CFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4 GCCFLAGS += -isysroot /Developer/SDKs/MacOSX10.4u.sdk -mmacosx-version-min=10.4
endif endif
endif endif
@ -106,7 +114,7 @@ $(BINARY): $(OBJS) $(EXTRADEPS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) $(TARGET_
$(info LD $@) $(info LD $@)
$(SILENT)$(call mkdir,$(dir $@)) $(SILENT)$(call mkdir,$(dir $@))
# EXTRADEPS need to be built into OBJDIR. # EXTRADEPS need to be built into OBJDIR.
$(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -o $(BINARY) \ $(SILENT)$(CROSS)$(LD) $(ARCHFLAGS) $(LDFLAGS) -o $(BINARY) \
$(OBJS) $(addprefix $(OBJDIR),$(EXTRADEPS)) \ $(OBJS) $(addprefix $(OBJDIR),$(EXTRADEPS)) \
$(addprefix $(OBJDIR),$(EXTRALIBOBJS)) lib$(OUTPUT).a $(addprefix $(OBJDIR),$(EXTRADEPS)) $(LDOPTS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) lib$(OUTPUT).a $(addprefix $(OBJDIR),$(EXTRADEPS)) $(LDOPTS)
@ -114,12 +122,12 @@ $(BINARY): $(OBJS) $(EXTRADEPS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) $(TARGET_
$(OBJDIR)%.c.o: $(OBJDIR)%.c.o:
$(info CC $<) $(info CC $<)
$(SILENT)$(call mkdir,$(dir $@)) $(SILENT)$(call mkdir,$(dir $@))
$(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -MMD -c -o $@ $< $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(GCCFLAGS) $(CFLAGS) -MMD -c -o $@ $<
$(OBJDIR)%.cpp.o: $(OBJDIR)%.cpp.o:
$(info CXX $<) $(info CXX $<)
$(SILENT)$(call mkdir,$(dir $@)) $(SILENT)$(call mkdir,$(dir $@))
$(SILENT)$(CROSS)$(CXX) $(ARCHFLAGS) $(CFLAGS) -MMD -c -o $@ $< $(SILENT)$(CROSS)$(CXX) $(ARCHFLAGS) $(GCCFLAGS) $(CXXFLAGS) -MMD -c -o $@ $<
# lib rules # lib rules
lib$(OUTPUT).a: $(TARGET_DIR)lib$(OUTPUT).a lib$(OUTPUT).a: $(TARGET_DIR)lib$(OUTPUT).a
@ -133,7 +141,7 @@ $(OUTPUT).dll: $(TARGET_DIR)$(OUTPUT).dll
$(TARGET_DIR)$(OUTPUT).dll: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS)) $(TARGET_DIR)$(OUTPUT).dll: $(LIBOBJS) $(addprefix $(OBJDIR),$(EXTRALIBOBJS))
$(info DLL $(notdir $@)) $(info DLL $(notdir $@))
$(SILENT)$(call mkdir,$(dir $@)) $(SILENT)$(call mkdir,$(dir $@))
$(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(CFLAGS) -shared -o $@ $^ \ $(SILENT)$(CROSS)$(CC) $(ARCHFLAGS) $(GCCFLAGS) -shared -o $@ $^ \
-Wl,--output-def,$(TARGET_DIR)$(OUTPUT).def -Wl,--output-def,$(TARGET_DIR)$(OUTPUT).def
# create lib file from objects # create lib file from objects