Can't use threads

Pages: 1234
I also downloaded boost, and I get a LOT of linker erros becouse of undefined references! Help!
It's hard to work blind.
-lboost_thread
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <boost\thread.hpp>

using namespace std;

int main ()
{
  	boost::thread mythread([](){cout<<"Hello World!";});
}

The compiler wrote:
In function `thread<main()::<lambda()> >':
[Linker error] c:/program files/mingw/bin/../lib/gcc/mingw32/4.7.0/include/c++/boost/thread/detail/thread.hpp:186: undefined reference to `__imp___ZN5boost6thread12start_threadEv'
In function `__static_initialization_and_destruction_0':
[Linker error] c:/program files/mingw/bin/../lib/gcc/mingw32/4.7.0/include/c++/boost/system/error_code.hpp:214: undefined reference to `boost::system::generic_category()'
[Linker error] c:/program files/mingw/bin/../lib/gcc/mingw32/4.7.0/include/c++/boost/system/error_code.hpp:215: undefined reference to `boost::system::generic_category()'
[Linker error] c:/program files/mingw/bin/../lib/gcc/mingw32/4.7.0/include/c++/boost/system/error_code.hpp:216: undefined reference to `boost::system::system_category()'
In function `_ZN5boost16thread_exceptionC2EiPKc':
[Linker error] c:/program files/mingw/bin/../lib/gcc/mingw32/4.7.0/include/c++/boost/thread/exceptions.hpp:49: undefined reference to `boost::system::system_category()'
In function `_ZN5boost6threadD1Ev':
[Linker error] c:/program files/mingw/bin/../lib/gcc/mingw32/4.7.0/include/c++/boost/thread/detail/thread.hpp:174: undefined reference to `__imp___ZN5boost6thread6detachEv'
C:\Users\Vilim\Desktop\New folder\collect2.exe [Error] ld returned 1 exit status
And I don't know what -lboost_thread is supposed to be, but by the leading '-' I suppose it's a compiler option, and it's not working.
It's a linker option. It says to link against that library.
I need to see your build commands.
viliml, The latest error you posted is because you didn't link the boost library (.lib/.a) correctly or at all.
Also, for the threading problem, is there a namespace in thread.h that you aren't using? I've never used thread.h, but I would take a look at the header itself to see whats going on, because if you included the file, and are using its declarations, then they should work; unless they're in a namespace or something like that.
Last edited on
I use MinGW 4.7.1 and I use the c++0x flag instead of c++11. I haven't compiled any thread examples using the new standard yet, but it's supposed to have support for the new standard. Once I get home, I'll try compiling a simple thread example using both boost and standard.
Please use forward slash.
$ g++ -std=c++0x -c thread.cpp
$ g++ thread.o -lboost_thread -lboost_system -o thread.bin
OOh a linker option, tnx!
But it still doesn't work! I compiled with theese compile options:
-std=c++11 -fpermissive -Wall -Wextra -c
and theese linker options:
-static-libstdc++ -static-libgcc -lboost_thread -lboost_system
and it gives me the following message in the console:
The console wrote:
Cannot execute C:\USERS\VILIM\DESKTOP\NEW FOLDER\NEIMENOVANO2.EXE

And closes in less than a millisecond. I needed to run it from cmd to see what it wrote!
Major Tom wrote:
Also, for the threading problem, is there a namespace in thread.h that you aren't using?

Here's the begginning of the thead header:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef _GLIBCXX_THREAD
#define _GLIBCXX_THREAD 1

#pragma GCC system_header

#ifndef __GXX_EXPERIMENTAL_CXX0X__
# include <bits/c++0x_warning.h>
#else

#include <chrono>
#include <functional>
#include <memory>
#include <bits/functexcept.h>
#include <bits/functional_hash.h>
#include <bits/gthr.h>

#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)

namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION

  /**
   * @defgroup threads Threads
   * @ingroup concurrency
   *
   * Classes for thread support.
   * @{
   */

  /// thread
  class thread
  {

And so on...
So, all I need for threads is for 2 macros to be defined:_ GLIBCXX_HAS_GTHREADS and _GLIBCXX_USE_C99_STDINT_TR1. The second one is dfefined, the first one isn't.
But it still doesn't work! (...)
I needed to run it from cmd to see what it wrote!
See the sticky in the beginner section.
Do you think I'm that noobish! I have an IDE that stops the execution after it's done (doesn't close the window), AND even as an addition to THAT, I tried using system("pause")!
You act like one...
If it works in the terminal, ¿what's the problem?

Edit:
"Cannot execute C:\USERS\VILIM\DESKTOP\NEW FOLDER\NEIMENOVANO2.EXE"
¿who did say that?
I supposed that neimenovano2.exe is the program, that it builds correctly and it works in the terminal.
If your IDE fails to execute it, it's your IDE problem. Check out permissions and settings, but modifying the source code shouldn't affect it.
Last edited on
No, it build's correctly, and links correctly, but wehn I run the exe, that message posps out and terminates the program immeaditly!
Help please, neither C++11 nor boost threads work!!
boost doesn't work (there should be link errors) because this '-lboost_system' doesn't exist.

Instead it looks like this: libboost_system-mgw46-mt-1_50.a (made up in the boost headers)

you need to provide the the path to the boost libs like ...\boost\stage\lib
boost::any works! Actiually, I bet EVERYTHING except for threads that usually works with the librarys I have doesn't work!
Things like boost::any probably works because all the code for it is contained in the header so it doesn't need linking.
Hmm yes. Everything I tried that works doen't need linking to a library... I'll keep trying, searching for anything else that doesn't work!
Pages: 1234