libopencm3-examples icon indicating copy to clipboard operation
libopencm3-examples copied to clipboard

patch to fix C++ linking problem

Open Martin-Furter opened this issue 7 years ago • 1 comments

Yesterday I ran into a few problems linking C++ code with libopencm3 and the provided makefiles. One reason was that it used gcc instead of g++ for linking, the other reason was the missing option --specs=nosys.specs as described in https://github.com/libopencm3/libopencm3/issues/741 .

The following patch fixes these problems for me, but a new variable "OPENCM3_LINK_CPLUSPLUS = 1" has to be set in the makefile to enable C++ linking.

Index: examples/rules.mk
===================================================================
--- examples/rules.mk   (revision 250)
+++ examples/rules.mk   (working copy)
@@ -32,7 +32,11 @@
 
 CC             := $(PREFIX)-gcc
 CXX            := $(PREFIX)-g++
+ifeq ($(OPENCM3_LINK_CPLUSPLUS),1)
+LD             := $(PREFIX)-g++
+else
 LD             := $(PREFIX)-gcc
+endif
 AR             := $(PREFIX)-ar
 AS             := $(PREFIX)-as
 OBJCOPY                := $(PREFIX)-objcopy
@@ -123,7 +127,11 @@
 ###############################################################################
 # Linker flags
 
+ifeq ($(OPENCM3_LINK_CPLUSPLUS),1)
+TGT_LDFLAGS            += --static -nostartfiles --specs=nosys.specs
+else
 TGT_LDFLAGS            += --static -nostartfiles
+endif
 TGT_LDFLAGS            += -T$(LDSCRIPT)
 TGT_LDFLAGS            += $(ARCH_FLAGS) $(DEBUG)
 TGT_LDFLAGS            += -Wl,-Map=$(*).map -Wl,--cref
@@ -208,6 +216,10 @@
        @#printf "  CXX     $(*).cpp\n"
        $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).cpp
 
+%.o: %.C
+       @#printf "  CXX     $(*).C\n"
+       $(Q)$(CXX) $(TGT_CXXFLAGS) $(CXXFLAGS) $(TGT_CPPFLAGS) $(CPPFLAGS) -o $(*).o -c $(*).C
+
 clean:
        @#printf "  CLEAN\n"
        $(Q)$(RM) *.o *.d *.elf *.bin *.hex *.srec *.list *.map generated.* ${OBJS} ${OBJS:%.o:%.d}

Ooh, it also recognizes *.C files as C++ files :)

(edited: i've put the patch into a code block)

Martin-Furter avatar Feb 01 '18 02:02 Martin-Furter

Notes: only useful if we're trying to document the rules.mk file as a standalone template, maybe that sort of thing should just be a demo repo with a submodule and makefile setup for a single project?

karlp avatar Mar 04 '18 13:03 karlp