I am using g++ version 4.5.2 .
Although I have include array header file
#include <array> it does not complain about the existence of the header file.
But when I do std::array<char , 16>,
the exception is thrown when I try to compile with g++
"array not a member of namespace std"
I searched on the net and saw a suggestion somewhere to use std::tr1::array<>,
but that did not help either ?
Does someone know what the problem is ?
You need to compile with GCC 4.6 and later to have C++11 features and you need to set the right flag to -std=C++0x or in GCC 4.7 -std=C++11
Related
I am working on a programming assignment, and it requires me to read a json file. To do so I am using https://github.com/nlohmann/json. When I #include "json.hpp" I get errors. I am guessing this is occurring because I need to encode it correctly for Mac. But I am not sure how to do so! Would anyone be able to help me with the same?
Here is an image of my compiler ouput
That library is written in C++11 (as per the documentation).
Your compiler is not using C++11 by default. To fix it will require something like this:
g++ -std=c++11 ...
If it complains that -std=c++11 is not valid, try -std=c++0x (an obsolete synonym).
I am using lasted mingw w64 compiler (from April 2014, with g++ 4.9.0), and I am unable to compile smart pointers (including <memory> header ), either with -std=c++0x or gnu0x. I get always same error:
error: 'shared_ptr' in namespace 'std' does not name a template type
I was told that lastest gcc was able to compile that. But this don't seem to be my case. I am missing anything?
Remove the -ansi compiler flag. For C++, it is equivalent to -std=c++98.
It comes after -std=c++11 and is overriding it. std::shared_ptr did not exist in C++98
I want to use the std::move function in <utility>. So I put an include for this at the top of my code along with all the others. However, when I use the move function eclipse underlines it as red and it won't compile. I know I am using c++ 11 since I can declare move constructors however, this won't work. I am using GCC to compile and I used the -std=c++11 option. I also put this in my linker. Before that #include <utility> would not show up. Do I need to include something else?
Here is the basic prolbem. std::move(...) does not seem to be defined.
#include <utility>
#include <vector>
int main()
{
std::vector<int> v1;
std::vector<int> v2;
v1 = std::move(v2); // Function move could not be resolved.
return 0;
}
Also here are the options I have set on my compiler -O3 -g3 -Wall -c -fmessage-length=0 -std=c++11
Your code is OK. But you should update your gcc to newest version. Gcc 4.7.2 don't have everything implemented from c++11 standard.
Are you sure is not defined? Perhaps your error is at linking time cause the linker cannot find the library?
Whether the case, you have to add library paths to your eclipse project settings as well as include paths.
I've compiled your example and after executing ldd on the generated binary I can see there are linking dependences, I hope this help:
--EDIT
If you would like to use MinGW GCC 8.4.1 and threads/mutex/futures/atomics do not download the Win32 threader version insted download the Posix version.
--EDIT
My installation of MinGW is as follows:
x32-4.8.1-release-win32 (as the threader) - sjlj rev 5
I have unpacked and correctly confirmed that MinGW GCC 4.8.1 (revision 5) is installed in C:\MinGW\mingw32. I have set up Code Blocks to look for the latest and greatest compiler in the correct path (this I am sure of). I can compile a normal program using #include iostream. Ok now when I try and run a program using #include thread it gives me "error: 'thread' is not a member of 'std'".
Now here is what I have done and what I have tried:
I am following a sort of template or tutorial here at cplusplus.com.
I have the code exactly as it is presented on the webpage (towards the bottom).
I have tried, in Code Blocks, to use Compiler flags "Have g++ follow the C++11 ISO language standard -std=c++11".
I have also tried the flag "Have g++ follow the coming C++0x ISO language standard -std=c++0x"
I have tried both at the same time and one at a time, no mas.
I have also tried those commands manually.
Another command I tried manually was -std=gnu++11 which was recommended in the thread header.
--EDIT
It seems like __cplusplus is < 201103L which is stated (or rather defined) in the thread header.
This only happens when I manually use -std=c++11, for some reason C::B removes it if it was manually stated so I must use a check box to use this flag...
--EDIT
My compiler settings under the Toolchain Executables tab are as follows:
C compiler: i686-w64-mingw32-gcc-4.8.1.exe
C++ compiler: i686-w64-mingw32-c++.exe
Linker for dynamic: i686-w64-mingw32-c++.exe
Linker for static: ar.exe
Debbuger: GDB/CDB debugger: default
Resource compiler: windres.exe
Make Program: mingw32-make.exe
I have tried using other executables in the bin folder and still no luck...
I'm starting to wonder if GCC supports C++11 or threading !?
Has anyone been able to get threads to work with MinGW GCC, Code blocks or in general?
If so how did you do it? Any links that might help? Any advice?
P.S. I know there are other ways of threading like posix or other SDK's like SFML (I have successfully tried threading with this). But I want to use GCC threading and I'm quite baffled as to why it is so hard to do seeing as all the necessary files are there...
--EDIT
I have found that when I manually compile the program outside of Code Blocks I still get the same errors, whether I use g++ c++ or i686-w64-mingw32-g++/c++
here is the command I run to build the files:
C:\MinGW\mingw32\bin>g++.exe -D__GXX_EXPERIMENTAL_CXX0X__ -o0 -g3
-Wall -c -fmes sage-length=0 -std=c++11 -Wc++11-compat -o obj\Debug\main.o "F:\C Projects\Code Blocks\thread\main.cpp"
still returns error: 'thread' is not a member of 'std'
Could this be a bad build? I will try other revisions...
--EDIT
probably to late for an answere, but here is what worked for me:
1. Get x86_64-w64-mingw32-gcc-4.8-stdthread-win64_rubenvb.7z from:
http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/rubenvb/gcc-4.8-experimental-stdthread/
2. Setup a new compiler in codeblocks with
x86_64-w64-mingw32-gcc-4.8.1.exe
x86_64-w64-mingw32-g++.exe
x86_64-w64-mingw32-g++.exe
ar.exe
windres.exe
mingw32-make.exe
3. Set the new compiler for your project
Right click in your project -> build options
Select the new compiler
Under compiler falgs check -std=c++0x and -std=c++11
Under Other options set -std=gnu++11
4. Have fun with c++11 concurrency
Hope that works for you also, as an alternative you can just use visual studio.
I think you meant GCC 4.8.1 - the answer is yes, it supports a set of C++11 features including partial multi-threading support. Please visit http://gcc.gnu.org/releases.html to see supported set.
gcc 4.8.1 is C++11 feature complete. I cannot speak to the Windows implementation but certainly on Linux and OS X it works as advertised, including all the concurrency functionality. I just #include <thread> and call g++ -std=gnu++11 and it works. Here's a minimal piece of code that compiles just fine:
#include <iostream>
#include <thread>
#include <mutex>
std::mutex mx;
int i;
void thrfunc();
int main(void)
{
i=0;
std::thread thr1(thrfunc),thr2(thrfunc);
thr1.join();
thr2.join();
return 0;
}
void thrfunc()
{
mx.lock();
i++;
std::cout << std::this_thread::get_id() << " i: " << i << std::endl;
mx.unlock();
}
I had the same issues, I installed the lates MinGW-Builds
http://sourceforge.net/projects/mingwbuilds/files/mingw-builds-install/
and set my toolchain executables to:
x86_64-w64-mingw32-gcc-4.8.1.exe
x86_64-w64-mingw32-g++.exe
x86_64-w64-mingw32-g++.exe
ar.exe
windres.exe
mingw32-make.exe
I hope this helps.
I am using netbeans 7.2.1 with minwg compiler. I am getting the following error messages when trying to build the application:
error: 'function' in namespace 'std' does not name a type
error: 'bind' is not a member of 'std'
although I included functional.h in the begining of the file, and I am using 'function' and 'bind' in the form of: std::function and std::bind
Where is the problem? Is it in the compiler or there is something missing? I remember that I compiled and ran the same application successfully on visual studio 2010.
It is not functional.h, it is just functional.
#include <functional> //without .h
Note that std::function and std::bind come with C++11 only. So you might have to upgrade your compiler in case you have not done yet.
Also, compile your code with -std=c++11 option:
$ g++ -std=c++11 file.cpp
That should work if you've upgraded your compiler. If your compiler is a bit old, you can also try -std=c++0x instead.
You need to include the header functional. It is available in C++11. If you are still having problems, then your compiler may not support C++11 yet. Try upgrading.
You can also use boost::bind:
#include <boost/bind.hpp>