rt-thread支持标准的c++库吗?

发布于 2020-09-30 09:54:10    浏览:598

■目的:
尝试运行c++的动态模块(.mo)

■环境:
硬件:cortex-a7
编译环境:ubantu 16.04

     arm-none-eabi-g++(6.3.1)

编译器下载路径:

https://developer.arm.com/-/media/Files/downloads/gnu-rm/6-    2017q2/gcc-arm-none-eabi-6-2017-q2-update-linux.tar.bz2

■问题:

  1. 我想引用 c++的 iostream标准库,并且打印个简单的helloworld(cout<<"hello")但是运行不起来,后来尝试不引用 c++标准库iostream就可以运行(不引用c++的任何库,简单的创建object等)。所以想问下 rt-thread目前支持c++标准库吗?
  2. 目前rt-thread对于C++的支持有详细的说明文档吗?(只在cplusplus下的README看到了信息,但是没有说明是否支持标准库)
  3. 如果支持c++标准库的话,麻烦大神帮忙分析一下我这边是不是哪里尝试的不对,具体尝试步骤如下:

■尝试步骤

步骤一:利用 https://github.com/RT-Thread/rtthread-apps 提供的demo。
然后将 rtthread-apps/hello 目录下的文件 main.c替换为main.cpp,填入代码如下:

#include <iostream>
using namespace std;

void *__dso_handle = 0;//后面会提到

int main()
{
        cout<<"hello world"<<endl;
    return 0;
}

步骤二:修改编译参数:
① rtconfig.h 加入 #define RT_USING_LIBC
② rtconfig.py 加入

M_CFLAGS = CFLAGS + ' -mlong-calls  -fPIC '
M_CXXFLAGS = CXXFLAGS + ' -mlong-calls  -fPIC '
M_LFLAGS = DEVICE + CXXFLAGS + ' -Wl,--gc-sections,-z,max-page-size=0x4' +\
                                '   -shared -fPIC -nostartfiles -nostdlib -static-libgcc'
M_POST_ACTION = STRIP + ' -R .hash $TARGET\n' + SIZE + ' $TARGET \n'

②连接脚本link.lds 加入

    __exidx_start = .;
    .ARM.exidx   :
    {
        *(.ARM.exidx* .gnu.linkonce.armexidx.*) 
        _sidata = .;
    }
    __exidx_end = .;

    __rodata_start = .;
    .rodata   : { *(.rodata) *(.rodata.*) }
    __rodata_end = .;

    . = ALIGN(4);
    .ctors :
    {
        PROVIDE(__ctors_start__ = .);
        KEEP(*(SORT(.ctors.*)))
        KEEP(*(.ctors))
        KEEP (*(SORT(.init_array.*)))
        KEEP (*(.init_array))
        PROVIDE(__ctors_end__ = .);
    }
   
    . = ALIGN(4);

    .dtors :
    {
        PROVIDE(__dtors_start__ = .);
        KEEP(*(SORT(.dtors.*)))
        KEEP(*(.dtors))
        PROVIDE(__dtors_end__ = .);
    }
    . = ALIGN(4);

步骤三:编译
1.scons --target=ua -s
2.在 rtthread-apps 目录下执行:scons --app=hello --verbose

arm-none-eabi-g++ -o hello/hello.mo -mcpu=cortex-a7 -ffunction-sections -fdata-sections -Iinclude -mcpu=cortex-a7 -ffunction-sections -fdata-sections -Iinclude -Wall -O2 -Wl,--gc-sections,-z,max-page-size=0x4 -shared -fPIC -nostartfiles -nostdlib -static-libgcc -e main hello/main.o
hello/main.o: In function `_GLOBAL__sub_I_main.cpp':
main.cpp:(.text.startup._GLOBAL__sub_I_main.cpp+0x5c): undefined reference to `__dso_handle'
/opt/gcc-arm-none-eabi-6-2017-q2-update/bin/../lib/gcc/arm-none-eabi/6.3.1/../../../../arm-none-eabi/bin/ld: hello/hello.mo: hidden symbol `__dso_handle' isn't defined
/opt/gcc-arm-none-eabi-6-2017-q2-update/bin/../lib/gcc/arm-none-eabi/6.3.1/../../../../arm-none-eabi/bin/ld: final link failed: Bad value

3.为了修复上面错误,所以在代码中加入:void *__dso_handle = 0(见步骤一);
4.再编译可通过

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
CXX hello/main.o
LINK hello/hello.mo
arm-none-eabi-strip -R .hash hello/hello.mo
arm-none-eabi-size hello/hello.mo
   text       data        bss        dec        hex    filename
    909        168          8       1085        43d    hello/hello.mo
scons: done building targets.

步骤四:运行
将编译好的hello.mo 放入板子中运行报错:

msh />hello
[E/DLMD] Module: can't find _ZSt16__ostream_insertIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_PKS3_i in kernel symbol table
[E/DLMD] Module: can't find _ZNSt8ios_base4InitC1Ev in kernel symbol table
[E/DLMD] Module: can't find _ZNSt8ios_base4InitD1Ev in kernel symbol table
[E/DLMD] Module: can't find _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_ in kernel symbol table
[E/DLMD] Module: can't find _ZSt4cout in kernel symbol table
[E/DLMD] Module: can't find __aeabi_atexit in kernel symbol table

查看更多

bernard
bernard 2020-09-30
This guy hasn't written anything yet

动态模块是另外一回事,很复杂,涉及到系列的符号问题。

建议未来向rt-smart方向转换

1 个回答

撰写答案

请登录后再发布答案,点击登录
关注者
0
被浏览
598

发布
问题

分享
好友

手机
浏览

扫码手机浏览