1515
1616ifeq ($(PLATFORM ) ,SOLARIS)
1717# have to explicitly pull in the socket library
18- LOADLIBES += -lsocket
18+ LDLIBS += -lsocket
1919endif
2020
2121ifeq ($(PLATFORM ) ,NETBSD)
2222# have to explicitly state that the maths library should be included
23- LOADLIBES += -lm
23+ LDLIBS += -lm
2424endif
2525
2626ifeq ($(BUILD ) ,LINUX-alpha)
@@ -35,22 +35,41 @@ endif
3535
3636ifeq ($(PLATFORM ) ,MINGW)
3737# need to explicitly add the Windows sockets 2 library
38- LOADLIBES += -lWs2_32
38+ LDLIBS += -lWs2_32
3939# gcc handling of DLLs on Windows
4040LDFLAGS += -Wl,--enable-auto-import
41+ # $(CC) is used for C compiles and link operations. This defaults to cc which on most platforms then maps to gcc
42+ # However, MinGW platforms do not have a mapping from cc to gcc so builds fail. This is a work-round of this problem
43+ CC := gcc
4144endif
4245
46+ ifeq ($(PLATFORM ) ,MACOS)
47+ CXX := clang -m64
48+ endif
49+
50+ # need to build PIC on some platforms in order to use code in shared libraries
51+ # Note: I use PIC in all compiles on these platforms assuming that the historic lower performance of PIC is now negligible
4352ifeq ($(PLATFORM ) ,GNULINUX)
44- # need to build PIC in order to use code in shared libraries
53+ CFLAGS += -fPIC
54+ CXXFLAGS += -fPIC
55+ else ifeq ($(PLATFORM),FREEBSD)
56+ CFLAGS += -fPIC
57+ CXXFLAGS += -fPIC
58+ else ifeq ($(PLATFORM),NETBSD)
59+ CFLAGS += -fPIC
60+ CXXFLAGS += -fPIC
61+ else ifeq ($(PLATFORM),OPENBSD)
4562CFLAGS += -fPIC
4663CXXFLAGS += -fPIC
4764endif
4865
66+ # manage switch between static and dynamic linking of runtime library
4967ifeq ($(STATIC ) ,on)
5068# switch on static linking with C/C++ runtimes
5169LDFLAGS += -static-libgcc -static-libstdc++
5270else
53- LOADLIBES += -lstdc++
71+ # dynamic linking options - create a linker dependency on C++ runtime
72+ LDLAST += -lstdc++
5473endif
5574
5675# ###############################################################################
@@ -198,7 +217,7 @@ build:: $(LIBRARY) $(ARCHIVE_LIBRARIES) $(IMAGE) $(SHARED_SO)
198217$(SUBDIR ) /% .o : % .cpp
199218 @echo " $( LIBNAME) :$( SUBDIR) : C++ compiling $<"
200219 @mkdir -p $(SUBDIR )
201- @ $(CXX ) -x c++ -c -MMD $(CPPFLAGS ) $(CXXFLAGS ) $(INCLUDES ) $< -o $@
220+ $(CXX ) -x c++ -c -MMD $(CPPFLAGS ) $(CXXFLAGS ) $(INCLUDES ) $< -o $@
202221
203222# the rule for compiling a C source file
204223$(SUBDIR ) /% .o : % .c
@@ -250,8 +269,8 @@ $(SHARED_SO): $(LIBRARY) $(ARCHIVES)
250269 @echo " $( LIBNAME) :$( SUBDIR) : $( BUILD_TYPE) Linking Shared Library $( SHARED_SO) "
251270 @echo " $( LIBNAME) :$( SUBDIR) : flags: $( LDFLAGS) "
252271 @for l in $(LIBRARY ) $(ARCHIVES ) ; do echo " $( LIBNAME) :$( SUBDIR) : using: $$ l" ; done
253- @echo " $( LIBNAME) :$( SUBDIR) : libs: $( LOADLIBES ) "
254- @gcc -shared -o $(SHARED_SO ) $(LDFLAGS ) $(RC_OBJECTS ) -Wl,--whole-archive $(LIBRARY ) -Wl,--no-whole-archive $(ARCHIVES ) $(LOADLIBES )
272+ @echo " $( LIBNAME) :$( SUBDIR) : libs: $( LDLIBS ) $( LDLAST ) "
273+ @$( CC ) -shared -o $(SHARED_SO ) $(LDFLAGS ) $(RC_OBJECTS ) -Wl,--whole-archive $(LIBRARY ) -Wl,--no-whole-archive $(ARCHIVES ) $(LDLIBS ) $( LDLAST )
255274
256275endif
257276
@@ -267,13 +286,15 @@ $(ARCHIVE_LIBRARIES): FORCE
267286 @$(MAKE ) -C $@
268287
269288# the rule for linking an image
289+ # Note: I used to link with $(CXX) which maps onto g++ but there's a bug see: http://sourceforge.net/p/tdm-gcc/bugs/291/
290+ # I now link with $(CC) which maps onto cc by default but should then map onto gcc
270291$(IMAGE ) : $(LIBRARY ) $(ARCHIVES )
271292 @echo " $( LIBNAME) :$( SUBDIR) : $( BUILD_TYPE) Linking $( IMAGE) "
272293 @echo " $( LIBNAME) :$( SUBDIR) : flags: $( LDFLAGS) "
273294 @for l in $(LIBRARY ) $(ARCHIVES ) ; do echo " $( LIBNAME) :$( SUBDIR) : using: $$ l" ; done
274- @echo " $( LIBNAME) :$( SUBDIR) : libs: $( LOADLIBES ) "
295+ @echo " $( LIBNAME) :$( SUBDIR) : libs: $( LDLIBS ) $( LDLAST ) "
275296 @mkdir -p $(dir $(IMAGE ) )
276- @gcc -o $(IMAGE ) $(LDFLAGS ) $(RC_OBJECTS ) $^ $(LOADLIBES )
297+ @$( CC ) -o $(IMAGE ) $(LDFLAGS ) $(RC_OBJECTS ) $^ $(LDLIBS ) $( LDLAST )
277298
278299endif
279300
0 commit comments