I installed gcc49 on FreeBSD10.1. I am trying to use it for C++11 development. However, every time I'm compiling some C++11 valid code (yes I use -std=c++11) that uses specific math functions, it spits out errors, such as
error: std::round is not a member of std
/usr/include/math.h
For example, here:
#include <cmath>
#include <iostream>
int main()
{
std::cout << std::round(10.1) << std::endl;
}
So it seems it tries to use the old include files that came with FreeBSD, and not the ones corresponding to the new gcc from /usr/local/lib/gcc49/include
I tried setting CPLUS_INCLUDE_PATH to /usr/local/lib/gcc49/include with no luck, the system still tries to search /usr/include instead.
I saw that this may be a bug in FreeBSD g++,
Getting GCC in C++11 mode to work on FreeBSD
however even using the -D_GLIBCXX_USE_C99 as suggested in https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=194929
doesn't fix the problem for math functions.
The weirdest thing is that I can compile any other C++11 functions not from <cmath>, like std::stol, but have to use the -D_GLIBCXX_USE_C99 flag as mentioned in the bug report above.
Any idea how to make g++ fully functional with C++11 on FreeBSD 10.1?
1 Answer 1
It works out of box on 10.1, you shouldn't need any hacks. Of course, it only works with -std=c++11, as std::round is only available since 11 standard (see http://en.cppreference.com/w/cpp/numeric/math/round).
$ freebsd-version -ku
10.1-RELEASE
10.1-RELEASE
$ cat test.cc
#include <cmath>
#include <iostream>
int main() {
std::cout << std::round(10.1) << std::endl;
}
$ g++5 -std=c++11 -o test test.cc
$ ./test
10
Note: gcc was compiled from ports, package was reported to not work. Probably because packages for 10.1 are at the time of writing compiled on 10.0, which still had c++11 compatibility issues.
4 Comments
pkg install lang/gcc49. The code above didn't compile. How did you install the compiler? I did not compile it from source.pkg install because I just wanted to see if my app is portable without issues on FreeBSD. You should mention in the answer that you used the ports, as using pkg install doesn't seem to work.
roundis not a member ofstd. Use it withoutstd::. It may be a GCC extension.