How can I write cross-platform c++ programs on my mac? - c++

My university professor has given a requirement for us in his c++ class: We must write our programs using the gnu C++ compiler (GCC). I understand that after installing xcode, I get an apple version of gcc. However, this is not fully cross compatible as I understand. So I have tried to install the gnu gcc compiler by using mac ports, but I have been unsuccessful. After doing:
port install gcc47
When I go to the terminal and run:
gcc47 -v
I get "command not found"
When I run:
gcc -v
I get:
gcc version 4.2.1 (Apple Inc. build 5666) (dot 3)
Showing that I am still using apple's version. The part I am not understanding is:
How do I install gcc (the gnu version, not apple's version)
How do I use it with an ide, such as eclipse?

The version of GCC you use (Apple or gnu native) should be irrelevant for you to proceed.
Writing portable C++ has nothing to do with the compiler--outside of ensuring your compiler does it's best to adhere to the C++ standard. Luckily enough, GCC is one of the best C++ compilers out there.
You're not looking to do cross-platform C++ per-se, because true portable C++ is somewhat of a pain to write given the various state of C++ compilers for various systems (for example the CC on VAX/VMS doesn't support templates...). I suspect what you want to do is ensure the code you write on your mac, can be compiled by your prof. As long as GCC can handle it on your mac, it should be fine.
So to your specific questions:
Your first question is unnecessary, as you can use apple's supplied g++ to compile your code.
For your second question, I would highly recommend against using an IDE while trying to learn. IDE's offer some really great time saving features, but they hide some important aspects, that I believe are worth struggling with early in your learning process, and which will help strengthen your C++ skills. And while things have certainly have gotten better, some IDE's were notorious for creating non-portable C++ (ie using void main()).
If you are still set on using Eclipse, or XCode, since it doesn't matter which GCC installation you use, the default setup should work just fine.

I've run into troubles installing gcc47 via ports in the past, but gcc46 went as expected. You may also want to install gcc_select.
From there, you can use gcc_select, or specify the path explicitly in Eclipse's toolchain editor.
Other notes:
If you don't need C++11 features, then Apple's GCC 4.2.1 should work fine. Perhaps you can detail why you can't use it. Basically, Apple's added some extensions to the toolchain in some cases (marked APPLE_ONLY), and you have good control of what options are enabled/disabled.
Recent versions of Clang which ship with Xcode handle C++ well (including some support for C++11). There are some advanced things in GCC that I miss when using Clang, but Clang's current C++ support is really quite good.

Related

"Include What you use"

I read about tool called "include-what-you-use" which can help clean superfluous includes
from source code. I understood that there is a version for compiler LLVM(clang) and version for GCC.
My questions are:
Why this tool is compiler-dependent and not "cross-platform" for compilers. Why from the beginning the creators of the tool didn't make it compiler-independent? Is it related to the special implementation it has or something like that?
If I want to take a version of the tool compatible for LLVM and I want to make it compatible with GCC (since I'm working with GCC), what do I have to do for that?
For the most part, Include-What-You-Use should be able to handle any valid C++ codebase, regardless of whether that codebase was written with gcc or clang in mind. I recently had the occasion to run Include-What-You-Use on a very large codebase that was usually compiled with gcc and it worked fine. So in that sense it is already compatible.
That said, it might not work perfectly. It's likely that some of the information it provides will be wrong, even if it's a clang codebase. So always verify the output manually.
why this tool is compiler-dependent and not "cross-platform" for compilers. Why from the beginning the creaters of the tool didn't make
it compiler-independent ? is it related to the special implementation
it has or something like that ?
Reason is simple, clang has is more modern fresh and has better modular architecture. As a result is is much easier to create tools using clang modules.
That is why clang was first which had address sanitizer and have more cool sanitizers. And this is why when someone creates new tool for C++ stars from clang.
If i want to take a version of the tool compatible for llvm and i want to make it compatible with gcc(since i'm working with gcc). What
i have to do for that ?
clang was created since Apple was not happy with gcc. So when it was written it supposed to be as much compatible with gcc as possible, since there was lots of code which was verified with gcc.
Since clang is mature now and provides new own features, there might be small differences with gcc (for example different bugs in implementations of new C++ standards), but in most common code there should be no problem.
IWYU should work without problems on both compilers. My coworker used it on large project build with 3 compilers (clang, gcc and VS) and it worked like a charm.
The tool itself needs parts of the compiler! It is sitting somewhere between reading the source and parsing it. LLVM provides an API which is used for the tool. The tool itself is not standalone but a plugin to the clang/llvm. As this, it needs the clang/llvm.
The modifications which will be done by the tool are fully compatible to every c++ compiler. And also the plugin in combination with clang/llvm should be able to parse more or less every code base independent of used other compilers. There might be some strange macros which are supported by other tool chains, which llvm might be struggle with. But that should be a rare case at all.

Many questions about the various C++ compilers available to me on OS X

I am having trouble understanding the different compilers that are available to me.
I mainly use Xcode for writing and compiling, and in Xcode's preferences, there are all of these options for C++ compilation:
C++ Language Dialect:
C++98[-std=c++98] through C++14[-std=c++14]
GNU++98[-std=gnu++98] through GNU++14[-std=gnu++14]
C++ Standard Library:
libstdc++ (GNU C++ standard library)
libc++ (LLVM C++ standard library with C++11 support)
Can someone explain what exactly all of that ^ is?
I understand that (and correct me if I'm wrong), that apple no longer distributes GCC with Xcode and use Clang instead?
If that were the case, then why does Xcode have the option for GNU C++ standard library? Doesn't GNU make GCC?
What compiler is invoked when I run C++ code in my local terminal with g++ filename.cpp?
Is there any way to make sure that this g++ "compiler" is up to date?
What's the difference between compiling with g++ in the terminal and using Xcode?
Also, what would be the difference if I tried running C++ programs with Clang?
My class requires us to test our programs on the department's server's compiler via ssh from my terminal. The server is a Unix machine and I know that its compiler is GNU's GCC compiler and we also access it using g++. Does this mean that the local g++ in my terminal is also GCC?
edit: Grammar
1.
C++ language dialect
C++98[-std=c++98] through C++14[-std=c++14]
GNU++98[-std=gnu++98] through GNU++14[-std=gnu++14]
The C++ language has evolved over time. These are the various versions of the language that are available to you. If you have to be compatible with something old, you might be forced to use an old one. Otherwise you'd probably want ot use the newest available, which is c++14 in the list above. 14 stands for 2014, 98 for 1998 - it is supposed to represent the year that version of the standard was blessed.
In addition to standard C++ there are non-standard extensions. Gnu is a compiler "manufacturer", the "GNU" above is the non-stanadard extensions as specified by GNU for a particular documented version.
C++ Standard Library:
libstdc++ (GNU C++ standard library)
libc++ (LLVM C++ standard library with C++11 support)
In addition to the base language, the standard library is also a part of the standard. These are two different implementations of the standard library. The first is by GNU, the second by llvm. llvm are a different compiler manufacturer.
On osx you'd probably use libc++ as I believe the llvm compiler (clang++) is now standard there. The llvm compiler will support the gnu extensions if you need them. You probably don't. Just use the latest version of whatever is default.
Yes
two different pieces, the compiler and the standard library. You can use clang++ with libstd++
g++ -v will tell you
not really. Update xcode to the newest or start looking at homebrew or ports to get the latest and greatest they package of whatever compiler you like.
Probably the same compiler, you can set it either to point at any compiler you have installed. So what each points at is your choice.
both g++ and clang++ are standards compliant. You're unlikely to notice much difference. They will complile the same source files into equivalent binaries.
run g++ -v in any terminal to see exactly what it is.

Am I able to use C++11 in QNX?

I have some cross-platform code with some C++11 features like #include <thread> and others. I will soon be using a QNX 6.6 board, and I'm wondering if my code can compile on it, and which features will be available.
QCC is the official QNX C++ compiler, but I can't find any documentation citing which C++11 features, or even which C++ features in general, it supports. Is this a wrapper around GCC or its own thing? Either way, can I get or compile other compilers on this platform?
From what I know qcc is just using gcc internally. Because of this you can use all functionalities provided by the version of gcc that QNX decided to put into their package.
Judging from the release notes of QNX 6.6 gcc 4.7 is used:
GCC 4.7 tool chain, including support for the Intel Advanced Vector Extensions (AVX)
GDB 7.5
New: Binutils 2.24
Python 2.7.5, as a host-side tool
The release notes provide a link to information about gcc 4.7 but I think this link better shows which specific features are supported. There is too much information on the linked page, because of that I do not copy it. But in essence the link states:
GCC provides experimental support for the 2011 ISO C++ standard. This support can be enabled with the -std=c++11 or -std=gnu++11 compiler option ... GCC's C++11 mode implements much of the C++11 standard produced by the
ISO C++ committee
Whereas #Marged's answer seems to cover absolutely every important aspect of your question, I'd like to add that it is also possible to get more current versions of all GNU dev tools (like gcc, gdb or make..). This is officially provided by the QNX staff for "experimental use only", I guess1. But so far I've had only good experiences with them.
Check out QNX's updated Core Development Tools
(You need to register to the QNX community portal first to open the link)
You would then update your Linux dev system like that:
Get files from here
Binutils
GCC
Extract files into a new folder
(do not extract and overwrite existing folder directly since it might be that symbolic links don't get updated)
that should create the host and the target folder
Copy & paste the new files into the actual QNX folder and overwrite existing files
Optionally: update the config default file's value to the new compiler version
e.g. /../qnx650/host/linux/x86/etc/qcc/gcc/default
make sure 32bit libraries are installed (if not):
$ sudo apt-get install lib32stdc++6
if not installed correctly errors like the following can occur
$ i486-pc-nto-qnx6.5.0-g++: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory
1 Official text:"Stable versions of the Core Development Tools are included as part of QNX Momentics. You can download updated versions of these tools currently being developed through this project and benefit from their enhancements earlier!"
The version of gcc that is used by qcc does support much of the C++11 specification. I've used it. Just add -std=c++11 to the compiler line.
I did find one issue with C++11 support on QNX 6.6. This was in July 2014, so things may have changed. The support of vector initialization (std::vector<int> {1,2, 3, 4};) in the C++11 library shipped with QNX 6.6 was broken. Code would compile cleanly, but then fail rather cryptically when run. Again, I don't currently know the current status of this issue, so YMMV.

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).

gcc version 4.1.2 in mac os x

I am taking a programming class and we are required to use the gcc 4.1.2 compiler to compile our c++ projects. I will be creating my projects in xcode and can't find how to set that compiler. I went to the get info window on the project and hit the drop down under Compiler Version, however I do not have 4.1.2 on the list. It seems that this compiler is not installed on my computer. Does anybody know where I can download it and how I can set it as my system default gcc compiler for the term?
You can probably get away with using whatever version of GCC is on your Mac, and doing a final compile on the university machines as a check. In general, the user visible changes using a later version is stricter syntax checking, so you might do something on the Mac that won't pass a newer compiler, but that generally isn't too common. I haven't had to change more than a few lines on our 50k line codebase.
With the current developer tools, only gcc 4.0 and 4.2 are available. You could probably get gcc 4.1.2 via MacPorts, however, I'm not sure whether you can integrate it into XCode.
Before you try this, you should maybe first check with the class instructor whether it is really necessary to use exactly 4.1.2, or if it is OK to use 4.0 resp. 4.2 instead.