I compiled & installed gcc4.4 using macports.
When I try to compile using -> g++ -g -Wall -ansi -pthread -std=c++0x main.cpp...:
#include <thread>
...
std::thread t(handle);
t.join();
....
The compiler returns:
cserver.cpp: In member function 'int CServer::run()':
cserver.cpp:48: error: 'thread' is not a member of 'std'
cserver.cpp:48: error: expected ';' before 't'
cserver.cpp:49: error: 't' was not declared in this scope
But std::cout <<... compiles fine..
Can anyone help me?
gcc does not fully support std::thread yet:
http://gcc.gnu.org/projects/cxx0x.html
http://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html
Use boost::thread in the meantime.
Edit
Although the following compiled and ran fine for me with gcc 4.4.3:
#include <thread>
#include <iostream>
struct F
{
void operator() () const
{
std::cout<<"Printing from another thread"<<std::endl;
}
};
int main()
{
F f;
std::thread t(f);
t.join();
return 0;
}
Compiled with
g++ -Wall -g -std=c++0x -pthread main.cpp
Output of a.out:
Printing from another thread
Can you provide the full code? Maybe there's some obscure issue lurking in those ...s?
I had the same issue on windows using MinGW. I found wrapper classes for in on github mingw-std-threads Including
mingw.mutex.h, mingw.thread.h files to global MinGW directory fixed this issue. All I had to do is to include header file and my code stayed the same
#include "mingw.thread.h"
...
std::thread t(handle);
...
Drop -ansi, it means -std=c++98, which you obviously don't want. It also causes macro __STRICT_ANSI__ to be defined and this may change the behavior of the headers, e.g. by disabling C++0x support.
Related
i was just making a few changes to my program, when all of a sudden g++ complained with an internal compiler error.
Clang however compiles it without any problems and also does not give any warnings, that would indicate anything weird.
I distilled the problem down to this:
#include <functional>
template<typename T>
class A{
T someVar;
};
template<typename T>
class B {
int x;
std::function<A<double>(A<int>&)> someLambda = [&](A<int>& aInt){
int xVar = x;
A<double> aRet;
return aRet;
};
};
int main(int argc, char** argv){
B<int> a;
return 0;
}
I tried both GCC 4.9.2 and 4.8.4, with both failing (internal compiler error).
Flags I used:
g++ -std=c++11 -O0 -g -Wall main.cpp -o gccBin
clang++ -std=c++11 -O0 -g -Wall main.cpp -o clangBin
main.cpp: In instantiation of 'struct B<int>::<lambda(class A<int>&)>':
main.cpp:10:7: required from here
main.cpp:14:24: internal compiler error: in tsubst_copy, at cp/pt.c:12569
int xVar = x;
^
libbacktrace could not find executable to open
Please submit a full bug report,
with preprocessed source if appropriate.
See <http://gcc.gnu.org/bugs.html> for instructions.
Clang++(3.5.1) compiles it without a problem, as I mentioned.
I also tried multiple machines, everywhere the same.
Is there some kind of error I overlooked? I searched a bit on the internet and the only similar problems i could find should have been fixed by now (as the bugtracker states).
Could maybe someone try and run this code on their machine or give other advice?
Thank you,
Lazarus
It's a compiler bug. Just go ahead and file a bug report to the GCC dudes!
Okay, so I have
tmp.cpp:
#include <string>
int main()
{
std::to_string(0);
return 0;
}
But when I try to compile I get:
$ g++ tmp.cpp -o tmp
tmp.cpp: In function ‘int main()’:
tmp.cpp:5:5: error: ‘to_string’ is not a member of ‘std’
std::to_string(0);
^
I'm running g++ version 4.8.1. Unlike all the other references to this error that I found out there, I am not using MinGW, I'm on Linux (3.11.2).
Any ideas why this is happening? Is this standard behaviour and I did something wrong or is there a bug somewhere?
you may want to specify the C++ version with
g++ -std=c++11 tmp.cpp -o tmp
I don't have gcc 4.8.1 at hand , but in older versions of GCC,
you can use
g++ -std=c++0x tmp.cpp -o tmp
At least gcc 4.9.2 I believe also support part of C++14 by specifying
g++ -std=c++1y tmp.cpp -o tmp
Update:
gcc 5.3.0 (I am using the cygwin version) supports both -std=c++14 and -std=c++17 now.
to_string works with the latest C++ versions like version 11. For older versions you can try using this function
#include <string>
#include <sstream>
template <typename T>
std::string ToString(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
By adding a template you can use any data type too.
You have to include #include<sstream> here.
I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem
Okay, so I have
tmp.cpp:
#include <string>
int main()
{
std::to_string(0);
return 0;
}
But when I try to compile I get:
$ g++ tmp.cpp -o tmp
tmp.cpp: In function ‘int main()’:
tmp.cpp:5:5: error: ‘to_string’ is not a member of ‘std’
std::to_string(0);
^
I'm running g++ version 4.8.1. Unlike all the other references to this error that I found out there, I am not using MinGW, I'm on Linux (3.11.2).
Any ideas why this is happening? Is this standard behaviour and I did something wrong or is there a bug somewhere?
you may want to specify the C++ version with
g++ -std=c++11 tmp.cpp -o tmp
I don't have gcc 4.8.1 at hand , but in older versions of GCC,
you can use
g++ -std=c++0x tmp.cpp -o tmp
At least gcc 4.9.2 I believe also support part of C++14 by specifying
g++ -std=c++1y tmp.cpp -o tmp
Update:
gcc 5.3.0 (I am using the cygwin version) supports both -std=c++14 and -std=c++17 now.
to_string works with the latest C++ versions like version 11. For older versions you can try using this function
#include <string>
#include <sstream>
template <typename T>
std::string ToString(T val)
{
std::stringstream stream;
stream << val;
return stream.str();
}
By adding a template you can use any data type too.
You have to include #include<sstream> here.
Suppose we have the following code:
#if !defined(__cplusplus)
# error This file should be compiled as C++
#endif
#include <stdio.h>
#include <string>
//#define USE_CXX_CLASS
#ifdef USE_CXX_CLASS
class SomeClass
{
public:
SomeClass() {}
~SomeClass() {}
std::string GetSomeString()
{
// case #1
}
};
#endif // USE_CXX_CLASS
int foo()
{
// case #2
}
int
main (int argc, char *argv[])
{
(void)argc;
(void)argv;
#ifdef USE_CXX_CLASS
SomeClass someInstance;
someInstance.GetSomeString();
#endif // USE_CXX_CLASS
foo();
return 0;
}
And suppose that it were to be compiled the C++ compiler (and not the C compiler) from GCC version 4.2.1 with the options -Wreturn-type -Werror=return-type. If the above code is compiled as is without first uncommenting the //#define USE_CXX_CLASS line above, then you will see a warning but no error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In function 'int foo()':
test.cpp:26: warning: control reaches end of non-void function
But if the //#define USE_CXX_CLASS line is uncommented, then the warning is treated as an error:
.../gcc-4.2.1/bin/g++ -g -fPIC -Wreturn-type -Werror=return-type test.cpp -c -o test.o
test.cpp: In member function 'std::string SomeClass::GetSomeString()':
test.cpp:18: error: no return statement in function returning non-void [-Wreturn-type]
gmake: *** [test.o] Error 1
Yes, one is a non-member function (case #2), and the other is a C++ function (case #1). IMO, that should not matter. I want both conditions treated as an error, and I don't want to add -Werror or -Wall at this point in time (probably will do so later, but that is out of scope of this question).
My sub-questions are:
Is there some GCC switch that I am missing that should work? (No I do not want to use #pragma's.)
Is this a bug that has been addressed in a more recent version of GCC?
For reference, I have already poured through other similar questions already, including the following:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
C question: no warning?
Is a return statement mandatory for C++ functions that do not return void?
It has been fixed, it works well with g++ 9.3: both member functions and free functions are treated as error with -Wall -Werror=return-type
I do see an error even w/o the USE_CXX_CLASS flag. i.e. g++ is consistent with the error for both class member functions and non member functions.
g++ (GCC) 4.4.3 20100127 (Red Hat 4.4.3-4)
It seems to me that what you need is a shell script wrapper around gcc.
Name it something like gcc-wrapper and g++-wrapper.
In your Makefile set CC and CXX to the wrappers.
Have the wrapper invoke GCC and pipe its output to another program which will search for your desired warning strings.
Have the search program exit with an error when it finds the warning.