And*_* Ge 4 c++ linux macos gcc
我正在为一个项目编写一个C++库.它基本上包含一个源文件.我在我的Mac上编写了源文件并用它编译g++ -c source.cpp -o source.o并创建了一个静态库ar rcs libmylib.a source.o.还有一个头文件定义了几个函数接口.
我可以在我自己的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和服务器的架构应该是相同的(aka,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
Run Code Online (Sandbox Code Playgroud)
服务器:
[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)
Run Code Online (Sandbox Code Playgroud)
Mac和RHEL Linux不是二进制兼容的.可悲的事实是,即使这些系统可能基于相同的CPU,它们也不会共享相同的库和动态库链接语义.(Mac基于FreeBSD + Mach,而RHEL基于Linux,以及Linux).
但是,可以从mac到linux进行交叉编译,但是我建议你,除非你想要一个偏头痛,否则最好让虚拟机运行linux来进行你的本地开发希望部署到您的服务器.