Oct 15, 2011 at 5:57pm UTC
Hello all,
I am trying to compile a thread program using g++ running on windows 7. I have the latest pthread lib and the compiler installed.
Here's my simple program
#include <iostream.h>
#include <pthread.h>
#include <stdlib.h>
void* thread_func(void* data)
{
cout <<"thread_func()"<<endl;
}
int main()
{
pthread_t p_threads;
pthread_create(&p_threads, NULL, thread_func, NULL);
return 0;
}
When I compiled it, I got an error on <stdlib.h>
In file included from mythread.cpp:3:
C:\\cygnus\\.........stdlib.h:103: parse error before 'unsigned'
Now if I flipped line 1 with line 3, ie., switched the include orders between <stdlib.h> and <iostream.h> to the following,
#include <stdlib.h>
#include <iostream.h>
#include <pthread.h>
The file compiled and ran fine.
That got me very curious, so I wrote another simple cpp file
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
cout << "test"<<endl;
return 0;
}
Now it doesn't matter the order of the #includes, the file compiled and ran fine.
I have no idea what's going on. Any help will be appreciated!
Oct 15, 2011 at 6:10pm UTC
Since Windows doesn't support pthread in native, I think the cause is pthread.h, this header defines something conflicting with others. I don't think there is any problem to include pthread.h after all other headers.
Oct 15, 2011 at 6:15pm UTC
I think that's it. Thanks!
Oct 15, 2011 at 6:27pm UTC
pthread( ) , I believe, is an abbreviated name for POSIX Thread. Windows doesn't support POSIX threads. For Windows, <process.h> should be used instead.
Wazzak
Oct 15, 2011 at 7:01pm UTC
What happens if you use the modern C++ headers <iostream> and <cstdlib> instead of the old <iostream.h> and <stdlib.h> ?