I try to use std::this_thread::sleep_for() function but got the error
error: 'std::this_thread' has not been declared.
The flag _GLIBCXX_USE_NANOSLEEP included.
What else is needed to force it to work?
MinGW ==> gcc version 4.7.2 (GCC)
SSCCE:
#include<thread>
int main() {
std::this_thread::sleep_for(std::chrono::seconds(3));
}
command line:
g++ -D_GLIBCXX_USE_NANOSLEEP -std=gnu++0x ssce.cpp -o ssce.exe
result of compilation:
ssce.cpp: In function 'int main()':
ssce.cpp:4:8: error: 'std::this_thread' has not been declared
Use MinGW with POSIX threads, Luke.
http://sourceforge.net/projects/mingwbuilds/
Related
I updated my g++ because my older version didn't fully support std::filesystem, but now I cannot use mutexes at all. Example code:
#include <mutex>
int main(const int argc, const char** argv)
{
std::mutex test;
return 0;
}
Compiled with:
g++ -Wall -pedantic main.cpp -std=c++17 -o main.exe
The error, note how it suggests adding include for <mutex> right under the include that is already there:
main.cpp: In function 'int main(int, const char**)':
main.cpp:5:8: error: 'mutex' is not a member of 'std'
5 | std::mutex test;
| ^~~~~
main.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <mutex>
+++ |+#include <mutex>
2 |
The full version of g++ is: g++ (MinGW.org GCC Build-2) 9.2.0. I installed MinGW using the MinGW installation manager. Is it possible that I am missing the correct version of stdlib?
I opened the mutex file in the MinGW directory and I see nothing that would be obviously wrong.
g++ -std=c++11 -Wall -g threads.cpp -o threads.out
In file included from threads.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:37:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:215:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:401:32: error: use of undeclared identifier '_ISspace'
static const mask space = _ISspace;
^
Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:402:32: error: use of undeclared identifier '_ISprint'
static const mask print = _ISprint;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:403:32: error: use of undeclared identifier '_IScntrl'
static const mask cntrl = _IScntrl;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:404:32: error: use of undeclared identifier '_ISupper'
static const mask upper = _ISupper;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:405:32: error: use of undeclared identifier '_ISlower'
static const mask lower = _ISlower;
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:406:32: error: use of undeclared identifier '_ISalpha'
static const mask alpha = _ISalpha;
can you please help me to resolve this __locale issue while compiling the c++ code.
#include <iostream>
#include <thread>
using namespace std;
void fun(void)
{
cout << "Vaule " << 10 << endl;
}
int main()
{
thread t1(fun);
thread t2(fun);
return 0;
}
compiling command:
g++ -std=c++11 -Wall -g thread.cpp -o thread.out
Two things fix the problem you're having.
Thing the first, add the compiler option -pthread. My compile command: clang++ -Wall -Wextra -std=c++11 -pthread main.cpp
Thing the second, join your threads before ending the program.
t1.join();
t2.join();
Something it does not fix, is that your std::cout statement will likely be jumbled because the threads simply dump their data into the single stream as they please. For an example, my output was the following:
Vaule Vaule 10
10
In order to fix that, you'll likely need to place a lock(mutex) around the std::cout statement.
As I said in my comment, I do not recommend using g++ unless you installed it yourself. The command you're using is an alias that doesn't behave, because of some text you left out.
❯ g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Contrast with clang++
❯ clang++ --version
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin20.3.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
Of note in the g++ section is the 'Configured with' line. It uses a Standard Library from gcc 4.2.1, which is pre-C++11. You should not have left that information out.
I have the following code:
#include <type_traits>
int main()
{
}
g++ file.cc -std=c++0x
works just fine.
However, I need to use clang++ for some reason.
When I try
clang++ file.cc -std=c++0x
I get a bunch of errors:
In file included from file.cc:1:
In file included from /usr/include/c++/4.4.4/type_traits:50:
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:41: error: expected ')'
struct is_function<_Res(_ArgTypes......)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:28: note: to match this '('
struct is_function<_Res(_ArgTypes......)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:230:12: error: redefinition of 'is_function<type-parameter-0-0 (type-parameter-0-1, ...)>'
struct is_function<_Res(_ArgTypes......)>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/4.4.4/tr1_impl/type_traits:227:12: note: previous definition is here
struct is_function<_Res(_ArgTypes...)>
^
/usr/include/c++/4.4.4/tr1_impl/type_traits:233:29: error: type qualifier is not allowed on this function
struct is_function<_Res(_ArgTypes...) const>
....
clang++ --version gives:
clang version 2.8 (branches/release_28)
Target: x86_64-redhat-linux-gnu
Thread model: posix
Any ideas how to fix that? (-std=c++11 doesn't work, not recognized.)
Clang 2.8 does not support C++11 features, you need to upgrade your compiler to use them.
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.
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.