【问题标题】:RISC-V undefined reference to `memcpy'RISC-V 未定义对“memcpy”的引用
【发布时间】:2020-12-28 02:08:47
【问题描述】:

我正在使用 RISC-V 32E 工具链来编译一些独立的 CPP 代码。我在下面遇到错误。

/opt/riscv32i/bin/riscv32-unknown-elf-g++ -Os -ffreestanding -o firmware/firmware.elf \
    -Wl,-Bstatic,-T,firmware/sections.lds,-Map,firmware/firmware.map,--strip-debug \
    firmware/start.o firmware/print.o  firmware/stream.o firmware/main.o firmware/accel.o firmware/data_redir_m.o  -lgcc -fno-threadsafe-statics -nostdlib 
/opt/riscv32i/lib/gcc/riscv32-unknown-elf/8.2.0/../../../../riscv32-unknown-elf/bin/ld: firmware/data_redir_m.o: in function `.L31':
data_redir_m.cpp:(.text+0x442): undefined reference to `memcpy'
collect2: error: ld returned 1 exit status
Makefile:46: recipe for target 'firmware/firmware.elf' failed
make: *** [firmware/firmware.elf] Error 1

我希望 hex 文件尽可能小,所以我的偏好是不使用 -lstdc++ 库。让我很困惑的是为什么即使我在 data_redir_m.cpp 文件中手动添加 memcpy() 函数定义,它仍然会抱怨此类错误。

#include "typedefs.h"

void * memcpy ( void * destination, const void * source, int num ){
  int i=0;
  *((int*)destination) = *((int*)source);
}

static int check_clockwise( Triangle_2D triangle_2d )
{
  int cw;

  cw = (triangle_2d.x2 - triangle_2d.x0) * (triangle_2d.y1 - triangle_2d.y0)
       - (triangle_2d.y2 - triangle_2d.y0) * (triangle_2d.x1 - triangle_2d.x0);

  return cw;

}

// swap (x0, y0) (x1, y1) of a Triangle_2D
static void clockwise_vertices( Triangle_2D *triangle_2d )
{

  bit8 tmp_x, tmp_y;

  tmp_x = triangle_2d->x0;
  tmp_y = triangle_2d->y0;

  triangle_2d->x0 = triangle_2d->x1;
  triangle_2d->y0 = triangle_2d->y1;

  triangle_2d->x1 = tmp_x;
  triangle_2d->y1 = tmp_y;

}



// find the min from 3 integers
static bit8 find_min( bit8 in0, bit8 in1, bit8 in2 )
{
  if (in0 < in1)
  {
    if (in0 < in2)
      return in0;
    else
      return in2;
  }
  else
  {
    if (in1 < in2)
      return in1;
    else
      return in2;
  }
}


// find the max from 3 integers
static bit8 find_max( bit8 in0, bit8 in1, bit8 in2 )
{
  if (in0 > in1)
  {
    if (in0 > in2)
      return in0;
    else
      return in2;
  }
  else
  {
    if (in1 > in2)
      return in1;
    else
      return in2;
  }
}


// project a 3D triangle to a 2D triangle
void projection(
        bit32 input_lo,
        bit32 input_mi,
        bit32 input_hi,
        Triangle_2D *triangle_2d
        )
{
  #pragma HLS INLINE off
  Triangle_3D triangle_3d;
  // Setting camera to (0,0,-1), the canvas at z=0 plane
  // The 3D model lies in z>0 space
  // The coordinate on canvas is proportional to the corresponding coordinate
  // on space

    bit2 angle = 0;
    triangle_3d.x0 = bit8(input_lo( 7,  0));
    triangle_3d.y0 = bit8(input_lo(15,  8));
    triangle_3d.z0 = bit8(input_lo(23, 16));
    triangle_3d.x1 = bit8(input_lo(31, 24));
    triangle_3d.y1 = bit8(input_mi( 7,  0));
    triangle_3d.z1 = bit8(input_mi(15,  8));
    triangle_3d.x2 = bit8(input_mi(23, 16));
    triangle_3d.y2 = bit8(input_mi(31, 24));
    triangle_3d.z2 = bit8(input_hi( 7,  0));

  if(angle == 0)
  {
    triangle_2d->x0 = triangle_3d.x0;
    triangle_2d->y0 = triangle_3d.y0;
    triangle_2d->x1 = triangle_3d.x1;
    triangle_2d->y1 = triangle_3d.y1;
    triangle_2d->x2 = triangle_3d.x2;
    triangle_2d->y2 = triangle_3d.y2;
    triangle_2d->z  = triangle_3d.z0 / 3 + triangle_3d.z1 / 3 + triangle_3d.z2 / 3;
  }

  else if(angle == 1)
  {
    triangle_2d->x0 = triangle_3d.x0;
    triangle_2d->y0 = triangle_3d.z0;
    triangle_2d->x1 = triangle_3d.x1;
    triangle_2d->y1 = triangle_3d.z1;
    triangle_2d->x2 = triangle_3d.x2;
    triangle_2d->y2 = triangle_3d.z2;
    triangle_2d->z  = triangle_3d.y0 / 3 + triangle_3d.y1 / 3 + triangle_3d.y2 / 3;
  }

  else if(angle == 2)
  {
    triangle_2d->x0 = triangle_3d.z0;
    triangle_2d->y0 = triangle_3d.y0;
    triangle_2d->x1 = triangle_3d.z1;
    triangle_2d->y1 = triangle_3d.y1;
    triangle_2d->x2 = triangle_3d.z2;
    triangle_2d->y2 = triangle_3d.y2;
    triangle_2d->z  = triangle_3d.x0 / 3 + triangle_3d.x1 / 3 + triangle_3d.x2 / 3;
  }

}



// calculate bounding box for a 2D triangle
void rasterization1 (
        Triangle_2D triangle_2d,
        hls::stream<ap_uint<32> > & Output_1,
        hls::stream<ap_uint<32> > & Output_2
        )
{
    Triangle_2D triangle_2d_same;
    bit8 max_min[5];
    max_min[0]=0;
    max_min[1]=0;
    max_min[2]=0;
    max_min[3]=0;
    max_min[4]=0;
    bit16 max_index[1];
    max_index[0]=0;
    bit32 tmp1, tmp2, tmp3, tmp4;
    static int parity = 0;
  #pragma HLS INLINE off
  // clockwise the vertices of input 2d triangle
  if ( check_clockwise( triangle_2d ) == 0 ){

    tmp1(7,0) = 1;
    tmp1(15, 8) = triangle_2d_same.x0;
    tmp1(23,16) = triangle_2d_same.y0;
    tmp1(31,24) = triangle_2d_same.x1;
    tmp2(7,0) = triangle_2d_same.y1;
    tmp2(15, 8) = triangle_2d_same.x2;
    tmp2(23,16) = triangle_2d_same.y2;
    tmp2(31,24) = triangle_2d_same.z;

    tmp3(15,0) = max_index[0];
    tmp3(23,16) = max_min[0];
    tmp3(31,24) = max_min[1];

    tmp4(7,0) = max_min[2];
    tmp4(15, 8) = max_min[3];
    tmp4(23,16) = max_min[4];
    tmp4(31,24) = 0;
    if(parity==0){
        Output_1.write(tmp1);
        Output_1.write(tmp2);
        Output_1.write(tmp3);
        Output_1.write(tmp4);
        parity = 1;
    }else{
        Output_2.write(tmp1);
        Output_2.write(tmp2);
        Output_2.write(tmp3);
        Output_2.write(tmp4);
        parity = 0;
    }
#ifdef PROFILE
  data_redir_m_out_1+=4;
#endif

    return;
  }
  if ( check_clockwise( triangle_2d ) < 0 )
    clockwise_vertices( &triangle_2d );




  // copy the same 2D triangle
  triangle_2d_same.x0 = triangle_2d.x0;
  triangle_2d_same.y0 = triangle_2d.y0;
  triangle_2d_same.x1 = triangle_2d.x1;
  triangle_2d_same.y1 = triangle_2d.y1;
  triangle_2d_same.x2 = triangle_2d.x2;
  triangle_2d_same.y2 = triangle_2d.y2;
  triangle_2d_same.z  = triangle_2d.z ;

  // find the rectangle bounds of 2D triangles
  max_min[0] = find_min( triangle_2d.x0, triangle_2d.x1, triangle_2d.x2 );
  max_min[1] = find_max( triangle_2d.x0, triangle_2d.x1, triangle_2d.x2 );
  max_min[2] = find_min( triangle_2d.y0, triangle_2d.y1, triangle_2d.y2 );
  max_min[3] = find_max( triangle_2d.y0, triangle_2d.y1, triangle_2d.y2 );
  max_min[4] = max_min[1] - max_min[0];

  // calculate index for searching pixels
  max_index[0] = (max_min[1] - max_min[0]) * (max_min[3] - max_min[2]);

  tmp1(7,0) = 0;
  tmp1(15,8) = triangle_2d_same.x0;
  tmp1(23,16) = triangle_2d_same.y0;
  tmp1(31,24) = triangle_2d_same.x1;

  tmp2(7,0) = triangle_2d_same.y1;
  tmp2(15,8) = triangle_2d_same.x2;
  tmp2(23,16) = triangle_2d_same.y2;
  tmp2(31,24) = triangle_2d_same.z;

  tmp3(15,0) = max_index[0];
  tmp3(23,16) = max_min[0];
  tmp3(31,24) = max_min[1];

  tmp4(7,0) = max_min[2];
  tmp4(15,8) = max_min[3];
  tmp4(23, 16) = max_min[4];
  tmp4(31, 24) = 0;

  if(parity==0){
    Output_1.write(tmp1);
    Output_1.write(tmp2);
    Output_1.write(tmp3);
    Output_1.write(tmp4);
    parity = 1;
  }else{
    Output_2.write(tmp1);
    Output_2.write(tmp2);
    Output_2.write(tmp3);
    Output_2.write(tmp4);
    parity = 0;
  }
  return;
}


void data_redir_m (
        hls::stream<ap_uint<32> > & Input_1,
        hls::stream<ap_uint<32> > & Output_1,
        hls::stream<ap_uint<32> > & Output_2
        )
{
#pragma HLS INTERFACE ap_hs port=Input_1
#pragma HLS INTERFACE ap_hs port=Output_1
#pragma HLS INTERFACE ap_hs port=Output_2

  bit32 input_lo;
  bit32 input_mi;
  bit32 input_hi;
  bit128 input_tmp;

  hls::stream<ap_uint<32> > Output_1_1;
  hls::stream<ap_uint<32> > Output_2_2;
  Triangle_2D triangle_2ds_1;
  Triangle_2D triangle_2ds_2;



  input_lo = Input_1.read();
  input_mi = Input_1.read();
  input_hi = Input_1.read();
#ifdef PROFILE
  data_redir_m_in_1+=3;
#endif

  projection (input_lo,input_mi,input_hi,&triangle_2ds_1);
  rasterization1 (triangle_2ds_1, Output_1, Output_2);

}

【问题讨论】:

  • 是链接订单的事情吗? IE。能不能把 memcpy 的实现提早一点?
  • 我把 memcpy() 定义放在 'typedef.h' 文件中,得到了同样的错误。
  • 对不起,如果我不清楚,但我的意思是在命令行上的早期.o文件中实现该功能:firmware/start.ofirmware/print.ofirmware/stream.ofirmware/ main.o 固件/accel.o 固件/data_redir_m.o.另一个想法是您使用 g++,但这是一个 c 符号。编译器会破坏它吗?
  • 我不知道,但我希望 memcpy 在 libc 而不是 libstdc++ 中。

标签: memcpy riscv


【解决方案1】:

如果要为非os环境构建gcc,需要提前构建newlib。并告诉gcc去哪里找libc,方法是在配置项目的时候声明sysroot。

您可以使用此脚本(ian910297/build-riscv-gnu-toolchain) 来构建 riscv gnu 工具链。如果您选择此方法,请注意目录名称。

否则,riscv官方也提供了类似的脚本来构建,比如:riscv/riscv-gnu-toolchain)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 2015-07-17
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多