Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a C++ library compiled on my Mac not working on the server?

Tags:

c++

linux

macos

gcc

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.

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.

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.

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:

My 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

The server:

[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)
like image 251
Andy Ge Avatar asked Mar 13 '23 23:03

Andy Ge


1 Answers

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).

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.

like image 98
PaulProgrammer Avatar answered Apr 27 '23 10:04

PaulProgrammer