【Linux】Linux64位环境下编译32位报错skipping incompatible的解决办法

本文首发于 ❄️慕雪的寒舍

问题

如题,当我尝试在wsl2的ubuntu中使用-m32选项编译32位程序的时候,出现了下面的两种报错

❯ g++ -m32 test.cpp -o test1 && ./test1
In file included from test.cpp:1:
/usr/include/stdio.h:27:10: fatal error: bits/libc-header-start.h: No such file or directory
   27 | #include <bits/libc-header-start.h>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
❯ g++ -m32 test.cpp -o test1 && ./test1
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libstdc++.so when searching for -lstdc++
collect2: error: ld returned 1 exit status

解决

原因是当前缺少32位的开发库,需要安装

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

安装后重试,编译成功。

❯ g++ -m32  test.cpp -o test1 && ./test1
4

代码很简单,是一个打印指针大小的代码,在32位下指针大小是4,64位下指针大小是8;

#include <stdio.h>

int main()
{
    void * ptr= nullptr;
    printf("%d\n",sizeof(ptr));
    return 0;
}

更多说明

在linux下可以使用下面的命令查看你的系统位数。

❯ getconf LONG_BIT
64

参考文章:assembly - /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++ - Stack Overflow

相关推荐

  1. windows MinGW,TDM-GCC 编译6432应用

    2024-04-02 20:12:03       32 阅读
  2. [环境配置]conda 64安装32python

    2024-04-02 20:12:03       35 阅读
  3. 32QT连接64MySQL

    2024-04-02 20:12:03       36 阅读
  4. 【QT】跨平台区分3264

    2024-04-02 20:12:03       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-02 20:12:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-02 20:12:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-02 20:12:03       82 阅读
  4. Python语言-面向对象

    2024-04-02 20:12:03       91 阅读

热门阅读

  1. 设计之魅:高质量面向对象设计的秘密

    2024-04-02 20:12:03       39 阅读
  2. Redis

    Redis

    2024-04-02 20:12:03      58 阅读
  3. Python零基础教学(数据类型)

    2024-04-02 20:12:03       32 阅读
  4. 在Linux中部署redis

    2024-04-02 20:12:03       41 阅读
  5. Jackson 2.x 系列【5】注解大全篇一

    2024-04-02 20:12:03       49 阅读
  6. C语言中,`while` 和 `for` 的区别

    2024-04-02 20:12:03       36 阅读
  7. 把本地项目上传到gitee上

    2024-04-02 20:12:03       39 阅读
  8. Debian 配置国内软件源

    2024-04-02 20:12:03       39 阅读
  9. Debian/Ubuntu安装ping和netstat命令

    2024-04-02 20:12:03       42 阅读
  10. 【python】dict转json存入sql

    2024-04-02 20:12:03       43 阅读
  11. 【算法集训】基础算法:双指针

    2024-04-02 20:12:03       38 阅读
  12. 李白打酒加强版(c++实现)

    2024-04-02 20:12:03       28 阅读