Skip to content

Commit ea0c04f

Browse files
committed
py: Add LIBS_USERMOD to LIBS.
This enables C++ modules to correctly postion -l linker flags at the end of the flags instead of at the start. Updated the example C++ micropython.mk accordingly. Also changed example.cpp to use a std::vector to prove that libstdc++.a is being correctly linked. Signed-off-by: Dryw Wade <[email protected]>
1 parent 4efc5e1 commit ea0c04f

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

examples/usercmodule/cppexample/example.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <vector>
2+
#include <numeric>
3+
14
extern "C" {
25
#include <examplemodule.h>
36
#include <py/objstr.h>
@@ -14,8 +17,13 @@ mp_obj_t cppfunc(mp_obj_t a_obj, mp_obj_t b_obj) {
1417
// Prove we have (at least) C++11 features.
1518
const auto a = mp_obj_get_int(a_obj);
1619
const auto b = mp_obj_get_int(b_obj);
20+
// Prove that we can use the C++ standard library.
21+
std::vector<int> vec;
22+
vec.push_back(a);
23+
vec.push_back(b);
24+
int std_accumulate = std::accumulate(vec.begin(), vec.end(), 0);
1725
const auto sum = [&]() {
18-
return mp_obj_new_int(a + b);
26+
return mp_obj_new_int(std_accumulate);
1927
} ();
2028
// Prove we're being scanned for QSTRs.
2129
mp_obj_t tup[] = {sum, MP_ROM_QSTR(MP_QSTR_hellocpp)};

examples/usercmodule/cppexample/micropython.mk

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ SRC_USERMOD_CXX += $(CPPEXAMPLE_MOD_DIR)/example.cpp
88
CFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR)
99
CXXFLAGS_USERMOD += -I$(CPPEXAMPLE_MOD_DIR) -std=c++11
1010

11+
# Add any necessary library paths to LDFLAGS_USERMOD
12+
# LDFLAGS_USERMOD += -Lpath/to/libs
13+
1114
# We use C++ features so have to link against the standard library.
12-
LDFLAGS_USERMOD += -lstdc++
15+
LIBS_USERMOD += -lstdc++

py/py.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ SRC_USERMOD_LIB_ASM :=
4747
CFLAGS_USERMOD :=
4848
CXXFLAGS_USERMOD :=
4949
LDFLAGS_USERMOD :=
50+
LIBS_USERMOD :=
5051

5152
# Backwards compatibility with older user c modules that set SRC_USERMOD
5253
# added to SRC_USERMOD_C below
@@ -69,6 +70,7 @@ SRC_USERMOD_PATHFIX_LIB_ASM += $(patsubst $(USER_C_MODULES)/%.S,%.S,$(SRC_USERMO
6970
CFLAGS += $(CFLAGS_USERMOD)
7071
CXXFLAGS += $(CXXFLAGS_USERMOD)
7172
LDFLAGS += $(LDFLAGS_USERMOD)
73+
LIBS += $(LIBS_USERMOD)
7274

7375
SRC_QSTR += $(SRC_USERMOD_PATHFIX_C) $(SRC_USERMOD_PATHFIX_CXX)
7476
PY_O += $(addprefix $(BUILD)/, $(SRC_USERMOD_PATHFIX_C:.c=.o))

0 commit comments

Comments
 (0)