Getting GCC in C++11 mode to work on FreeBSD - c++

How do I get a working GCC-based C++11 setup on FreeBSD 10? It seems that the standard library that comes with recent GCC versions on FreeBSD is broken. I've installed the port gcc49 and then try to compile this:
#include <string>
int main()
{
auto str = std::to_string(42);
str = std::to_string(42ull);
str = std::to_string(4.2);
str.clear();
return 0;
}
This gives me an error:
g++49 -v -std=c++11 foo.cc
Using built-in specs.
COLLECT_GCC=g++49
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/lto-wrapper
Target: x86_64-portbld-freebsd10.0
Configured with: ./../gcc-4.9-20141022/configure --disable-nls --enable-gnu-indirect-function --libdir=/usr/local/lib/gcc49 --libexecdir=/usr/local/libexec/gcc49 --program-suffix=49 --with-as=/usr/local/bin/as --with-gmp=/usr/local --with-gxx-include-dir=/usr/local/lib/gc
c49/include/c++/ --with-ld=/usr/local/bin/ld --with-pkgversion='FreeBSD Ports Collection' --with-system-zlib --with-ecj-jar=/usr/local/share/java/ecj-4.5.jar --enable-languages=c,c++,objc,fortran,java --prefix=/usr/local --mandir=/usr/local/man --infodir=/usr/local/info/gcc49 --build=x86_64-portbld-freebsd10.0
Thread model: posix
gcc version 4.9.2 20141022 (prerelease) (FreeBSD Ports Collection)
COLLECT_GCC_OPTIONS='-v' '-std=c++11' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/local/libexec/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/cc1plus -quiet -v foo.cc -quiet -dumpbase foo.cc -mtune=generic -march=x86-64 -auxbase foo -std=c++11 -version -o /tmp//ccbNFhtI.s
GNU C++ (FreeBSD Ports Collection) version 4.9.2 20141022 (prerelease) (x86_64-portbld-freebsd10.0)
compiled by GNU C version 4.9.2 20141022 (prerelease), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/../../../../../x86_64-portbld-freebsd10.0/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/local/lib/gcc49/include/c++/
/usr/local/lib/gcc49/include/c++//x86_64-portbld-freebsd10.0
/usr/local/lib/gcc49/include/c++//backward
/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/include
/usr/local/include
/usr/local/lib/gcc49/gcc/x86_64-portbld-freebsd10.0/4.9.2/include-fixed
/usr/include
End of search list.
GNU C++ (FreeBSD Ports Collection) version 4.9.2 20141022 (prerelease) (x86_64-portbld-freebsd10.0)
compiled by GNU C version 4.9.2 20141022 (prerelease), GMP version 5.1.3, MPFR version 3.1.2, MPC version 1.0.2
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 8405316ee381c37148f55a17e42da47a
foo.cc: In function 'int main()':
foo.cc:5:14: error: 'to_string' is not a member of 'std'
auto str = std::to_string(42);
^
foo.cc:6:9: error: 'to_string' is not a member of 'std'
str = std::to_string(42ull);
^
foo.cc:7:9: error: 'to_string' is not a member of 'std'
str = std::to_string(4.2);
^
The default include search path is /usr/local/lib/gcc49/include/c++/, which does contain the string header. Makes sense, because the gcc49 port ships with a corresponding libstc++. But it seems broken, per the above error.
Is this a known problem? Has anyone gotten a working setup of a GCC-based toolchain for C++11 on FreeBSD?
(Note: I'm aware that Clang is the default compiler on FreeBSD 10. But in this case I'm specifically looking to support a GCC-based toolchain.)

Turns out that adding the following flags to g++ will make it work:
-D_GLIBCXX_USE_C99 -D_GLIBCXX_USE_C99_MATH -D_GLIBCXX_USE_C99_MATH_TR1

Related

g++ libstdc++.so.6: version `CXXABI_1.3.9' not found after upgrade to gcc version 7.3.0 from 4.8.5

i guess the problem is that the g++ version 7.3.0 still using the old stdc lib , im not sure ..
how can i check ? and how can i upgrade to new versions as the runtime error im getting of the app
looks like this :
./a.out
./a.out: /lib64/libstdc++.so.6: version `CXXABI_1.3.9' not found (required by ./a.out)
./a.out: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found (required by ./a.out)
This is what i have now :
g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/x86_64-pc-linux-gnu/7.3.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ./configure --disable-multilib --enable-languages=c,c++
Thread model: posix
gcc version 7.3.0 (GCC)
this is my compile/link flags :
g++ echo.cpp src/*.cpp -Wall -O0 -g -std=c++14 -I/home/vagrant/libuv/include -Isrc -L/home/vagrant/libuv/build -lssl -lcrypto -Wl,--no-as-needed -Bstatic -luv_a -ldl -lpthread
and those are my stdc files after searching :
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/32/libstdc++.so
/usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.so
/usr/local/lib64/libstdc++.so
/home/vagrant/gcc-7.3.0/x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so
/home/vagrant/gcc-7.3.0/stage1-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so
/home/vagrant/gcc-7.3.0/prev-x86_64-pc-linux-gnu/libstdc++-v3/src/.libs/libstdc++.so
You dynamically linked the C++ standard library, then replaced it with a different version.
The version now installed on your computer is not compatible with the one your program requires, so it cannot run.
Rebuild your project, so that it links against the newer version, or downgrade GCC.
In future you may wish to consider statically linking the standard library instead, so that this particular version is just bundled with your executable. But this has downsides (which you can research).

Compile with c++17 mac

I can't compile with -std=c++17, I got :
error: invalid value 'c++17' in '-std=c++17'
However I update Xcode and clang.
My Clang version is:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin`
And I load the newest header like optional, I have to do
#include <experimental/optional>
instead of
#include <optional>
Xcode brings its own complete toolchain, including headers and the actual compiler.
Apple LLVM version 9.0.0 (clang-900.0.39.2) (which ships with Xcode 9.2) does not support the usage of the flag -std=c++17 since its too old. The optional header is only included under the folder experimental/. Which is why you need to #include <experimental/optional>
In order to compile your program with c++17 support using the compiler which comes with Xcode 9.2 you need to use the -std=c++1z flag.
Xcode 9.3 will be shipped with Apple LLVM version 9.1.0 (clang-902.0.30) which has support for the -std=c++17 flag. However the optional header is as of today still under the experimental/ subdirectory. This might change during the betas.
Here is what I get with this tests:
#include <experimental/optional>
int main(int, char* []) {
return 0;
}
g++ -std=c++17 -o test test.cpp
error: invalid value 'c++17' in '-std=c++17'
g++ -std=c++1z -o test test.cpp
Did you try the c++1z argument?
Also of note my test compiles without the -std=c++1z argument provided.
I think I'm on a newer version of OSX than you:
Target: x86_64-apple-darwin17.4.0
You should use -std=c++1z as flag.
libc++ with c++17 support since macos 15
-std=c++1z also works on Apple LLVM version 8.1.0 (clang-802.0.42)

C++ Compile error on NetBSD '::system' has not been declared

I have some C++ code that compiles nicely on Linux but so far I'm having trouble getting it to compile correctly on NetBSD.
These are my includes:
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
#include <boost/regex.hpp>
I compile with this command:
g++ -v -O2 -fno-strict-aliasing -I /usr/pkg/include \
barefoot.cpp -o barefoot -L /usr/pkg/lib/ -lboost_regex \
-L /usr/pkg/lib/ -lboost_regex -lpthreads
Which renders this output:
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64--netbsd
Configured with: /usr/src2/tools/gcc/../../external/gpl3/gcc/dist/configure --target=x86_64--netbsd --enable-long-long --enable-threads --with-bugurl=http://www.NetBSD.org/Misc/send-pr.html --with-pkgversion='NetBSD nb2 20111202' --enable-__cxa_atexit --with-tune=nocona --with-mpc=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-mpfr=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --with-gmp=/var/obj/mknative/amd64/usr/src2/destdir.amd64/usr --enable-tls --disable-multilib --disable-symvers --disable-libstdcxx-pch --build=x86_64-unknown-netbsd5.99.56 --host=x86_64--netbsd
Thread model: posix
gcc version 4.5.3 (NetBSD nb2 20110806)
COLLECT_GCC_OPTIONS='-v' '-O2' '-fno-strict-aliasing' '-I' '/usr/pkg/include/' '-o' 'a.out' '-L' '/usr/pkg/lib/' '-mtune=nocona' '-march=x86-64'
/usr/libexec/cc1plus -quiet -v -I /usr/pkg/include/ barefoot.cpp -quiet -dumpbase barefoot.cpp -mtune=nocona -march=x86-64 -auxbase barefoot -O2 -version -fno-strict-aliasing -o /var/tmp//cc9Dcmwi.s
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
#include "..." search starts here:
#include <...> search starts here:
/usr/pkg/include/
/usr/include/g++
/usr/include/g++/backward
/usr/include/gcc-4.5
/usr/include
End of search list.
GNU C++ (NetBSD nb2 20110806) version 4.5.3 (x86_64--netbsd)
compiled by GNU C version 4.5.3, GMP version 5.0.2, MPFR version 3.0.1-p4, MPC version 0.9
GGC heuristics: --param ggc-min-expand=99 --param ggc-min-heapsize=131007
Compiler executable checksum: a34e7d170f4dd8d4687d2b62e8dca4b7
In file included from /usr/include/g++/bits/gthr.h:166:0,
from /usr/include/g++/ext/atomicity.h:34,
from /usr/include/g++/bits/ios_base.h:41,
from /usr/include/g++/ios:43,
from /usr/include/g++/istream:40,
from /usr/include/g++/fstream:40,
from barefoot.cpp:29:
/usr/include/g++/bits/gthr-default.h:130:9: error: 'pthread_detach' was not declared in this scope
/usr/include/g++/bits/gthr-default.h:130:1: error: invalid type in declaration before ';' token
/usr/include/g++/bits/gthr-default.h: In function 'int __gthread_detach(pthread_st*)':
/usr/include/g++/bits/gthr-default.h:668:46: error: '__gthrw_pthread_detach' cannot be used as a function
In file included from /usr/pkg/include/boost/regex/v4/regex_workaround.hpp:25:0,
from /usr/pkg/include/boost/regex/v4/regex.hpp:32,
from /usr/pkg/include/boost/regex.hpp:31,
from barefoot.cpp:32:
/usr/include/g++/cstdlib: At global scope:
/usr/include/g++/cstdlib:132:11: error: '::system' has not been declared
I tried adding -lpthread, but that resulted in the same output. Not sure what to try next.
I do not know exactly why you are facing this problem, but I can provide you with some next steps to try.
Notice that the first error is that pthread_detach is missing. The system header files will typically make sure whatever headers they need are included, but you can try adding <pthread.h> above all the other headers.
If that fails, you need to figure out which file got included as a result of adding #include <pthread.h>, and make sure pthread_detach is present inside it (it really should be there somewhere).
Assuming it is there, there must be some conditional compilation that is causing it to not be visible to your source code. Find the conditional guards, and what macro values affect it.
Then, you will need to see why your system is defining the macro values in a way that causes the function pthread_detach to not be visible.
With this investigation, you may discover that you are not including the right <pthread.h> file. This may be due to system include directories conflicting with the include directories you want to use.
This answer (it is towards the bottom) to this question shows how to display the predefined macros for the g++ compiler.

‘cout’ is not a member of ‘std’ & ‘cout’ was not declared in this scope

I realize that there are several duplicates like this but none of them have worked for me so far.
I am trying to compile a C++ very simple program on Ubuntu using g++ but it is giving me scope errors.
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
This gives me this error:
sudo g++ -v test.c
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ was not declared in this scope
I also tried defining the scope as many other posts say, but that also didn't work, but gave me a different error:
#include <iostream>
int main()
{
std::cout << "Hello world";
}
Gives error:
test.c: In function ‘int main()’:
test.c:6:3: error: ‘cout’ is not a member of ‘std’
Most of the suggestions online suggest "using namespace std;", "#include " and "std::cout".
So I tried all 3 together, still no luck :(
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello world";
}
gives error:
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ is not a member of ‘std’
I have gone through several forums online but none of them seem to work for me :(
This is a part of a bigger issue because of which one of my linux make doesn't work.
Btw, I am using g++ and not gcc as a few posts messed up.
EDIT 1:
I changed the name to .cpp, and execute without sudo. Here is the verbose output:
pranoy#pranoyubuntu1210:~/Desktop/SIP/SIPp/sipp-3.3$ g++ -v test.cpp -o test
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.7/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -version -fstack-protector -o /tmp/cczzibvL.s
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 521527ea42f0901bf839bcaad0cb13e6
test.cpp: In function ‘int main()’:
test.cpp:5:3: error: ‘cout’ is not a member of ‘std’
This isn't the case for this, but could be an answer to "error: ‘cout’ is not a member of ‘std’". I came here looking for help, so this might help someone else.
If you have a header file (header.h) where the last function/class declaration does not have a semi-colon after it:
#include <string>
function(std::string str)
And your c++ file includes this before iostream:
#include "header.h"
#include <iostream>
The function/class (function in this case) messes up the declarations in iostream. #include basically just pastes the text into the code. Adding the semi-colon can fix the problem.
What you've written works absolutely fine on my Ubuntu system, with the same version of g++.
It sounds like you haven't installed all of the necessary files for the C++ environment, or something isn't quite right with it. Try this:
$ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib
(Run dpkg -l | grep libstdc++ to get the exact version of libstdc++ if the above fails)
For C++ programs, use g++, not gcc. If you get errors that suggest your compiler cannot find the standard library, that's because you probably used gcc.
(C++, Linux Terminal, no compiling error, but no prints into the Terminal), maybe try:
$ g++ yourcode.cpp
$ ./a.out
first line: g++ compiles your code.
second line: runs your compiled code.
Rename file extension with .cpp .
Compile source code by command
gcc source.cpp -o output.out
GCC will automatically compile it as a C++ program.

MinGW error about TITLEBARINFO not declared

I'm using the latest MinGW:
MINGWBASEDIR=C:\mingw
gcc version 4.7.0 (GCC)
gcc version 4.7.0 (GCC)
GNU gdb (GDB) 7.5
GNU ld (GNU Binutils) 2.22
GNU windres (GNU Binutils) 2.22
GNU dlltool (GNU Binutils) 2.22
GNU Make 3.82.90
#define __MINGW32_VERSION 3.20
#define __W32API_VERSION 3.17
This is the source file (test.cpp):
#include <stdio.h>
#include <windows.h>
int main(int Argc,char** Args) {
printf("%d",sizeof(TITLEBARINFO));
}
This is how I compile it:
g++ -c test.cpp
And this is the error message reported by MinGW:
C:\temp>g++ -c test.cpp
test.cpp: In function 'int main(int, char**)':
test.cpp:5:22: error: 'TITLEBARINFO' was not declared in this scope
TITLEBARINFO is supposed to be declared in winuser.h which windows.h includes.
I've checked these 2 header files in mingw/include dir, the definition of TITLEBARINFO is truly there. But I still get this compiler error, how strange!
gcc -c -D_WIN32_WINDOWS=0x0410 test.cpp
MinGW apparently defaults to 0x0400.