Click to See Complete Forum and Search --> : pthread usage
ruchimca
February 9th, 2006, 03:43 AM
I am trying to run simple thread program in vc++ 6.0. I get error "cannot incude <pthread.h>.
This is quite obvious error, but from where do i get the dlls to have thread programs working on vc++ 6.0 or do i need to use another software?
I also have MKS Toolkit installed on my machine. However, there also its not working out.
Butterfly
February 9th, 2006, 04:03 AM
As I remeber, pthread is used under UNIX but for M$ I could not rememer.
humble_learner
February 9th, 2006, 04:23 AM
pthread is used for posix threads used on unix.
On Windows, please use CreateThread(), _beginthread etc.
ruchimca
February 9th, 2006, 06:44 AM
I have downloaded pthreads-win32 version from gnu site. Now I dont get "cannot include" error but while linking I get
unresolved external symbol _pthread_create
i have mentioned appropriate lib path and include path.
Marc G
February 9th, 2006, 07:42 AM
[ moved thread ]
Marc G
February 9th, 2006, 07:43 AM
I have downloaded pthreads-win32 version from gnu site. Now I dont get "cannot include" error but while linking I get
unresolved external symbol _pthread_create
i have mentioned appropriate lib path and include path.
What do you mean with "lib path"? Are you specifying the full path to the pthread lib including filename? You need to link with the pthread lib file, so add it to your linker settings.
/******** this is the main thread's code */
int main(int argc,char *argv[])
{
int worker;
pthread_t threads[NTHREADS]; /* holds thread info */
int ids[NTHREADS]; /* holds thread args */
int errcode; /* holds pthread error code */
int *status; /* holds return code */
/* create the threads */
for (worker=0; worker<NTHREADS; worker++) {
ids[worker]=worker;
if (errcode=pthread_create(&threads[worker],/* thread struct */
NULL, /* default thread attributes */
hola, /* start routine */
&ids[worker])) { /* arg to routine */
errexit(errcode,"pthread_create");
}
}
/* reap the threads as they exit */
for (worker=0; worker<NTHREADS; worker++) {
/* wait for thread to terminate */
if (errcode=pthread_join(threads[worker],NULL)) {
errexit(errcode,"pthread_join");
}
/* check thread's exit status and release its resources */
if (*status != worker) {
fprintf(stderr,"thread %d terminated abnormally\n",worker);
exit(1);
}
}
return(0);
}
Now, the I have downloaded latest pthread-win32 files from gnu site in C:\PTHREADS\Pre-built\
C:\PTHREADS\Pre-built\Include -> has files pthread.h, semaphore.h, sched.h
C:\PTHREADS\Pre-built\lib -> has dlls and libs pthreadVSE2.lib, pthreadVSE2.dll, pthreadVSE.lib, pthreadVSE.dll, PTHREADVCE.LIB,PTHREADVCE.dll, pthreadVC2.lib, pthreadVC2.dll, pthreadVC.dll, pthreadVC.lib, pthreadGCE2.dll,pthreadGC.dll,pthreadGCE.dll,
pthreadGC2.dll and some .a's
Now, I have included C:\PTHREADS\Pre-built\Include in vc++ include directory path and C:\PTHREADS\Pre-built\lib in lib directory path [ Tools->
options-> Directories]
After building the code, I get error "LNK2001 unresolved external symbol __imp_pthread_create.... and __imp_pthread_join....
How can I resolve this?
Regards.
ruchimca
February 10th, 2006, 02:02 AM
I also tried running cmd,
nm *.*|grep -i pthread_create in c:\PTHREADS\Pre-built\lib
and I can see some lines generating the symbol
0:0000000 B __imp__pthread_create
0:0000000 T _pthread_create
0:0000000 B __imp__pthread_create
0:0000000 T _pthread_create
0:0000000 B __imp__pthread_create
0:0000000 T _pthread_create
0:0000000 B __imp__pthread_create
0:0000000 T _pthread_create
nm: md5.sum: not executable, object, or library format
nm: pthread.def: not executable, object, or library format
0:0004532 T _pthread_create
0:0005216 T _pthread_create
0:0004676 T _pthread_create
0:0006640 T _pthread_create
Why is the linking not happening then?
Marc G
February 10th, 2006, 07:25 AM
You are only specifying the libraries path. You need to explicitly tell it to link with one of those lib files by adding one of the pthread lib files to the linker settings.
ruchimca
February 13th, 2006, 11:22 PM
How do i specify that (linker settings to take pthread lib) in vc++ 6.0?
Marc G
February 14th, 2006, 05:22 AM
You can either do from the project settings or use something like the following pragma in your code:
#pragma comment(lib,"pthread.lib")
of course, replace pthread.lib with the actual name of the lib.
anantwakode
February 14th, 2006, 07:26 AM
Hi ruchimca,
Which platform actually do you want ?
Unix/linux or Windows ...
-Anant
Marc G
February 14th, 2006, 03:17 PM
His original post says he is using vc++ 6.0, so i'm quite sure it's Windows.
ruchimca
February 14th, 2006, 11:45 PM
okay, Thanks! it works now by using project settings Link.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.