If I write in C or C++ on for example: Windows. Is it guaranteed than I can compile and run it on any other operating system such as Mac OS, Linux, Unix-like systems? So, does it mean C or C++ is cross-platform language?
If you write a C or C++ program that strictly complies with some standard, then the program should work on any platform that supplies a tool chain that complies with that standard. In that sense, C and C++ are cross-platform languages.
No.... there are C and C++ compilers for many many many platforms, but different compilers have their own quirks, and the libraries they link to are completely different on various platforms. Mozilla had a guideline of what features to use and what to avoid to make your software cross platform.
There are environments like cygwin that help with cross platform compatibility in windows *nix.
You can write libraries that are stock standard C that don't have dependencies on platform libraries that will be pretty portable
If you directly access any Windows API, it will fail to run (or even compile) on other platforms. If you use a standard function which indirectly accesses the correct API, or if you add #ifdef guards and access the correct platforms API, then the answer is better. The former should be cross platform. Latter will work on platforms your code caters to.
For Mac OSX, Linux, and, of course, Windows, you can write and compile C++. From personal experience, I've always found Windows to be easiest for C++ usage, closely followed by Linux, and with Max OSX trailing distantly behind. The compilers tend to be temperamental in my experience, and either because the support community is better for C++ on Windows, or because it's naturally better for programming, I've always had less problems with Windows. Though I labeled it in second place, I don't have that much experience in Linux.
Edit: You say "guaranteed" to run and compile. For basic C++ this is definitely the case, but some more advanced features may have varying support across platforms.
Related
I have watched a video in which one of the director of scs software (who makes a EURO TRUCK SIMULATOR game) saying that the game is written entirely in C++, yet the game runs on Windows and Mac OS as well. How could they achieve it? Is it possible to run a C++ program on MAC OS?
Yes.
...OK, stack overflow wants at least 30 characters. OS X comes with the C++ compiler clang++, which is built on top of llvm. In fact, Apple stopped supplying a port of gcc some time ago, so developers either have to build it themselves or adapt to clang. According to Wikipedia, Apple itself develops clang.
It is possible, either to run gcc in terminal or to use Xcode to compile it, however you cannot then use Cocoa but alternatives such as Qt are available for mac.
Sure, there are C++ compilers for Mac OS X as well. One is even included in xcode (the standard development environment on a Mac).
But that does not mean, that you can compile just any C++ program on a Mac. It has to be designed to use libraries, that are available on both platforms (instead of MFC on Windows or Cocoa on the Mac). This is possible for example by using wxwidgets or QT. With these two libraries it is even possible to compile the program for Unix platforms like Linux as well.
Regardless of language a multi-platform program relies on some basic support parts that are implemented differently on each platform. With pure standard C++ this is the standard library and the runtime support, but in practice a game, say, will also rely on 3rd party libraries with platform specific implementations. So given that the platform itself is compatible with the basic platform assumptions of the C++ language, and the Mac is, achieving such portability is conceptually very straightforward, although difficult to get perfect.
As an example of a platform that's not entirely compatible with C++, Microsoft's .NET virtual machine is apparently so at odds with the ideas of C++ that Microsoft has found it necessary to define two different non-standard variants of C++ for it, and offers no way to produce GUI programs for it in pure standard C++. It's not impossible to use only standard C++ for .NET, and indeed one main reason to use C++ with .NET is to combine standard C++ parts with more .NET-y parts; it's just that using only standard C++ the code becomes complex, inefficient and generally impractical. For example, my first questions here on SO concerned how to do a simple .NET message box in standard C++ by way of COM technology as an intermediate layer, and that worked, but with a ridiculous amount of complex non-general support code.
As another example, using C++ in web pages is very much an impedance mismatch, so to speak. Again Microsoft is the main player who has attempted this, with their ActiveX technology. It turned out to be a good way ensure that PCs all over the world got infested with malware, because C++ is not the kind of language where it's easy to constrain what the code can do. As far as I know only Microsoft's own Internet Explorer browser ever supported ActiveX in web pages. And as of Windows 10 the IE browser is being phased out and replaced by the newer Edge browser, that does not support ActiveX in web pages.
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 am an experienced Mac/iOS developer, but I initially started to program in C++. I haven't touched C++ for years, and now, it's time to do so, because I want to have multi platform support for a new kind of file type I am trying to create.
C++ has evolved over the years, to what I find is a very bloated mess of 3rd party libraries and an effort by the C++ standards committee to control the evolution of the language. Hence, my questions.
I want to have as much advanced tools at my disposal as possible, and at the same time conform to standards as possible. My main development platform is OS X Lion, and I have access to a fedora 16 installation. I want to target Lion and above, Windows XP and above, and the latest Linux kernels, so backwards compatibility is not an issue here.
One choice is to install the Boost libraries to my machine, but to what I have experienced a long time ago it is a very painful process, with compile-time errors and quirks that have to be done to OS X. I don't know if that experience will be the same if I try that now. The other choice is to stick with TR1 which Lion currently offers. However, TR1 is not a standard as I understand, it is a de facto popular implementation of things that were scheduled to be done in C++11. That way I lose a lot of advanced features that Boost offers.
With these two options in hand, what is the recommended way to have advanced C++ features at your disposal and conform to standards as much as possible? If it's Boost, is it recommended to compile Boost as static libraries in order to avoid installing Boost on end-user machines?
What is the current support of Xcode 4.3 for C++11 features?
I would appreciate any comments on the above questions as well as any other thoughts on the matter. I am trying to get in sync with the current version and features of C++ and I begin to realize that this may not be so easy as I initially thought.
Thanks.
First of all, there are two very distinct parts in Boost:
those which are header-only
those which require compiled libraries
If you stick to header-only, it will definitely be much easier. Note that some libraries, like Asio, have two compilation modes (header-only and library). A number of useful libraries, however, such a boost::regex, do require a compiled library.
Second, C++11 support is moving fast. GCC 4.7 and the upcoming Clang 3.1 support all major features of the Standard, except from atomics (discussions are still ongoing on the best implementation strategies), so on Linux and Mac, things are great... however Visual Studio is lagging behind, and Microsoft is not really interested in moving fast, so on Windows support is minimal still (and advertised supported features are based on older versions of the Standard and not 100% compatible with the last version). It does not mean you cannot compile for Windows, merely that you should use Mingw or equivalent and thus forgo interaction with existing Windows DLLs.
Based on those two observations, I would recommend:
try to avoid C++11 for now if you want to interact with Windows DLL, otherwise I would recommend Clang (for its integration in XCode)
try to avoid Boost libraries and stick to the header-only parts (there is still much goodness)
if you want libraries, you can use DLL as long as you distribute them alongside the executable or use a package system or whatever, but it does present a greater difficulty (for the installation) than just static linking.
Note I'm not a Mac developer but I'd prefer Boost. Many C++11 standards are taken from it and you can expect more Boost features will be ported to C++ standards.
And as a Qt enthusiast: take a look at QtCore. It's very powerful and cross-platform.
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