在不同的 gcc 中构建和 运行
buinding and running in different gcc
- 环境A:centos7(相同os) / gcc.7.3.1(更高的gcc)
- 环境B:centos7(同os) / gcc.4.8.5(下gcc)
我在环境 A 中构建了一个 C++ 可执行文件,运行 在环境 B 中构建了它。
到目前为止我还没有遇到问题,但是这种方法会不会有问题?
从技术上讲,构建静态可执行文件可提高可移植性。
因此,使用 g++ -O -Wall -Wextra -g
编译 C++ 文件,使用 g++ -static *.o
编译目标文件 link
或者,仅动态使用 libc.so
,而不是 C++ 标准库。所以 link 与 g++ *.o -Bstatic
你的图书馆 -Bdynamic -lc
有关详细信息,请阅读 GCC, of GNU binutils, perhaps of GNU bash, and the Program Library Howto and Drepper's paper How to write shared libraries
的文档
请注意,对于 运行 可执行文件(参见 elf(5) and execve(2) and other syscalls(2)...),您不需要编译器。
您可能想在机器 B 上使用 strace(1) and ldd(1)。
别忘了有很多测试用例。
请注意 GCC、Bash、Binutils 是 free software. You are allowed to download and study their source code and improve it (there are licensing conditions when you redistribute an improved binary, read their GPL license)
您可能遇到法律或许可问题(例如,如果您的 C++ 代码使用 Qt)。您可能需要咨询律师。
另见 LinuxFromScratch。
- 环境A:centos7(相同os) / gcc.7.3.1(更高的gcc)
- 环境B:centos7(同os) / gcc.4.8.5(下gcc)
我在环境 A 中构建了一个 C++ 可执行文件,运行 在环境 B 中构建了它。 到目前为止我还没有遇到问题,但是这种方法会不会有问题?
从技术上讲,构建静态可执行文件可提高可移植性。
因此,使用 g++ -O -Wall -Wextra -g
编译 C++ 文件,使用 g++ -static *.o
或者,仅动态使用 libc.so
,而不是 C++ 标准库。所以 link 与 g++ *.o -Bstatic
你的图书馆 -Bdynamic -lc
有关详细信息,请阅读 GCC, of GNU binutils, perhaps of GNU bash, and the Program Library Howto and Drepper's paper How to write shared libraries
的文档请注意,对于 运行 可执行文件(参见 elf(5) and execve(2) and other syscalls(2)...),您不需要编译器。
您可能想在机器 B 上使用 strace(1) and ldd(1)。
别忘了有很多测试用例。
请注意 GCC、Bash、Binutils 是 free software. You are allowed to download and study their source code and improve it (there are licensing conditions when you redistribute an improved binary, read their GPL license)
您可能遇到法律或许可问题(例如,如果您的 C++ 代码使用 Qt)。您可能需要咨询律师。
另见 LinuxFromScratch。