This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Cross platform programming
I've written some code using wxwidgets in c++. But I am not able to compile the same code for both windows and linux. Actually I'm a bit scared by the preprocessor directives generated by code-blocks ide. I wonder if anyone could point out some nice tutorials for learning cross-compilation.
Thanks in Advance
Jvc
If you want to build a cross platform program, you will stumbl across a few kind of problems.
Cross compilers problem
Some compilers are offering non-standard functionalities that might not work on other compiler. You have to make sure that compiler functionnalitie's you use are standard or available on all the compilers you use (An exemple would be the VC++ #pragma that wasn't usable on gcc until version 4.2.1)
Platform specific functions and libraries
Guess what, if you include <windows.h> it won't compile on a linux system (this is madness I know). So you must try to avoid those platform specific libraries/function. If you ever have to use them, try to encapsulate their use and select the specific class you need to compile on different system.
Here is a wonderful guide posted by the Mozilla foundation :
https://developer.mozilla.org/en/C___Portability_Guide
You could have a look at Cross Compiling wxWidgets Applications on Linux (Code::Blocks wiki) and Cross-Compiling Under Linux (wxWiki). There is also a section on cross compiling Windows applications in Brent W. Woodruff C++ Introduction to wxWidgets.
Related
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.
I've got new task to research the way of development C++ cross platform (Mac/Win) utility for our internal needs.
I've developed for 7 years using different "pink" languages like C# , Java , Managed C++.
But in this task , the requirement is to support Mac , and .NET that is running on Mac , is really pain (Know this from other guys who did used this).
So I've started to think about C++ if it's possible to use C++ for Cross platform development.
The application will no contain any GUI , but will contain a lot of System API calls , and a lot of business logic analysis.
Is there possible some library allowing to achieve such kind of Task ?
Is it possible to do at all ?
Yes, you can write standard, ISO C++ and run the programs on both platforms.
When you need to implement some functionality using platform specific APIs (e.g. using Win32 on Windows and POSIX APIs on Mac OS) then what you do is write your own wrapper functions that abstract away the platform specific details, and then use that wrapper in the rest of your program.
Tools like CMake will allow you use Visual Studio to build the program on Windows and Xcode to build on the Mac without having to manually manage separate Visual Studio and Xcode project files.
If I understand your question properly, the only thing you need to develop cross platform c++ is to get the right compilers. You could use GCC on both platforms or even use 2 different project files for visual studio and xcode. That's up to you. Personally, I prefer GCC.
Regarding code itself, it depends on what you do with it. STD is available on both platforms (std::vector, std::string, etc) so code should compile properly on both platforms.
Edit: Btw, most platform specific stuff are available through open source code (like boost though I personally don't like boost that much). If needed, you could even look at other open source projects that are cross platform (ogre3d, etc).
I am new to writing programs in c++ and i want to know if there is a way to export it to a windows format. Also what is a good way to learn objective-c, is it very different from c++? Thank you in advance.
Using mingw32 you can cross compile for windows. See http://www.mingw.org/wiki/LinuxCrossMinGW
Objective C is very different from C++. It's mainstream use (that I know of) is for Apple platforms. I'm sure there are others, but this is the most common that I have seen.
MinGw32 is the way that you would cross compile on a Linux platform to target Windows.
If you want to compile using gcc on the Windows platform, and be compatible with Linux libraries, you can use mingw32/MSys or Cygwin.
If you simply want to develop using gcc on Windows, ignoring any Posix(/Linux standard libraries) compatibility, then there are many options available to you. A popular method would be to download and install Eclipse for C++/CDT. It might use one of Cygwin or Mingw32 under the covers - not sure.
The Objective-C language is a simple computer language designed to enable sophisticated object-oriented programming. Objective-C is defined as a small but powerful set of extensions to the standard ANSI C language. Its additions to C are mostly based on Smalltalk, one of the first object-oriented programming languages. Objective-C is designed to give C full object-oriented programming capabilities, and to do so in a simple and straightforward way.
For reference
You can use Cygwin to code/compile the same code on a windows environment
If you are new to programming I would just try to write something working on both *nix and windows, and compile it directly on Windows if needed.
I am trying to figure out which C/C++ compiler to use. I found this list of C/C++ compilers at Wikipedia:
http://en.wikipedia.org/wiki/List_of_compilers#C.2FC.2B.2B_compilers
I am fairly certain that I want to go with an open source compiler. I feel that if it is open source then it will be a more complete compiler since many programmer perspectives are used to make it better. Please tell me if you disagree.
I should mention that I plan on learning C/C++ mainly to program 2D/3D game applications that will be compatible with Windows, Linux, MAC and iPhone operating systems. I am currently using Windows Vista x64 OS.
First of all, IMHO as a beginner your development environment (IDE) matters a lot more than the compiler.
I think that people place too much emphasis on compiler choice early on. While it is not Java, C++ is meant to be portable.
If the program you're writing only works with specific compilers, you're probably doing the wrong thing or can work a little on making it more portable.
If you get to a point where compiler choice makes a significant performance impact for you, then you've already perfected everything else in your program and you're in a good state and you are also quite advanced in your abilities. We used to teach the differences between compilers at fairly advanced stages in the CS curriculum.
If you use a UNIX based machine (Linux, Mac, actual Linux), then pretty much GNU (g++) is the way to go and is fairly much standard. If it's good enough to compile your OS, it's probably good enough for you. On a mac you can use XCode as your IDE, and it interfaces well with g++. On Linux some people prefer command line tools, though you might like the Eclipse C++ support, it is much better today than it was 3-4 years ago.
Things on Windows are trickier. If you can afford it, have access to, or are eligible for one of the free editions (e.g., via a school), I think the Microsoft Visual C++ Environments (or whatever they are called now) are pretty good for learning and they are used in production. I think there's actually a lightweight visual studio now with an emphasis on C++ that could be a good start. If you don't, you can probably find a distribution of Eclipse that is specific for C++ and includes an implementation of the GNU compilers.
Use gcc and g++ while you're still learning these languages, a big enough task for now. If you need a specialized compiler down the road, you'll want to have much deeper understanding of the language and your problem domain to properly evaluate candidates.
I feel that if it is open source then it will be a more complete compiler since many programmer perspectives are used to make it better.
That's not necessarily true. You could also say that if you use Microsoft's compiler, it will be optimal for Windows, since Microsoft knows best how to optimize a compiler for Windows.
Microsoft has Visual C++ Express Edition which is free and ofcourse includes a nice IDE that's very well suited for Windows development.
But if you're interested in making portable software, look at GCC, which is the default compiler on Linux and which is also available on the Mac. (The iPhone works totally different and requires special tools that only run on Mac OS X). You can get GCC for Windows with Cygwin or MinGW.
Get the Visual Studio Express (easier and quicker IMO, to setup) and learn with it; when you think you know enough about C++ and how "things" work, you could start using something like QT or GCC (with cygwin) and learn to port to different platforms.
For windows u can use CodeBlocks I believe it uses gcc and its pretty user friendly
I strongly suggest going with MinGW.
It is:
Open-source
Available on all major platforms
Comes with standard Win32 headers and libraries
The key to writing portable C++ code is:
Use a cross-platform version control system (subversion is a great choice), because this makes it easier to
Compile and test your code on other platforms early and often
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 9 years ago.
Saludos a todos en stackoverflow.com!!
So... I'm a C++ newbie currently taking the subject of Data Structures, and I want to consult something with you guys:
Since I started studying Systems Engineering, I've been using the last version of Dev-C++ for all my programming projects. It has done it's job well so far, but it has a FATAL flaw... trying to debug your code in it it's just... awfully painful!! Everytime you activate the debug mode you have to cross your fingers and hope it doesn't crash in your face just before the variable you are watching even changes.
Then, I though about using Visual-C++ from Microsoft, as it is free too; however, I asked my teacher and told me not to use it in the course, as it is not standard (compiled programs in it will only work for Windows); we are required to make code that can be compiled both in a Windows and Linux environment, and once compiled in any, being able to use the application on both environments.
So people, I want you to please give me advice: Which C++ compiling software out there has a strong and/or stable debugging mode, implements the standard compiler (is it called g++, by the way??), and it's hopefully free???
Thanks for your attention. Gracias y hasta luego ^_^ (BTW, happy first post!!! ^o^)
There is an implementation of g++ and GDB ( the Gnu Debugger ) for windows called Mingw that works very well. Though since I'm guessing you're also looking for a graphical IDE / debugger, I'd point you at Eclipse. The C Development Tools for eclipse include an excellent interface to the Mingw build tools and GDB. Also, on your comment about a compiled program being used on both Windows and *nix, this is not possible as far as I know.
It depends what you mean by 'standard'. You're going to have to compile your code on linux and windows anyway. There is no way to create and executable that will run on both. Just use VS to compile and debug on windows, then make sure it also compiles and runs under GCC on linux.
I think he just wants you to use the same compiler on both, so you don't have to deal with differences between compilers. I think that's good for you. Writing code that can compile on multiple compilers is important because it shows you what parts of the standard really are standard.
Code::Blocks - it's free, it's cross-platform, it's pretty good. You can download a package consisting of the CB IDE, the MinGW C++ compiler and the gdb debugger. Installation is very straightforward.
It sounds like you're asking for an IDE ( Integrated Dev Environment ).
Eclipse might be good for you. It's supported on both Windows and Linux.
http://www.eclipse.org/cdt/
In terms of compilers themselves GCC is your best bet. It is a compiler in the purest sense of the word though it doesn't have a native interface to it.
I would be looking at finding a good environment to develop in that could attach to it.
Straying from that my personal preference in IDE's right now is eclipse, but that is really a personal thing.
If you use only the standards features of C++, you can use Visual Studio Express C++. Your are not obligated to use the windows only extension of the compiler: MFC, ATL, Wizards, etc.
But if you want to make something portable then trying to target both compilers (GCC and VS) is really a better exercise anyway.
Also recently Nokia released a new multiplataform ide QtCreator, it is a first version but at least it deserve to be taken in consideration.
MinGW is pretty good as #Colfax state. Consider using ddd with Cygwin which makes using gdb somewhat easier. See this SO question
But wait...isn't Qt 4.5 released as LGPL now making that a great free tool? See here
As a linux guy, I think the best experience comes with GVIM as editor and Mingw as
programming environment. GCC is the standard, but LLVM-GCC is very promising and comes with a Mingw build ready for use in windows.
I use DevC++ from when i was starting learn to programm. And i love it. Just can't change to another compiler, so i use wxDevC++ a still maintained version of DevC++.