Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch to fix C++ linking problem #171

Open
Martin-Furter opened this issue Feb 1, 2018 · 1 comment
Open

patch to fix C++ linking problem #171

Martin-Furter opened this issue Feb 1, 2018 · 1 comment

Comments

@Martin-Furter
Copy link

Martin-Furter commented Feb 1, 2018

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 libopencm3/libopencm3#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)

@karlp
Copy link
Member

karlp commented Mar 4, 2018

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants