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

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.

Related

C++ Code for Windows & Linux

First of, I am very new to programming, but took an interest in it. I have successfully built a C++ Console program for Windows which is a simple Database program, which can edit / delete / input entries.
I am less and less relying on Windows for day to day stuff. I had an old HP Netbook which was impossible to use with Windows, but I put in a Linux Distro and works like a charm.
As I sometimes do use Windows, as well as having built the program to use in Windows, I am wondering if the same code can be used to compile a Linux program? I could use WINE to run it but would prefer running something specific to Linux. Is this possible with the same code or would I have to make another Linux version of it?
I would assume that since you are new to programming, that you did not make the extraordinary effort to make your code portable across platforms. That takes a significant skill set, especially if you are accessing external resources such as a database. So the answer is you will probably have to re-write for Linux, and specifically the database interface.
I guess that you want your C++ code to be compilable both on Linux and on Windows. You'll need operating-system specific compilers for that (a different one on Linux and on Windows).
I am wondering if the same code can be used to compile a Linux program?
The program to compile your C++ code is called a compiler. On Linux you will use GCC as the g++ command (which you could even customize with MELT, but that is not for newbies), or Clang/LLVM as the clang++ command. You'll use a builder like make (see here for why, and this example). Be sure to install a recent version (GCC 4.9 or Clang 3.5 at least in start of 2015) to get good C++11 support. Focus on learning C++11 (or C++14) not some earlier variant (so use a C++11 compiler).
I don't know Windows so I cannot recommend any good C++ compiler for it (but I heard of MinGW, CygWin and of Microsoft Visual C++; look also into recent Clang...).
You might be interested in cross-platform C++ framework libraries like Qt or POCO (and perhaps also Sqlite for database related stuff). They will help you to code some C++ usable on both systems (after recompilation).
BTW, you can always encapsulate your system specific code with preprocessor directives à la #if LINUX ;take care of putting all the OS specific (or OS related) code in a few source files.
It could happen (and I wish that for you) that you get fond & happy of Linux and will, in a few months, prefer to code for Linux only (you'll then install Linux on all your machines). BTW, study the source code of existing free software you like and use on Linux. That will teach you a lot.
The advices I gave here and here are still relevant today when coding on Linux. Read also something about porting & portability, and Advanced Linux Programming.

Development in Cygwin C++

For a few years I was writing programs in Visual Studio for Windows and with GCC (Code Blocks) for Linux. Most of my libs compiled seamlessly as they worked both in Windows and Linux. However at the moment I am a bit confused, as I have to create an app using Cygwin. I don't really understand if I am still in UNIX/Linux environment, just running app on Windows by some "emulation", or I am rather on Windows just having access to some Linux/Unix functionality. From what I understood from the FAQ's and documentation it looks like I just should behave like in Linux environment.
All explanations I found in internet usually are very general and don't explain the detailed differences from programmers viewpoint.
Short question: Can I just write programs like I did for Linux without any major changes when using Cygwin?
Maybe.
A lot of code written for Linux will compile in Cygwin with very few problems, which can mainly be fixed by messing with preprocessor definitions.
However, any code written for linux which:
Uses a Linux driver
Directly accesses the kernel
Relies on any code which does either of these two things (and doesn't have a Windows counterpart)
will quite definitely not work, regardless of how much you modify the code.
Much as it tries to, Cygwin cannot fully emulate (yes it is an emulator, of sorts) everything a POSIX system can normally do. Cygwin is not windows, just a conversion layer from its own machine language.
For more information, read cygwin's wikia
Can I just write programs like I did for Linux without any major
changes when using Cygwin?
The platforms are not identical, so you can not realistically expect to write the program in Linux, and then POOF expect it to build and work under Cygwin. But if you don't use things not available under Windows, then you won't need major changes. And you can write non-trivial programs, which will build and work on both, perhaps needing a few #ifdefs in places.
From your question I take it you want to work on Linux, but write programs for running under Cygwin. In that case you must also build and test it in the Cygwin environment all the time, so:
Use version control, commit often. I recommend a DVCS like git or mercurial which have separate commit and push, it will allow you to do commits more freely.
Whenever you commit/push, do checkout/pull and build on the Cygwin host. You can do this manually or automatically (by simple custom script polling the version control, or by Jenkins or something).
When ever your code stops building or working under Cygwin, fix it before continuing with new code.
If Cygwin is not absolute requirement, then I would look into using Qt SDK. It can be used for non-Qt projects too, the MinGW toolchain on Windows is very similar to gcc on Linux. And if you're willing to use Qt, then it has all sorts of platform-independent features for things you might want to do, such as discover locations of standard directories for saving files, use threads, print things, have GUI...

Building C++ cross-platform static libraries

I want to build cross-platform statically linked library in C++.
How am I supposed to do this(I am a complete NOOB, never been using g++,gcc,mingw etc I looked on the internet for a solution to my problem, but didn't find anything. Except that mingw is a Minimalist GNU for Windows...)? I've been always using Visual c++ on windows, but now I have to build lib which will be attached to Qt projects for: windows,mac,linux.
If anyone know a good tutorial, on using compilers, and how to build libraries, I'll appreciate sharing them here :)
Thanks, may the force be with you.
MinGW is a very good compiler! I use it all the time. Even though MinGW stands for Minimalist GNU for Windows, MinGW has everything you need to program in either C++ or C. If you are creating a QT Application, I recommend using QT Creator (just google it). I haven't used it lately, but I think it supports Cross-Platform compiling (as does MinGW, with some command-line commands...).
The best way to cross-compile though, is but getting a copy of each OS that you are going to compile for. So if you are doing Windows, Mac, and Linux, I recommend getting one of each (Mac OS X is around $25 and Linux is free). Then just use the included compilers (XCode on Mac; GCC on Linux) to compile. Unfortunately when you are performing cross compiling, you need to support each OS individually (some parts can shared between OSes).
Since you will be using a toolkit (QT), you probably don't have to worry much about platform specific calls or anything. Just make sure you use the QT typedefs and structs and not the Microsoft Windows ones...

Some Issues About Cygwin[Linux in Windows] (socket,thread,other programming and shell issues)

I have some question about cygwin :
Can I use Cygwin develop socket based code?
Does Cygwin have read() and write() functions that work with file descriptors?
Can I use Pthread library in Cygwin?
Does code that compiles in Cygwin also
compile in Linux without any change or with little change?
Will an executable file that built by
Cygwin run in Linux ?
Why does Cygwin not need the linker
option -lpthread when I use pthread library?
why in #include <iostream> don't I need to use using namespace std; ?
Can I work with QT in Cygwin? If so,How?
Can I boot my Linux in other
partition with Cygwin and use it?
Can I access the other partition
that is EXT3 in Cygwin?
On 1:
Yes. Socket libraries are shipped with Cygwin - many socket based apps such as web servers are included in the base distribution.
On 2:
Yes. I think all of the 'section 2 and 3' system calls in the GNU C runtime and library are implemented by the cygwin runtume. You can check this in the man pages that come with Cygwin. A list of system calls and std lib calls implementd by Cygwin can be found here.
On 3: Yes. Pthread is included in Cygwin. The list referred to in the link above mentions pthreads as well.
On 4: Anything built against GNU libraries should work with little or no change between Cygwin and Linux (assuming there are no dependencies missing on Cygwin). Depending on CPU architecture you may have to worry about word alignment, endianness and other architecture-specific porting issues, but if you're targeting Windows and Linux on Intel your code would have few if any porting issues arising from CPU architecture.
On 5: Cygwin will build a program against its own shared libraries by default but GCC can cross-compile to target other platforms. You could (in theory) set GCC up to cross-compile to any target supported by the compiler. There are plenty of resources on the web about cross-compiling with GCC, and I don't think the process will be materially different on Cygwin.
Note that Cygwin binaries will not run on Linux - or Vice-versa. You will still need separate builds for both.
On 6: Not sure - at a guess it's included in the standard runtime, perhaps because it was necessary to wrap the Win32 threading API for some reason.
On 7: Don't know - it's probably the same on g++ on all platforms. Apparently a compiler bug. Dan Moulding's Answer covers this in more detail.
On 8: Yes. IIRC QT is available in the standard builds and it will certainly compile on Cygwin. As with Linux/Unix, QT on Cygwin uses an X11 backend so you will need to have an X server such as XMing running.
In order to avoid the dependency on an X server you may want to build QT apps against the Win32 API,. It is possible to do this with MinGW, which is a set of header files and libraries to build native Win32 apps with GCC. MinGW can be used from within a Cygwin environment (an example of GCC on Cygwin cross-compiling to a non-Cygwin target) and the installer from cygwin.com gives you the option of installing it.
MinGW is quite mature; it has all of the 'usual suspects' - libraries and header files you would expect to find on a Unix/Linux GCC development environment and is very stable. It
is often the tool of choice for building Win32 ports of open-source software because it is (a) free, (b) supports the libraries used by the software and (c) uses GCC so it is not affected by dialectic variations between MSVC and GCC.
However, these dialectic variations in the language and available libraries (for example MSVC doesn't come with an implementation of getopt) mean that porting programs between MinGW and MSVC can be quite fiddly. My experience - admittedly not terribly extensive as I've only done this a few times - is that porting applications between MinGW32 and Linux is easier than porting between MinGW and MSVC. Obviously apps with non-portable dependencies such as Win32 specific API usage would require the dependent components to be re-written for the new platform but you'll have far fewer problems with differences in the standard libs, header files and language dialect.
QT does a fairly good job of providing a platform abstraction layer. It provides APIs for database access, threading, I/O and many other services as well as the GUI. Using the QT APIs where possible should help with portability and the Unix/Linux flavoured libraries that come with MinGW mean that it might give you a good platform for making applications that will port between Win32 and Linux with relatively little platform dependent code.
EDIT: The qt development packages in Cygwin are:
qt4: Qt application framework (source)
qt4-devel-tools: Qt4 Assistant, Designer, and Linguist
qt4-doc: Qt4 API documentation
qt4-qtconfig: Qt4 desktop configuration app
qt4-qtdemo: Qt4 demos and examples
You'll probably also need gcc4-g++ and some other bits and pieces. This listing on the cygwin web site has a list of the packages.
"Yes" to all of those except 5. You'll have to build your executables separately for Linux, but that should be straightforward since the answer to 4 is "yes".
Make sure you install all the development headers you need on both platforms.
Yes, Yes, Yes, Yes (if you write it carefully and don't use anything specific to cygwin), No.
On 5: it may be possible to cross-compile from Cygwin to Linux, I really don't know. If it is, then technically some executables "built by Cygwin" would run on Linux. But Cygwin executables won't run on Linux.
1-4: The answers to these are all "yes", because all of these features you are asking about are part of the POSIX standard. Cygwin is basically a POSIX layer for Windows so, naturally, Cygwin includes all of these features.
5: No, Cygwin binaries will not execute under the Linux kernel, because ultimately the code generated by Cygwin is Windows native code. It will link with the usual Windows DLLs (like kernel32.dll), whereas Linux does not even have a notion of DLLs (it has shared objects which are similar, but different). More importantly, executables built under Cygwin will be in the PE format, but Linux generally uses the ELF format for executables. You could, of course, do cross-compiling under Cygwin, and thereby generate native Linux executables, but it doesn't sound like this is what you are looking for. Also, compiling a new version of GCC to use as a cross-compiler under Cygwin is probably no trivial task.
6: GCC under Cygwin doesn't require use of -lpthread because all of the pthread code under Cygwin is in cygwin1.dll which is always linked in by default.
7: This is a bug in GCC (on all platforms). It has been around for a long time and will probably never be fixed.
8: ConcernedOfTunbridgeWells did a great job of explaining this, so there's not much left for me to say. However, it's worth noting that Cygwin has the X.org X Window System in its package list. With Cygwin 1.7.x, setting it up and using it is a breeze and it seems pretty rock-solid. If you want to use Qt with X, I'd recommend using Cygwin's X server.
In response to #7, the version of the C++ compiler that you have is probably out of date. If you don't need linux/posix portability, I'd switch to the non-cygwin MinGW GCC compiler - you can get 4.4.1 at http://tdragon.net/recentgcc/
Regarding 6, you don't need -lpthread because Pthreads support is part of the Cygwin DLL.

Compiler installation which is simple

Is there any compiler for C++ that works under W7 and is easy to install, except VC++?
I never get these scripts and linux emulations to work, and really just want to try another compiler.
The nuwen distribultion of the GCC compiler includes the compiler and all necessary supporting tools and libraries as a single Windows installer. You don't need any Linux emulation in order to use it. A similar, slightly smaller pacakage is TDM's MinGW build. Both of these are on GCC 4.5 (as of Aug-2010).
If you want an IDE, then Code::Blocks also comes as a complete system. This has recently (Jul-2010) been heavily improved, and comes with the GCC 4.4.1 compiler, if you want it. You might also want to look at CodeLite, which is also fairly easy to install.
Mingw is generally easier than cygwin. It doesn't come with a port of every unix tools as cygwin does, but the resulting .exes are native (no need for cygwin.dll)
Cygwin includes the gcc compiler and also provides a Unix look and feel which will be the other thing you need to get scripts and linux emulations to work. (This inlcude the libraries Unix libraries will have functions that VC does not have which might be the issue that you are having)
Qt for Windows comes with MingW, which I've found to be reasonably easy to use and install, and the LGPL version is priced right (free as in beer). You don't need to use any the Qt libraries in your application. I'm not sure which version of GCC is currenlty bundled with it. There are no licensing restrictions for the software you develop (unless you're actually modifying and redistributing source code of the LGPL version of Qt).