Is it possible to build a Mac binary on a non-Mac unix machine? - c++

I would like to set up a Mac buildbot slave, but unfortunately it's not possible to install Mac OS X 10.5 on my XenServer hypervisor. So, I've had an idea, but not quite sure whether or not it'll work. The application is C++, and on Mac it's compile using GNU Make. I have a Mac desktop PC, and I was hoping I could copy the .h and .lib files on to a Linux box, and try to build against the Mac headers:
#include <mach-o/dyld.h>
#include <AvailabilityMacros.h>

You have to cross compile. gcc can create many versions of object files, not dependent upon what sort of host system you are running. mac uses a VERY old version of the gcc suite that they have forked for OSX development so even though it's possible, it's not going to be easy.
Essentially, search for cross compiling on the gcc compiler, but be sure that it will take a substantial effort to do this.
[edit]
look here for an answer (albeit a different host machine OS)
[/edit]

Related

whats the difference between mingw and mingw64 [duplicate]

What are the differences between MinGW, MinGW-w64 and MinGW-builds?
And which one should I use to compile c++ 11 source code with the Eclipse IDE on a Windows 8 machine?
MinGW is a GCC-port for Windows. Not all of the Windows API is supported (but for many programs the supported stuff is sufficient) and it´s only for 32bit-Programs (which often can run on 64bit-Windows too, but some can´t, and you can´t compile them as 64bit).
MinGW-w64 is a improved version which supports both 32bit and 64bit, and some more of the WinAPI (still not all, because thats much work, but more than MinGW).
MinGW-w64 only provides their source code, but no binaries to "just use" the compiler.
MinGW-builds is a somewhat separate project to provide binaries in the most useful configurations. To get a specialized build of MinGW-w64, manual compiling is still possible.
Using the MinGW-builds self-installer is the easiest way, if nothing unusual is needed. Also see here for help with the self-installer.
Mingw compiles your code to Windows binaries that run under Windows.
Windows subsystem for Linux (WSL) makes Linux binaries. You can install other Linux programs under WSL, except if you need a graphical interface. You can access the Windows filesystem from WSL, but not vice versa.
Cygwin makes Windows binaries that can run under Windows outside the Cygwin shell, as long as you have the Cygwin DLL. This gives a Linux-like environment that is fully compatible with Windows.

Running linux g++ compiled code on Mac

Is is possible to compile c++ code on linux using g++ and run the code on Mac OSX? I have a few c++ programs that use one .cpp file, a few .h files, and a MakeFile altogether that produces a .o file that I typically run through the terminal. However, I'd like to find a way to send only the executable to my partner's home mac so he may review my program locally. (I've also use a few of these programs for automated math calculations, so it would be very convenient to run locally). I understand OSX typically uses .app bundles, but I'm not extremely familiar with how this works. Will the .o file (or ./a.out that's more common around here) simply run on OSX? I'd rather not install xCode on this machine if I don't have to.
Thanks a ton
It is possible to compile C++ on Linux and produce an executable on OS X. However you have to compile the code in a special way, called 'cross compilation'. It's not particularly simple to set up cross compilation and you need certain files from the platform you're targeting.
It's much simpler to just compile directly on the target platform.
If the Mac has a recent version of OS X installed you can easily install the necessary command line tools: Just try to run one of them or run the command xcode-select --install and OS X will ask to install the command line tools. (This will install just the necessary tools for compilation on the command line, and not the entire Xcode applicaiton.)
I understand OSX typically uses .app bundles, but I'm not extremely familiar with how this works.
You don't need to worry about .app bundles for simple C++ programs. OS X can run regular executable files just like linux. (Though the executable file format is different: OS X uses the Mach-O format instead of Linux's ELF format.)
Will the .o file (or ./a.out that's more common around here) simply run on OSX?
.o files, called 'object' files, don't run on their own anyway; They have to be 'linked' into an executable file. The default name for executable files created by the gcc toolchain is 'a.out' (as specified in the POSIX standard).
If you set up cross compilation to OS X then, yes, you could produce a.out files that would just run on OS X. The a.out files you produce normally for Linux , i.e. without cross compilation, won't run on OS X.
I'd rather not install xCode on this machine if I don't have to.
Xcode doesn't run on Linux anyway, so you couldn't run it. Instead you'd get a version of gcc that cross-compiles to OS X, or you'd install a different compiler, clang (and linker, lld instead of ld or gold).
A simple answer to your question is no. You cannot compile a program under linux and expect it to run on MacOSX.
However, MacOSX is just another UNIX OS under the hood and you can build your project with Make and GCC. If your partner doesn't know how to do this, I would suggest asking him to let you SSH into his machine.
However, if you're building executable on a mac you will want to install XCode. Even if you're using GCC from brew.
Another alternative is have your partner install a linux VM. He can use oracles virtual box to install linux and run your code w/in macosx.
I would suggest you sign up at Amazon for a free EC2 (Elastic Compute Cloud) account and take the free Basic Linux box. Install and build your software on Linux on there and then let your partner log into it and run it using just ssh which is already on OSX anyway. So no need to install anything on his Mac.
That way there is no need to transfer files between yourselves and update stuff when you make changes - just one environment to manage and keep up to date that you can both access from anywhere, any time.

Building on another platform?

I am trying to build an c++ application using gcc to run it on Cent OS among others. The thing is, compiling this in Cent OS is harder than the development. I am using 2 libraries, the MySQL C API and the boost asio. The MySQL C API has a different version of the library for each OS. This makes me believe it a terrible idea to get the edition for ubuntu and run it on Cent OS.
Is the following recommended:
Build the program in ubuntu and use the libraries designed to work with Cent OS. Then copy the executable from ubuntu to Cent OS.
If the above is a good idea, can I build it on windows?
The main reason why different libraries are made for different linux versions is because they are using dependencies to different system .so files.
It's not a good idea to use on Ubuntu the libraries designed to work with Cent OS , since at compile time, they (might) dynamically link to your Ubuntu system .so files.
You will never have the guarantee it will work.
Your suggestion of installing the Cent OS versions of the libraries in an Ubuntu system might work, but you need to test it first.
If you can run your Ubuntu compiled program on the Cent OS system , then you are safe to go.
Just make sure you make this test every time you want to install a library :).

GNU gcc and g++

Are the gcc and g++ compilers installed on a MAC OS X machine different from the ones on Ubuntu (Linux) GNU gcc and g++ compilers?
I am using Eclipse to develop a C++ program and there is toolchain section where it says MacOSX GCC and I was wondering if I need to install another compiler so that the executable would also run on Linux machines.
I am a bit new to the technical details of C++ development so I am sorry if this question does not make sense.
It it very unlikely that binary will execute on both Mac and on Linux. If is pretty likely that a binary will not execute between different distro's of Linux. You can either compile you binary for each OS. Or you can distribute the source code for you application and let you users compile it themselves.
Different versions of libstdc++.so are likely distributed with different OS's and this will cause you problems. A solution that partly works is to statically compile your binary so you are not depending on the target systems installed version of libraries.
MacOS is not Linux, it might have a bit in common with BSD, but definitely not Linux. They do, or can, use different configurations of the same compiler, but the programs are not compatible.
The only way you're going to run the same program on both is if you have something like Wine to provide a compatibility layer.

Mac OS X free C compiler

Is there a small, free C or C++ compiler for OS X, like TCC?
Xcode is not small, or simple like TCC, AFAIK.
I'm looking for something like a GCC binary file or TCC for OS X.
gcc is the most widely used one (and Apple provides official builds of it as part of XCode). XCode is also usually distributed on your OS X cd's, though that version may be a little out of date now, depending on what version of OS X you have.
For information and samples on how to use it, look here.
Look into Fink or one of the similar projects out there if you want a build of gcc that's not linked to Xcode. That said though, Xcode is a great IDE for OS X.
You could use the Xcode Tools that are already included in OSX.
Xcode is the same professional
developer toolset used by Apple to
create Mac OS X, as well as many great
Apple applications, and Xcode is
included with every copy of Mac OS X.
You can use Code::Blocks IDE its simple slick and hav good set of feture, a plus opensurce and ready to dl for 3 major plateforms
still recoment using Xcode if u just have to make projects for MAC, but if u are going to port it to linux and windows, then my choice is Code::Blocks [Give it a try atleast]
I know you want a small compiler, but it is best to just download and install Xcode. It is a great IDE. From there, you don't have to use it if you don't want to. Once it is installed, you can use something like this in terminal:
gcc myFile.c -o myFile
And that would build a source file called myFile.c which contains C code and the output module (executable) would be myFile. You'd execute it by running:
./myFile
Update: What is your reason for not wanting Xcode? Furthermore, is GCC itself ok (disregarding Xcode for a moment)? It's important to know that GCC is not a part of Xcode. Apple made use of GCC, which is an open source C/C++ (and more) compiler.
I just can't find binaries of GCC for mac so downloading Xcode is an easy way to get some...
Update 2: It also just came to my attention that NetBeans will compile C / C++ Code. It also uses the GCC compiler. I'm not sure if it using GCC from my Xcode installation but I thought I'd mention this.
Best Regards,
Frank
There are binary packages for LLVM, both for the Clang and GCC front-ends.
PCC, the Portable C Compiler, is available for the Mac. It is small and free. Its tarball weighs in at total of 1778 KB from its 4th of July CVS Snapshot.
If you don't want to use Xcode install the macports from macports.org and if you like a graphical user interface install porticus as a managment gui for all the installed ports. In Porticus you can now easily install all different versions of gcc without Xcode.
Porticus will compile the packages for your mac and install all the needed dependencies like a Linux packet manager.