Attempting to use threads in C++ for the first time. Here is my code:
#include <iostream>
#include <thread>
int main(){
std::thread th;
}
To compile, I am using:
g++ -g -Wall -std=c++11 -pthread threadtest.cpp
This gives the following error:
$ g++ -g -Wall -std=c++11 -pthread threadtest.cpp
threadtest.cpp: In function 'int main()':
threadtest.cpp:5:8: error: 'thread' is not a member of 'std'
5 | std::thread th;
| ^~~~~~
threadtest.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to
'#include <thread>'?
2 | #include <thread>
+++ |+#include <thread>
3 |
If I comment out the "std::thread th" line, the error message changes to:
$ g++ -g -Wall -std=c++11 -pthread threadtest.cpp
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lpthread
collect2.exe: error: ld returned 1 exit status
Operating system: Windows 10. Seems to get the same result whether I'm using the plane-old windows command line or if I do it in Cygwin. Anyone know what's wrong? Thanks in advance.
Related
I try to compile this very simplified program:
#include <pthread.h>
int main(){
pthread_yield();
return 0;
}
using -pthread like the IBM side says:
$ g++ -pthread test.cpp -o test
and get this error:
test.cpp: In function 'int main()':
test.cpp:4:15: error: 'pthread_yield' was not declared in this scope
pthread_yield();
I tried lots of other falgs too, but nothing worked so far. The pthread.h is in /usr/includes but pthread_yield() needs _AIX_PTHREADS_D7 defined.
Do I have to define this myselfe or is this done by adding some flag?
THX!
Other than defining symbol _AIX_PTHREADS_D7 you have to use library libpthreads_compat as well.
g++ -o marscode marscode.cc -D_AIX_PTHREADS_D7 -lpthreads_compat -lpthreads
I am running Windows 10 64bit. Cygwin is 64 bit.
I installed boost from cygwin package manager.
I tried to compile test.cpp:
#include <boost/asio.hpp>
int
main(int argc, char**argv)
{
return 0;
}
using command
g++ -std=c++11 -Wall -g -D__USE_W32_SOCKETS D_WIN32_WINNT=_WIN32_WINNT_WIN7 test.cpp -o test.exe
but compile fails. It looks like posix is being used.
Any ideas why this fails?
In file included from /usr/include/boost/asio/detail/fd_set_adapter.hpp:22:0,
from /usr/include/boost/asio/detail/select_reactor.hpp:27,
from /usr/include/boost/asio/detail/reactor.hpp:29,
from /usr/include/boost/asio/detail/impl/task_io_service.ipp:24,
from /usr/include/boost/asio/detail/task_io_service.hpp:198,
from /usr/include/boost/asio/impl/io_service.hpp:71,
from /usr/include/boost/asio/io_service.hpp:767,
from /usr/include/boost/asio/basic_io_object.hpp:19,
from /usr/include/boost/asio/basic_socket.hpp:20,
from /usr/include/boost/asio/basic_datagram_socket.hpp:20,
from /usr/include/boost/asio.hpp:21,
from appcontrol.cpp:16:
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp:82:12: error: 'fd_set' does not name a type
operator fd_set*()
^
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp:105:11: error: 'fd_set' does not name a type
mutable fd_set fd_set_;
^
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp: In constructor 'boost::asio::detail::posix_fd_set_adapter::posix_fd_set_adapter()':
/usr/include/boost/asio/detail/posix_fd_set_adapter.hpp:42:14: error: 'fd_set_' was not declared in this scope
FD_ZERO(&fd_set_);
^
Thanks to help from #cygwin IRC:
g++ -std=c++11 -Wall -g -D_XOPEN_SOURCE=500 test.cpp -o test.exe -lboost_system
I've written a simple code which create folder. The problem is that I can't compile it. The code is below:
#include <iostream>
#include <boost/filesystem.hpp>
int main()
{
boost::filesystem::create_directories("/tmp");
return 0;
}
Compilation:
g++ createFolder.cpp -std=c++0x -lboost_system -o createFolder
I have got errors:
collect2: ld returned 1 exit status
How to correct the compilation process to run this program.
Try adding boost-filesystem to you linker:
g++ createFolder.cpp -std=c++0x -lboost_system -lboost_filesystem -o createFolder
when compile with g++ -std=c++0x -Wall test.cc -o hello,
output fatal error: cstdatomic No such file or directly
Where is missing?
The include should be #include <atomic>
Compiling with:
g++ -std=c++0x -Wall -pthread test2.cc -o hello
I'm getting this error:
test2.cc: atomic_thread_fence is not a member of std
I am using g++ 4.5.2 in Ubuntu. What am I missing?
Do you have the following in test2.cc?
#include <atomic>