为什么在Mac上编译的C ++库在服务器上不起作用? [英] Why is a C++ library compiled on my Mac not working on the server?

查看:41
本文介绍了为什么在Mac上编译的C ++库在服务器上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个项目编写C ++库.它基本上包含一个源文件.我在Mac上编写了源文件,并使用 g ++ -c source.cpp -o source.o 进行了编译,并使用 ar rcs libmylib.a source.o .还有一个头文件定义了几个功能接口.

I am writing a C++ library for a project. It basically contains one source file. I wrote the source file on my Mac and compiled it with g++ -c source.cpp -o source.o and created a static library with ar rcs libmylib.a source.o. There was also a header file that defined several function interfaces.

我可以通过发布 g ++ myprogram.cpp -o myprogram -lmylib -L ​​在我自己的Mac上使用此库来编译程序,并且它可以编译并运行而没有任何错误.但是,当我只是将静态库复制到服务器并尝试使用该库编译一些代码时,它就无法工作.编译器抱怨某些功能的定义未定义.显然,链接器成功找到了该库,但根本找不到该定义.

I can compile programs using this library on my own Mac, by issuing g++ myprogram.cpp -o myprogram -lmylib -L .and it can compile and run without any bug. However, when I simply copied the static library to the server and tried to compile some code using that library, it didn't work. The compiler complaint that definition for some functions are not defined. Clearly the linker found the library successfully but it simply couldn't find the definition.

然后,我尝试在服务器上编译库源文件,并在服务器上编译我的项目.一切都还好.但是,当我将服务器上编译的库复制到Mac并尝试使用该库编译本地测试程序时,编译器投诉 ld:警告:忽略文件./libmylib.a,该文件是为存档而构建的,即而不是链接的架构(x86_64):./libmylib.a ,它拒绝链接.

Then I tried to compile the library source file on the server and compile my project on the server. Everything was ok. But when I copied the library compiled on the server to my Mac and tried to compile my local test program with that library, the compiler complaint ld: warning: ignoring file ./libmylib.a, file was built for archive which is not the architecture being linked (x86_64): ./libmylib.aand it refused to link.

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

What confused me is that the architecture of my Mac and the server should be the same (a.k.a, x86_64), but the same source file compiled on these two machine can't be used exchangely. What can be the reason for this? Shouldn't the library be compatible on machines based on the same architecture?

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

For more details about the system and compiler:

我的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 and RHEL Linux are not binary compatible. The sad truth is that even though these systems may be based on the same CPU, they don't share the same libraries and dynamic library linking semantics. (Mac is based on FreeBSD+Mach, and RHEL is based on, well, Linux).

但是,可以从Mac交叉编译到linux,但是我建议您,除非您偏头痛,否则最好让虚拟机运行linux来执行本地操作.您希望部署到服务器上的开发.

It is, however, possible to cross compile from mac to linux, but I would suggest to you that unless you are itching for a migraine, you'd be better off getting a virtual machine to run linux on to do your local development that you hope to deploy to your server.

这篇关于为什么在Mac上编译的C ++库在服务器上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆