【解決方案】64位Linux系統下無法編譯、運行32位C、C++程序的解決辦法

【故障】

1. 從32位系統裏面複製過來的32位C或C++程序無法運行,提示找不到庫(比如libstdc++.so.6)
2. gcc或g++帶-m32選項編譯成32位的程序,提示失敗。例如:

[oct1158@oct1158-fedora clocktest]$ g++ p.cpp -m32 -o p
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-redhat-linux/8/libstdc++.so when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++

【解決方案】

安裝相同版本的i686平臺的libgcc和libstdc++。這裏所說的相同版本,是指和系統裏已有的x86_64平臺編譯器相同的版本。不相同的版本是不能共存的。

所以,安裝前,必須先將已有的64位libgcc和libstdc++升級到最新版本:

[oct1158@oct1158-fedora clocktest]$ sudo dnf upgrade libstdc++.x86_64
Last metadata expiration check: 0:06:54 ago on Fri 08 Mar 2019 14:01:57 CST.
Dependencies resolved.
================================================================================
 Package                Architecture  Version              Repository      Size
================================================================================
Upgrading:
 cpp                    x86_64        8.3.1-2.fc29         updates         10 M
 gcc                    x86_64        8.3.1-2.fc29         updates         23 M
 gcc-c++                x86_64        8.3.1-2.fc29         updates         12 M
 gcc-gdb-plugin         x86_64        8.3.1-2.fc29         updates        139 k
 libgcc                 i686          8.3.1-2.fc29         updates        107 k
 libgcc                 x86_64        8.3.1-2.fc29         updates        100 k
 libgomp                x86_64        8.3.1-2.fc29         updates        210 k
 libstdc++              x86_64        8.3.1-2.fc29         updates        460 k
 libstdc++-devel        x86_64        8.3.1-2.fc29         updates        2.1 M

Transaction Summary
================================================================================
Upgrade  9 Packages

Total download size: 48 M
Is this ok [y/N]: y
Downloading Packages:
(1/9): gcc-8.3.1-2.fc29.x86_64.rpm              1.7 MB/s |  23 MB     00:13    
(2/9): gcc-gdb-plugin-8.3.1-2.fc29.x86_64.rpm   114 kB/s | 139 kB     00:01    
(3/9): libgcc-8.3.1-2.fc29.i686.rpm             708 kB/s | 107 kB     00:00    
(4/9): libgcc-8.3.1-2.fc29.x86_64.rpm           658 kB/s | 100 kB     00:00    
(5/9): libgomp-8.3.1-2.fc29.x86_64.rpm          839 kB/s | 210 kB     00:00    
(6/9): libstdc++-8.3.1-2.fc29.x86_64.rpm        1.6 MB/s | 460 kB     00:00    
(7/9): libstdc++-devel-8.3.1-2.fc29.x86_64.rpm  2.2 MB/s | 2.1 MB     00:00    
(8/9): cpp-8.3.1-2.fc29.x86_64.rpm              286 kB/s |  10 MB     00:35    
(9/9): gcc-c++-8.3.1-2.fc29.x86_64.rpm          319 kB/s |  12 MB     00:37    
--------------------------------------------------------------------------------
Total                                           1.2 MB/s |  48 MB     00:40     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Upgrading        : libgcc-8.3.1-2.fc29.x86_64                            1/18 
  Running scriptlet: libgcc-8.3.1-2.fc29.x86_64                            1/18 
  Upgrading        : libstdc++-8.3.1-2.fc29.x86_64                         2/18 
  Running scriptlet: libstdc++-8.3.1-2.fc29.x86_64                         2/18 
  Upgrading        : libstdc++-devel-8.3.1-2.fc29.x86_64                   3/18 
  Upgrading        : libgomp-8.3.1-2.fc29.x86_64                           4/18 
  Running scriptlet: libgomp-8.3.1-2.fc29.x86_64                           4/18 
  Upgrading        : cpp-8.3.1-2.fc29.x86_64                               5/18 
  Running scriptlet: cpp-8.3.1-2.fc29.x86_64                               5/18 
  Upgrading        : gcc-8.3.1-2.fc29.x86_64                               6/18 
  Running scriptlet: gcc-8.3.1-2.fc29.x86_64                               6/18 
  Upgrading        : gcc-c++-8.3.1-2.fc29.x86_64                           7/18 
  Upgrading        : gcc-gdb-plugin-8.3.1-2.fc29.x86_64                    8/18 
  Running scriptlet: gcc-gdb-plugin-8.3.1-2.fc29.x86_64                    8/18 
  Upgrading        : libgcc-8.3.1-2.fc29.i686                              9/18 
  Running scriptlet: libgcc-8.3.1-2.fc29.i686                              9/18 
  Cleanup          : gcc-gdb-plugin-8.2.1-6.fc29.x86_64                   10/18 
  Running scriptlet: gcc-gdb-plugin-8.2.1-6.fc29.x86_64                   10/18 
  Cleanup          : gcc-c++-8.2.1-6.fc29.x86_64                          11/18 
  Cleanup          : libstdc++-devel-8.2.1-6.fc29.x86_64                  12/18 
  Cleanup          : libgcc-8.2.1-6.fc29.i686                             13/18 
  Running scriptlet: libgcc-8.2.1-6.fc29.i686                             13/18 
  Running scriptlet: gcc-8.2.1-6.fc29.x86_64                              14/18 
  Cleanup          : gcc-8.2.1-6.fc29.x86_64                              14/18 
  Cleanup          : libstdc++-8.2.1-6.fc29.x86_64                        15/18 
  Running scriptlet: libstdc++-8.2.1-6.fc29.x86_64                        15/18 
  Cleanup          : libgcc-8.2.1-6.fc29.x86_64                           16/18 
  Running scriptlet: libgcc-8.2.1-6.fc29.x86_64                           16/18 
  Running scriptlet: cpp-8.2.1-6.fc29.x86_64                              17/18 
  Cleanup          : cpp-8.2.1-6.fc29.x86_64                              17/18 
  Running scriptlet: libgomp-8.2.1-6.fc29.x86_64                          18/18 
  Cleanup          : libgomp-8.2.1-6.fc29.x86_64                          18/18 
  Running scriptlet: libgomp-8.2.1-6.fc29.x86_64                          18/18 
  Verifying        : cpp-8.3.1-2.fc29.x86_64                               1/18 
  Verifying        : cpp-8.2.1-6.fc29.x86_64                               2/18 
  Verifying        : gcc-8.3.1-2.fc29.x86_64                               3/18 
  Verifying        : gcc-8.2.1-6.fc29.x86_64                               4/18 
  Verifying        : gcc-c++-8.3.1-2.fc29.x86_64                           5/18 
  Verifying        : gcc-c++-8.2.1-6.fc29.x86_64                           6/18 
  Verifying        : gcc-gdb-plugin-8.3.1-2.fc29.x86_64                    7/18 
  Verifying        : gcc-gdb-plugin-8.2.1-6.fc29.x86_64                    8/18 
  Verifying        : libgcc-8.3.1-2.fc29.i686                              9/18 
  Verifying        : libgcc-8.2.1-6.fc29.i686                             10/18 
  Verifying        : libgcc-8.3.1-2.fc29.x86_64                           11/18 
  Verifying        : libgcc-8.2.1-6.fc29.x86_64                           12/18 
  Verifying        : libgomp-8.3.1-2.fc29.x86_64                          13/18 
  Verifying        : libgomp-8.2.1-6.fc29.x86_64                          14/18 
  Verifying        : libstdc++-8.3.1-2.fc29.x86_64                        15/18 
  Verifying        : libstdc++-8.2.1-6.fc29.x86_64                        16/18 
  Verifying        : libstdc++-devel-8.3.1-2.fc29.x86_64                  17/18 
  Verifying        : libstdc++-devel-8.2.1-6.fc29.x86_64                  18/18 

Upgraded:
  cpp-8.3.1-2.fc29.x86_64                 gcc-8.3.1-2.fc29.x86_64               
  gcc-c++-8.3.1-2.fc29.x86_64             gcc-gdb-plugin-8.3.1-2.fc29.x86_64    
  libgcc-8.3.1-2.fc29.i686                libgcc-8.3.1-2.fc29.x86_64            
  libgomp-8.3.1-2.fc29.x86_64             libstdc++-8.3.1-2.fc29.x86_64         
  libstdc++-devel-8.3.1-2.fc29.x86_64    

Complete!
[oct1158@oct1158-fedora clocktest]$

然後安裝glibc.i686:

[oct1158@oct1158-fedora clocktest]$ sudo dnf install glibc.i686
[sudo] password for oct1158: 
Last metadata expiration check: 0:21:23 ago on Fri 08 Mar 2019 14:01:57 CST.
Package glibc-2.28-26.fc29.i686 is already installed.
Dependencies resolved.
Nothing to do.
Complete!

和libstdc++.i686:

[oct1158@oct1158-fedora clocktest]$ sudo dnf install libstdc++.i686
Last metadata expiration check: 0:08:24 ago on Fri 08 Mar 2019 14:01:57 CST.
Dependencies resolved.
================================================================================
 Package            Architecture  Version                Repository        Size
================================================================================
Installing:
 libstdc++          i686          8.3.1-2.fc29           updates          497 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 497 k
Installed size: 1.7 M
Is this ok [y/N]: y
Downloading Packages:
libstdc++-8.3.1-2.fc29.i686.rpm                 254 kB/s | 497 kB     00:01    
--------------------------------------------------------------------------------
Total                                           105 kB/s | 497 kB     00:04     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : libstdc++-8.3.1-2.fc29.i686                            1/1 
  Running scriptlet: libstdc++-8.3.1-2.fc29.i686                            1/1 
  Verifying        : libstdc++-8.3.1-2.fc29.i686                            1/1 

Installed:
  libstdc++-8.3.1-2.fc29.i686                                                   

Complete!
[oct1158@oct1158-fedora clocktest]$ 

可以用下面的命令查看已安裝的版本:

[oct1158@oct1158-fedora clocktest]$ sudo dnf install glibc libstdc++
Last metadata expiration check: 0:23:33 ago on Fri 08 Mar 2019 14:01:57 CST.
Package glibc-2.28-26.fc29.x86_64 is already installed.
Package glibc-2.28-26.fc29.i686 is already installed.
Package libstdc++-8.3.1-2.fc29.x86_64 is already installed.
Package libstdc++-8.3.1-2.fc29.i686 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[oct1158@oct1158-fedora clocktest]$ 

32位和64位的版本必須相同才能安裝,切記!

 

接下來,編譯一個C++程序測試一下:

#include <iostream>

using namespace std;

class A
{
private:
	const char *p;
public:
	A(const char *s);
	void test(void);
};

A::A(const char *s) : p(s)
{
}

void A::test(void)
{
	cout << p << endl;
}

int main(void)
{
	A *a = new A("Hello World!");
	a->test();
	delete a;
	return 0;
}
[oct1158@oct1158-fedora clocktest]$ g++ p.cpp -m32 -o p
[oct1158@oct1158-fedora clocktest]$ ./p
Hello World!
[oct1158@oct1158-fedora clocktest]$ file p
p: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=8cf224918f0a6259f5aea2411ec973a2234a1b02, not stripped
[oct1158@oct1158-fedora clocktest]$ 

這一次編譯就成功了

 

[oct1158@oct1158-fedora 4.9.1]$ ./cc1
./cc1: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory

找不到libz.so.1的問題也可以用同樣的方法解決。

[oct1158@oct1158-fedora 4.9.1]$ sudo dnf upgrade zlib
Last metadata expiration check: 0:04:39 ago on Fri 08 Mar 2019 14:34:16 CST.
Dependencies resolved.
Nothing to do.
Complete!
[oct1158@oct1158-fedora 4.9.1]$ sudo dnf install zlib.i686
Last metadata expiration check: 0:04:52 ago on Fri 08 Mar 2019 14:34:16 CST.
Dependencies resolved.
================================================================================
 Package        Architecture   Version                   Repository        Size
================================================================================
Installing:
 zlib           i686           1.2.11-14.fc29            fedora            91 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 91 k
Installed size: 201 k
Is this ok [y/N]: y
Downloading Packages:
zlib-1.2.11-14.fc29.i686.rpm                     65 kB/s |  91 kB     00:01    
--------------------------------------------------------------------------------
Total                                            23 kB/s |  91 kB     00:03     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1 
  Installing       : zlib-1.2.11-14.fc29.i686                               1/1 
  Running scriptlet: zlib-1.2.11-14.fc29.i686                               1/1 
  Verifying        : zlib-1.2.11-14.fc29.i686                               1/1 

Installed:
  zlib-1.2.11-14.fc29.i686                                                      

Complete!
[oct1158@oct1158-fedora 4.9.1]$ ./cc1

^C
[oct1158@oct1158-fedora 4.9.1]$ 

裝了zlib.i686就可以運行了cc1程序了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章