我正在为一个项目编写一个 C++ 库。它基本上包含一个源文件。我在 Mac 上编写了源文件并使用 g++ -c source.cpp -o source.o 编译它并使用 ar rcs libmylib.a source.o 创建了一个静态库。还有一个定义了几个函数接口(interface)的头文件。

我可以在我自己的 Mac 上使用这个库编译程序,通过发出 g++ myprogram.cpp -o myprogram -lmylib -L . 并且它可以编译和运行而没有任何错误。但是,当我简单地将静态库复制到服务器并尝试使用该库编译一些代码时,它不起作用。编译器提示某些函数的定义是 而不是 。显然链接器成功找到了库,但它根本找不到定义。

然后我尝试在服务器上编译库源文件并在服务器上编译我的项目。一切正常。但是当我将在服务器上编译的库复制到我的 Mac 并尝试使用该库编译我的本地测试程序时,编译器提示 ld: warning: ignoring file ./libmylib.a, file was built for archive which is not the architecture being linked (x86_64): ./libmylib.a 并且它拒绝链接。

让我困惑的是,我的 Mac 和服务器的 架构 应该是相同的(又名 x86_64 ),但是在这两台机器上编译的相同源文件不能互换使用。这可能是什么原因?库不应该在基于相同架构的机器上兼容吗?

有关系统和编译器的更多详细信息:

我的 Mac:

AdvancedMage-3:encryption Andy$ uname -a
Darwin AdvancedMage-3.local 15.3.0 Darwin Kernel Version 15.3.0: Thu Dec 10 18:40:58 PST 2015; root:xnu-3248.30.4~1/RELEASE_X86_64 x86_64
AdvancedMage-3:encryption Andy$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/c++/4.2.1
Apple LLVM version 7.0.2 (clang-700.1.81)
Target: x86_64-apple-darwin15.3.0
Thread model: posix

服务器:
[Andy@localhost ~]$ uname -a
Linux localhost.localdomain 3.10.0-327.4.4.el7.x86_64 #1 SMP Tue Jan 5 16:07:00 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
[Andy@localhost ~]$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.8.5 20150623 (Red Hat 4.8.5-4) (GCC)

最佳答案

Mac 和 RHEL Linux 不是二进制兼容的。可悲的事实是,即使这些系统可能基于相同的 CPU,它们也不共享相同的库和动态库链接语义。 (Mac 基于 FreeBSD+Mach,而 RHEL 基于 Linux)。

但是,可以从 mac 交叉编译到 linux,但我建议您,除非您因偏头痛而发痒,否则最好使用虚拟机来运行 linux 以进行您的本地开发希望部署到你的服务器上。

10-08 05:40
查看更多