On Mon, Mar 15, 2021 at 6:00 PM Flavio Castro Alves Filho < flavio.al...@gmail.com> wrote:
> Hello, > > I am trying to use STL in a simple C++ Hello project and I am getting > compilation error.s > > Is STL supported in NuttX? Can I use it? > > Yes, you can. Please reference the following config and Make.defs to setup your C++ environment: nuttx/boards/arm/stm32/stm32f4discovery/configs/cxxtest/defconfig nuttx/boards/sim/sim/sim/configs/cxxtest/defconfig nuttx/nuttx/boards/sim/sim/sim/configs/libcxxtest/defconfig > Here is my sample code: > > === Hello.cpp === > #include <stdio.h> > #include "HelloWorld.h" > > CHelloWorld::CHelloWorld() { > mSecret = 42; > printf("Constructor: mSecret=%d\n",mSecret); > } > > CHelloWorld::~CHelloWorld() { > > } > > bool CHelloWorld::HelloWorld(void) { > printf("HelloWorld: mSecret=%d\n",mSecret); > > if (mSecret == 42) { > printf("CHelloWorld: HelloWorld: Hello, world!"); > return true; > } > else { > printf("CHelloWorld: HelloWorld: CONSTRUCTION FAILED!\n"); > return false; > } > } > ========= > > === Hello.h === > class CHelloWorld > { > public: > CHelloWorld(); > ~CHelloWorld(); > bool HelloWorld(void); > private: > int mSecret; > }; > ========= > > === Log.cpp === > #include "Log.h" > #include <stdio.h> > #include <string> > > void Log::print(std::string_view message) { > > FILE *fp = fopen("/dev/ttyS0", "w"); > if (fp == NULL) { > printf("Error opening serial port!\n"); > return; > } > > std::string msgStr { message }; > > /* Try to force input data on stdin */ > fwrite(msgStr.c_str(), sizeof(char), message.length(), fp); > > fclose(fp); > } > ========= > > === Log.h === > #include <string_view> > > class Log { > public: > static void print(std::string_view message); > }; > ========= > > And there the compilation error: > ========= > -- Build files have been written to: /home/ubuntu/nuttx-apps/hellocpp/build > [4/4] Linking CXX executable src/hellocpp > FAILED: src/hellocpp > : && arm-none-eabi-ld --entry=__start -nostartfiles -nostdlib > -nodefaultlibs > -T/home/ubuntu/nuttx-apps/hellocpp/nuttx-export-9.1.0/scripts/ld.script > -o hellocpp.elf src/CMakeFiles/hellocpp.dir/HelloWorld.cpp.o > src/CMakeFiles/hellocpp.dir/Log.cpp.o > src/CMakeFiles/hellocpp.dir/main.cpp.o > -L/home/ubuntu/nuttx-apps/hellocpp/src/hellocpp > -L/home/ubuntu/nuttx-apps/hellocpp/nuttx-export-9.1.0/libs > --start-group -lc -larch -lbinfmt -lboard -lboards -ldrivers > -lfs -lmm -lsched -lxx -lnet -lsupc++ > /usr/lib/gcc/arm-none-eabi/9.2.1/thumb/v7e-m/nofp/libgcc.a > --end-group && cd /home/ubuntu/nuttx-apps/hellocpp/build/src && > arm-none-eabi-objcopy -S -O binary > /home/ubuntu/nuttx-apps/hellocpp/build/hellocpp.elf > /home/ubuntu/nuttx-apps/hellocpp/build/hellocpp.bin > arm-none-eabi-ld: src/CMakeFiles/hellocpp.dir/Log.cpp.o: in function > `void std::__cxx11::basic_string<char, std::char_traits<char>, > std::allocator<char> >::_M_construct<char const*>(char const*, char > const*, std::forward_iterator_tag)': > /usr/include/newlib/c++/9.2.1/bits/basic_string.tcc:212: undefined > reference to `std::__throw_logic_error(char const*)' > arm-none-eabi-ld: > /usr/include/newlib/c++/9.2.1/bits/basic_string.tcc:219: undefined > reference to `std::__cxx11::basic_string<char, std::char_traits<char>, > std::allocator<char> >::_M_create(unsigned int&, unsigned int)' > arm-none-eabi-ld: > /usr/include/newlib/c++/9.2.1/bits/basic_string.tcc:225: undefined > reference to `std::__cxx11::basic_string<char, std::char_traits<char>, > std::allocator<char> >::_S_copy_chars(char*, char const*, char > const*)' > arm-none-eabi-ld: src/CMakeFiles/hellocpp.dir/Log.cpp.o: in function > `std::__cxx11::basic_string<char, std::char_traits<char>, > std::allocator<char> >::~basic_string()': > /usr/include/newlib/c++/9.2.1/bits/basic_string.h:658: undefined > reference to `std::__cxx11::basic_string<char, std::char_traits<char>, > std::allocator<char> >::_M_dispose()' > arm-none-eabi-ld: > /usr/include/newlib/c++/9.2.1/bits/basic_string.h:658: undefined > reference to `std::__cxx11::basic_string<char, std::char_traits<char>, > std::allocator<char> >::_M_dispose()' > ninja: build stopped: subcommand failed. > make[1]: *** [Makefile:13: _build] Error 1 > make[1]: Leaving directory '/home/ubuntu/nuttx-apps/hellocpp' > make: *** [Makefile:25: default] Error 2 > ========= > > My CMakelists: > > === CMakeLists.txt === > cmake_minimum_required(VERSION 3.2...3.15) > > project(hellocpp > VERSION 1.0 > DESCRIPTION "Hello world C++ Nuttx" > ) > > set(CMAKE_CXX_STANDARD 17) > set(CMAKE_CXX_STANDARD_REQUIRED ON) > set(CMAKE_C_STANDARD 99) > > set(NUTTX_PATH "${CMAKE_SOURCE_DIR}/nuttx-export-9.1.0") > > include(cmake/phigw.cmake) > > set(AC_COMMON_FLAGS "${AC_COMMON_FLAGS} -fno-builtin -Wall -Wshadow > -Wundef -g") > > set(AC_DEFINES "${AC_DEFINES} -DCONFIG_WCHAR_BUILTIN") > > include_directories( > src > ${NUTTX_PATH}/include > ${NUTTX_PATH}/arch/chip > ) > > set(EXE_NAME hellocpp) > > set(CMAKE_CXX_FLAGS "${AC_HW_FLAGS} ${AC_DEFINES} ${AC_COMMON_FLAGS} > ${AC_CXX_EXTRA_FLAGS}") > if (PARAM_DEBUG) > set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os -g") > else() > set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Os") > endif() > > set(CMAKE_SKIP_RPATH ON) > set(CMAKE_CXX_LINK_EXECUTABLE "${CMAKE_LINKER} ${AC_LINKER_FLAGS} -o > ${EXE_NAME}.elf <OBJECTS> <LINK_LIBRARIES>") > > set(BUILD_SHARED_LIBS OFF) > > add_subdirectory(src) > ========= > > === src/CMakelists.txt === > set(HEADER_FILES > HelloWorld.h > Log.h > ) > > set(SOURCE_FILES > HelloWorld.cpp > Log.cpp > ) > > link_directories(${EXE_NAME} ${NUTTX_PATH}/libs) > > add_executable(${EXE_NAME} ${SOURCE_FILES} main.cpp ${HEADER_FILES}) > > add_custom_command( > TARGET ${EXE_NAME} > POST_BUILD > COMMAND ${CMAKE_OBJCOPY} ARGS -S -O binary > ${CMAKE_BINARY_DIR}/${EXE_NAME}.elf > ${CMAKE_BINARY_DIR}/${EXE_NAME}.bin > ) > > target_link_libraries(${EXE_NAME} --start-group) > target_link_libraries(${EXE_NAME} c) > target_link_libraries(${EXE_NAME} arch) > target_link_libraries(${EXE_NAME} binfmt) > target_link_libraries(${EXE_NAME} board) > target_link_libraries(${EXE_NAME} boards) > target_link_libraries(${EXE_NAME} drivers) > target_link_libraries(${EXE_NAME} fs) > target_link_libraries(${EXE_NAME} mm) > target_link_libraries(${EXE_NAME} sched) > target_link_libraries(${EXE_NAME} xx) > target_link_libraries(${EXE_NAME} net) > target_link_libraries(${EXE_NAME} supc++) > target_link_libraries(${EXE_NAME} > /usr/lib/gcc/arm-none-eabi/9.2.1/thumb/v7e-m/nofp/libgcc.a) > target_link_libraries(${EXE_NAME} --end-group) > ========= > > I am using Ubuntu 20.04 operating system and the toolchain provided > from the Ubuntu Repository. > > Best regards, > > Flavio > > > > > -- > Flavio de Castro Alves Filho > > flavio.al...@gmail.com > Twitter: http://twitter.com/#!/fraviofii > LinkedIn profile: www.linkedin.com/in/flaviocastroalves >