【问题标题】:/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a when searching for -lstdc++ /usr/bin/ld: cannot find -lstdc++/usr/bin/ld: 搜索 -lstdc++ 时跳过不兼容的 /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a /usr/bin/ld: 找不到 -lstdc++
【发布时间】:2020-06-18 13:37:47
【问题描述】:

为什么会出现这个错误?

g++ -m32 func.o test.o -o prgram

/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/9/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status

我的代码如下:

#include<stdio.h>
  2 
  3 
  4 extern "C" int calcsum(int a, int b, int c);
  5 
  6 int main(int argc,char* argv[])
  7 {
  8     int a = 10;
  9     int b = 20;
 10     int c = 30;
 11     int sum = calcsum(a,b,c);
 12 
 13     printf("a: %d", a);
 14     printf("b: %d", b);
 15     printf("c: %d", c);
 16     printf("sum: %d", sum);
 17     return 0;
 18 }
 19 
 1 section .text
  2     global _start
  3 _start:
  4 global _calcsum
  5 _calcsum:
  6 
  7     push ebp
  8     mov ebp, esp
  9 
 10     mov eax, [ebp+8]
 11     mov ecx, [ebp+12]
 12     mov edx, [ebp+16]
 13 
 14     add eax, ecx
 15     add eax, edx
 16 
 17     pop ebp
 18     ret

【问题讨论】:

    标签: linux assembly linker g++ 32bit-64bit


    【解决方案1】:

    您正在 64 位系统上构建 32 位二进制文​​件(因为您使用了 -m32 标志),但尚未安装 32 位版本的开发库。

    如果您使用的是 Ubuntu,请尝试:

    sudo apt install gcc-multilib g++-multilib libc6-dev-i386
    

    至于您的程序集文件,它不能按原样工作。 start 将由 cpp 文件定义,因此将其从 asm 文件中删除。接下来,删除_calcsum 的下划线。构建 ELF 二进制文件时,函数名称不以下划线开头:

    section .text
    global calcsum
    calcsum:
    
        push ebp
        mov ebp, esp
    
        mov eax, [ebp+8]
        mov ecx, [ebp+12]
        mov edx, [ebp+16]
    
        add eax, ecx
        add eax, edx
    
        pop ebp
        ret
    

    将其构建为 32 位 ELF 对象文件:

    nasm -felf32 func.s -o func.o
    

    【讨论】:

    • 我现在得到未定义的“calcsum”引用错误。我的汇编代码如下:
    • 1 section .text 2 global calcsum 3 calcsum 4 5 push ebp 6 mov ebp, esp 7 8 mov eax, [ebp+8] 9 mov ecx, [ebp+12] 10 mov edx, [ ebp+16] 11 12 add eax, ecx 13 add eax, edx 14 15 pop ebp 16 ret 17 ~ ~
    • @Hex 再次更新答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2019-02-23
    • 1970-01-01
    • 2021-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多