3

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?

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • Do you ask for C++11? – Deduplicator Feb 09 '15 at 17:11
  • @Deduplicator yes, I need C++11 support – vsoftco Feb 09 '15 at 19:51
  • @mvw, I tried that, same issue, it seems to not being able to recognize the math part of the C++11 standard library – vsoftco Feb 09 '15 at 19:52
  • @vsoftco `round` is not a member of `std`. Use it without `std::`. It may be a GCC extension. – enedil Feb 10 '15 at 13:15
  • http://en.cppreference.com/w/cpp/numeric/math/round it is, since C++11. – vsoftco Feb 10 '15 at 13:17
  • @mvw I just used the latest stable `10.1/amd64` version (downloaded the installer image from FreeBSD's web site https://www.freebsd.org/where.html) inside a VirtualBox, and installed g++49 with `pkg install lang/gcc49`. I also modified `/etc/make.conf`, tried setting `CPLUS_INCLUDE_PATH `, but with no luck. – vsoftco Feb 10 '15 at 13:20
  • `gcc` 5 o `gcc 4.9`? – edmz Feb 10 '15 at 13:28
  • @black gcc4.9 (it has full support for most of the C++11 stuff, has no problem in compiling the above on any other platform) – vsoftco Feb 10 '15 at 13:32
  • @vsoftco I run into your bug as well. Tricky. – mvw Feb 10 '15 at 15:01
  • @mvm, yes, especially since it pretty much makes C++11 un-usable on FreeBSD – vsoftco Feb 10 '15 at 15:22
  • @vsoftco I compared the output after the pre-processing stage using -E from the FreeBSD and from the Debian machine. I found nothing obvious, alas. Regarding the header file /usr/include/lib/gcc49/include/c++/cmath, the part with `using std::round` is not included in case of FreeBSD. I lack knowledge of the header system to know how to adapt this without breaking other stuff. – mvw Feb 10 '15 at 15:56
  • @mvw I think the FreeBSD's C++11 library implementation is just broken... – vsoftco Feb 10 '15 at 16:42

1 Answers1

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.

Elisey
  • 59
  • 3
  • Sorry, I marked the question with gcc5 but I used gcc49. Will try gcc5. – vsoftco Feb 24 '15 at 22:37
  • I just used the latest stable 10.1/amd64 version (downloaded the installer image from FreeBSD's web site freebsd.org/where.html) inside a VirtualBox, and installed g++49 with `pkg install lang/gcc49`. The code above didn't compile. How did you install the compiler? I did not compile it from source. – vsoftco Feb 25 '15 at 21:03
  • I build everything from ports. Packages could be broken because they are built on 10.0. – Elisey Feb 25 '15 at 21:11
  • thanks, I should then compile the ports. I tested it quickly using `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. – vsoftco Feb 25 '15 at 21:26