现有如下C++测试程序:

#include <cstdio>

int main(void){
    printf("1\n");
    return 0;
}

我们想把它用riscv交叉编译器编译,有两种编译器可以选择:riscv64-unknown-elf-g++和riscv64-unknown-linux-gnu-g++

问题

如果使用的编译命令是:

riscv64-unknown-linux-gnu-g++ -o lib/test test.cpp
spike pk lib/test

就会报如下错误:

bbl loader
z  0000000000000000 ra 00000000000104dc sp 000000007f7e9b50 gp 0000000000012800
tp 0000000000000000 t0 0000000000000000 t1 0000000000000000 t2 0000000000012470
s0 0000000000000000 s1 0000000000000000 a0 0000000000010554 a1 0000000000000001
a2 000000007f7e9b58 a3 0000000000010572 a4 00000000000105ca a5 0000000000000000
a6 000000007f7e9b50 a7 0000000000000000 s2 0000000000000000 s3 0000000000000000
s4 0000000000000000 s5 0000000000000000 s6 0000000000000000 s7 0000000000000000
s8 0000000000000000 s9 0000000000000000 sA 0000000000000000 sB 0000000000000000
t3 ffffffffffffffff t4 0000000000000000 t5 0000000000000000 t6 0000000000000000
pc fffffffffffffffe va fffffffffffffffe insn       ffffffff sr 8000000200046020
User fetch segfault @ 0xfffffffffffffffe

解决方法

将编译命令后加上-static,选择静态编译

riscv64-unknown-linux-gnu-g++ -o lib/test test.cpp -static

运行:

spike pk lib/test

结果:

bbl loader
1

如果使用riscv64-unknown-elf-g++

输入的命令:

riscv64-unknown-elf-g++ -o lib/test test.cpp
spike pk lib/test

就不需要选择静态编译,输出结果为:

bbl loader
1

这是因为在riscv64-unknown-elf-g++编译器中默认就是静态编译,但是在riscv64-unknown-linux-gnu-g++编译器中需要手动指定

如果不加pk

因为printf是系统调用命令,如果不加pk的话就会报错,错误如下:

$ spike lib/test
ERROR: invalid load from debug module: 8 bytes at 0x0000000000012610
terminate called after throwing an instance of 'trap_load_access_fault'
Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐