Queries on Cross compiling, from Windows to Linux and from Linux to Windows, - c++

I am working on a C, project which uses ffmpeg library.
Currently I'm working on windows platform, and I'll be cross compiling the project for Linux ARM.
With that background, I have few basic questions.
If I use ANSI C++, I can be sure that, I'll be able to cross compile the project using corresponding compilers [ MSVC, MingW ]
But ..
If I'm using "Win32" and other "Windows" specific APIs in my project, how does the cross compiler will handle it, to make the project able to run on Linux.
Similarly, If I'm using Linux specific "features" in my project, how does the cross compiler will handle it, to make the project able to run on Windows.

When you cross-compile, the code that is being cross-compiled must use APIs that are available on the target platform (ie, the one that it will eventually run on). A cross-compiler does not magically give access to Win32 APIs when its output is run on a Linux machine; it is the same as compiling the code on the target machine, but means you don't need to actually do so. You could achieve the same thing, in other words, by just running a native (non-cross) compiler on an ARM Linux box, but you'd need a powerful enough ARM box to run the compiler.
That said, in principle, you could cross-compile to Linux while using winelib to provide win32 APIs. Not sure how well it works on ARM though - it's only really meant to be used on x86.
Finally, note that cross-compiling tends to be quite complex even in the best of times. It may make your life simpler to cross-compile from x86 Linux to ARM Linux instead of x86 Windows to ARM Linux - while it's possible to do cross-OS and cross-platform builds, the less variables you have changing the simpler things will be.

If you use Winapi, your project will not be able to run on Linux.

Related

Writing a cross-platform program

How could I write a program which runs on both Windows 7, Mac OS X (and maybe linux too)?
I heard Qt are a great framework to build cross-platform GUIs, but I think every program version need a recompile, isn't that right? And should I compile the win version under windows, the mac version under mac os x, the linux version under linux and so on?
I'm getting ideas and/or suggestions
The underlying binary format is different on each platform, so unless you're using a virtual machine (like Java or Flash does) you will have to recompile your program on each platform.
Some compilers (like GCC) allow cross-compiling, but it is not trivial to set up. Probably the easiest system to cross-compile on is Linux (there are several open source projects that have cross compilation set up from Linux to Windows).
In case of a GUI application, it depends on the language -- if you're stuck with C++, Qt or wxWindows might be a reasonable choice providing an abstraction layer over the native windowing system.
If you can go with Java, it makes life simpler, however the windowing system is Java's and not native.
Another language to think about is FreePascal w/ Lazarus -- it has a pretty good GUI designer that compiles to the native windowing system on every platform (WinAPI on Windows, Cocoa on OSX and GTK on Linux).
Not sure if C++ is a must, but Adobe Air is a great cross platform development environment for desktop, and its growing for mobile development as well. If you need an example of a major application using Adobe Air to deploy to multiple desktop OSes, just check out tweetdeck http://www.tweetdeck.com/
I'd highly suggest also looking into Flex and Flash Builder if you go that route.
There are two separate issues I would highlight when writing cross-platform programs -- how to make your code portable, and how to arrange for it to be built on the various different platforms.
As far as the building side of things goes, I would look into a cross-platform build system like CMake (http://www.cmake.org). You essentially write a script and CMake will generate the appropriate project file/makefile for a specific platform. You then build your program on each platform as you would normally. For example, on Windows, you might use CMake to generate a Visual C++ project for you, and then use Visual C++ to actually build your executable. On Linux, you might use CMake to generate a makefile, and then build the executable using g++.
The other aspect is how to make your code portable -- the key is to write C++ standard-compliant code and make use of libraries that are themselves portable across the platforms you're interested in. You can (and may sometimes need to) write platform-specific code for each of the different platforms -- if you do, you should hide it behind a portable interface and have the rest of the code use that.
Yes, you need to compile for each version when using C++.
The only thing that prevents you from compiling a program, for example, for Windows on Mac is to get a tool for doing that. It is possible, but the problem is finding the toolset.
Also you can use a virtual machine for running diferent OSs and compiling code for all platforms on the same machine.
Java runs on Windows, OS X and Linux

Compiling for Mac/Linux on Windows

Obviously you can't really run Mac or Linux apps on Windows, but can you compile binaries for those platforms using MSVC++ for example (plugging in additional compilers and tools obviously)? For a serious build system, you don't want one build server per platform so having an automated build server which compiles for all target platforms seems quite a reasonable aim.
Crosstool-NG seems like your best option for Linux apps; they show that as one of the standard configurations. I do not know about Mac OS X; this question suggests that it will probably be difficult.
I would like to believe (notice my careful words) that GCC can be built to run on windows (any relevant form of the triplet --mingw*) and target another triplet.
A proof-of-concept for the non-believers is provided here, where you can find Win64 hosted compilers that build native linux binaries. I assume the same can be done for mac if the necessary libraries (like the CRT and necessary Mac framework libraries) can be built/used by that compiler.
if you want to build applications using C++, why not use Qt from Nokia. it's cross platform. http://qt.nokia.com/

Crosscompiling C++; from Linux to Windows, does it really work?

I have the source code for some very simple command line programs. I was considering the option of compiling them on a Linux machine (they were deveoped here) so they can be used on Windows. If I am not wrong this is called Cross-compiling. I have never tried it, but reading yesterday some information, it seems to be kind of complicated or not successful. I would like to hear about your opinions, and how could I port a simple "hello world" program, which compiles on Linux with g++.
Thanks
Look into mingw, a suite of tools for building Win32 applications in Linux. If the programs don't depend on any Linux-specific functionality not supported by mingw, you should be fine.
Note that cross-compilation is not the same thing as cross-platform. With cross compilation, you compile the code to a Windows executable on the Linux box, then transfer the executable to a Windows box. With cross-platform, you transfer the source code to the Windows box and compile to a Windows executable using a Windows compiler.
The former is quite difficult (but not impossible), the latter is very easy, using a compiler such as MinGW, a others have mentioned.
I cross-compile on a daily basis. But I don't set up cross-compilers on a daily basis. It can be tricky, but it's certainly possible.
As long as you use standard C++ your code will be cross-platform. You can also use cross-platform libraries like STL, boost, Poco, Qt, etc...
Only when you start to use platform specific code you lose portability. For example including <windows.h> will make your code only compilable on Windows. (There are techniques around this like the #ifdef macro. This enables certain code portions only on one platform.)
So a simple hello world program should work on Linux, Mac, Windows or any other platform. You don't need anything special for this.
Note:
Some may mention Cygwin or mingw32. I'll briefly explain what they are:
Cygwin allows you to compile Linux applications using gcc/g++ on a Windows machine.
Mingw32 allows you to compile Windows applications using gcc/g++ on a Windows machine.
Edit:
If you want to setup a system for cross-compilation, then I recommend that you have a look at cmake.
Yes. We are currently compiling a 250 kloc app, running Qt with daily builds. It's working prefectly everyday, although I've to admit it is not distributed outside the company, but only used internal. For official releases, Visual Studio is prefered.
Compiled using mingw standard packages on Debian.

Windows -> Solaris 5.X C++ cross compiler recomendation

I have neved did a cross platform development before but the process we currently employ such as doing the development on a Windows machine (as we are mostly a Windows shop) and then actually building the binaries on a Solaris box looks a bit convoluted to me.
Can you recomend me a cross compiler so I can limit development tasks to a Windows machine (e.g. building Solaris binaries (.so)), and only use Solaris machine for a testing and deployment.
It would also be great to be able to test a resulting binaries on a Windows machine before (e.g. dependencies b/w binaries) deploying them into the Solaris box, but it looks like I am asking for too much.
Why not use a tool like cruisecontrol to build your apps for you simultanously on both platforms. Doing it this way makes it easier to add more platforms in the future.
Failing that you could use cygwin or mingw to build on windows only for development, then build your real distribution on solaris.
I don't know anything about cross compilers as I've never used therm, sorry
I don't know if a Windows/Cygwin hosted compiler that targets Solaris exists as a ready-to-use product, in any case some googling around didn't give any results.
However, I do have some experience with the creation of gcc&friends based toolchains that run under Cygwin and target Linux platforms (i386 and ARM). When your target is Linux, there's a tool called crosstool that automates a lot of the work that needs to be done, so basically with a reasonably fast machine, a healthy dose of patience and a (longish) evening of build runs you'll be able to build a usable toolchain.
Although in theory you could mimic what crosstool does to create your own cygwin to solaris toolchain, I'm afraid that in practice it may just not be worth the effort.

How to Cross Compile for Cell Linux on the PS3 from Windows?

How can a cross compilation setup be achieved to allow compiling Cell Linux programs on a Windows PC using the cygwin toolchain? The cygwin tools provide a GNU compiler to use in building the cross compiler, and associated tools for the build process e.g. rpm, cpio, make, flex, bison and so on.
I am moderately confident this is possible, but unaware of anyone who has actually done this. It has already been done for x86 Linux, but I wish to use Windows, without requiring the use and overhead of a virtual machine running an entire 2nd operating system.
The Cell Linux toolchain is a patched GNU toolchain, with C and C++ compilers for the PPU and SPU processors, and associated binutils. The sources for the Cell Linux SDK for Cell Linux can be found here. The source RPMS here have build scripts for use with the rpmbuild tool on Linux.
The specific question is: how can a set of Cell Linux GNU compilers for the PPU and SPU processors be built, on Windows, using Cygwin.
I've never done it, so I can't give you step by step instructions, but I can give you a general idea.
The instructions you linked will serve as a pretty good outline, but there will be definite changes.
For the host PC, you can install gcc and other build tools from MinGW or cygwin. That will give you the windows native parts of your toolchain.
Then you'll need to download the sources for the cell portions of the toolchain and compile them (with the appropriate options, --target, etc.) using the build environment you just installed.
Then you download and compile the sources for libspe2, and you're done.
But I'll warn you - it sounds easier than it is. Be prepared to spend a lot of time on it.
Since you can already do this on Linux x86, why don't you just install Linux a virtual machine? Also, what might be even easier, is to install Portable Ubuntu for Windows. It runs Linux alongside Windows using coLinux. Although this may not be optimal, it is probably much easier than trying to compile everything on Windows.
the ps2dev toolchain can easily be set up under cygwin
http://ps2dev.org/ps3/Tools/Toolchain
You should be able to build a canadian cross compiler on Linux that runs on windows and creates code for PS3. Have a look at the excellent crosstools from Dan Kegel.
Did you check if the Cell/PS3 devtools for windows/cygwin work for you?
A set of tools compiled to run on Windows via Cygwin can now be found on Sourceforge.
Mike Acton has a long, detailed article on cross-compiling for PS3 Linux on his Cell Performance blog.
It may be a bit out of date, but the bits on setting up the toolchain and various SDKs might prove handy.