LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-27-2005, 11:01 AM   #1
wallwaters
LQ Newbie
 
Registered: May 2005
Posts: 9

Rep: Reputation: 0
passing arg 3 of `pthread_create' from incompatible pointer type


passing arg 3 of `pthread_create' from incompatible pointer type

I do not understand this warning.


Following is my code . what is that warning. I dont see any thing wrong in code. Please help me.


Result vSend(int rxLength);
void *sendThread(void *rxLength);

void main()
{
vSend(100);
}
Result vSend(int rxLength)
{
Result status = PENDING;
pthread_t sendThread;
int tRetVal;
App_Report("vSend %d",rxLength);
tRetVal = pthread_create( &sendThread, NULL, &sendThread, &rxLength);
return status;
}


void *sendThread(void *rxLength)
{
int *length = (int *) rxLength;
print("Length %d"length );
return NULL;
}


passing arg 3 of `pthread_create' from incompatible pointer type

Thanks
Suneel
 
Old 05-27-2005, 11:47 AM   #2
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Re: passing arg 3 of `pthread_create' from incompatible pointer type

Quote:
Originally posted by wallwaters
passing arg 3 of `pthread_create' from incompatible pointer type

I do not understand this warning.


Following is my code . what is that warning. I dont see any thing wrong in code. Please help me.


Result vSend(int rxLength);
void *sendThread(void *rxLength); // change to void sendThread(void *rxLength);

void main()
{
vSend(100);
}
Result vSend(int rxLength)
{
Result status = PENDING;
pthread_t sendThread;
int tRetVal;
App_Report("vSend %d",rxLength);
tRetVal = pthread_create( &sendThread, NULL, &sendThread, &rxLength);
return status;
}


void *sendThread(void *rxLength) // change to void sendThread(void *rxLength);
{
int *length = (int *) rxLength;
print("Length %d"length );
return NULL;
}


passing arg 3 of `pthread_create' from incompatible pointer type

Thanks
Suneel
 
Old 06-02-2005, 03:23 PM   #3
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 66
Re: Re: passing arg 3 of `pthread_create' from incompatible pointer type

freegianghu, you can't return NULL; from a function with a void return-type.

The problem is that sendThread is defined both as a function (by a forward declaration) and as a variable. The variable masks the definition of the function, and so both sendThread arguments passed to pthread_create are of type pthread_t.

Since the third argument to pthread_create is supposed to be a callback function, not a pthread_t, this won't compile.
 
Old 06-02-2005, 08:58 PM   #4
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
Re: Re: Re: passing arg 3 of `pthread_create' from incompatible pointer type

Quote:
Originally posted by rjlee
freegianghu, you can't return NULL; from a function with a void return-type.

The problem is that sendThread is defined both as a function (by a forward declaration) and as a variable. The variable masks the definition of the function, and so both sendThread arguments passed to pthread_create are of type pthread_t.

Since the third argument to pthread_create is supposed to be a callback function, not a pthread_t, this won't compile.
Sorry, I made a mistake . Its should be void *sendThread(void *rxLength)

Have a nice day,
Giang Hu
 
Old 03-06-2010, 06:24 PM   #5
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: 0
title + undefined reference to `pthread_create'

Hi there;
I've encountered with the same problem in my code with some extra problems.Here are the code :
Code:
/*
 * pmutex.c
 *
 *  Created on: Mar 7, 2010
 */

#include <pthread.h>
#include <stdio.h>

pthread_mutex_t cntr_mutex = PTHREAD_MUTEX_INITIALIZER;

long protVariable = 0L;

void myThread(void *arg)
{

	int i,ret;

	for(i=0 ; i<10000 ; i++)
	{

		ret = pthread_mutex_lock(&cntr_mutex);

		if(ret == 0)
		{
			protVariable++;
		}


		ret = pthread_mutex_unlock(&cntr_mutex);

		if(ret == 0)
		{
			printf("ret is zero.\n");
		}

	}

	pthread_exit(NULL);
}

#define MAX_THREADS 10

int main()
{

	int ret,i;
	pthread_t threadIds[MAX_THREADS];

	for( i=0 ; i<MAX_THREADS ; i++)
	{

		//below code gives error: passing argument 3 of ‘pthread_create’ from incompatible pointer type
		//and undefined reference to `pthread_create'
		ret = pthread_create(&threadIds[i] , NULL , myThread , NULL);
		if(ret != 0)
		{

			printf("error creating thread %d\n",(int)threadIds[i]);
		}

		//below code gives error: undefined reference to `pthread_join'
		ret = pthread_join(threadIds[i] , NULL );
		if(ret != 0)
		{

			printf("error joining thread %d\n",(int)threadIds[i]);
		}

		printf("The protected variable value is %ld\n", protVariable);

		ret = pthread_mutex_destroy(&cntr_mutex);
		if(ret != 0)
		{

			printf("couldn't destroy the mutex.\n");
		}

	}

	return 5;
}
I looked at my code properly but I could not find error.
Thanks in advance!
 
Old 03-06-2010, 10:14 PM   #6
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
Quote:
Originally Posted by http://opengroup.org/onlinepubs/007908799/xsh/pthread_create.html
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
void *(*start_routine)(void*), void *arg);
From this, argument 3 should be a pointer to a function of the form
Code:
void * foo(void* bar)
but your passing it "myThread", which has signature
Code:
void myThread(void *arg)
So change return type from "void" to "void*", and of course add the "return" logic in the function.

For the "undefined reference" errors this usually means you're not linking to the library that contains the actual object code. It depends on your compiler, which you haven't told us, but for "gcc" I think you can pass "-pthread" as an argument when compiling. This should get rid of the second "undefined reference" error also.
 
1 members found this post helpful.
Old 03-07-2010, 05:19 AM   #7
gozlemci
Member
 
Registered: Mar 2010
Location: Turkey
Distribution: Ubuntu 12.04
Posts: 34

Rep: Reputation: 0
Quote:
Originally Posted by nadroj View Post
From this, argument 3 should be a pointer to a function of the form
Code:
void * foo(void* bar)
but your passing it "myThread", which has signature
Code:
void myThread(void *arg)
So change return type from "void" to "void*", and of course add the "return" logic in the function.

For the "undefined reference" errors this usually means you're not linking to the library that contains the actual object code. It depends on your compiler, which you haven't told us, but for "gcc" I think you can pass "-pthread" as an argument when compiling. This should get rid of the second "undefined reference" error also.
Thank you for reply.
I am using eclipse cdt as a compiler and ubuntu 9.04 as a operating system. When I compile code with terminal , it worked fine.
 
Old 03-07-2010, 10:50 AM   #8
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 58
It wasn't clear to me. So it's all working for you now or still not?
 
Old 03-08-2010, 01:46 AM   #9
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,990

Rep: Reputation: 66
Quote:
Originally Posted by gozlemci View Post
Hi there;
I've encountered with the same problem in my code with some extra problems.Here are the code :
Code:
...
[SIZE="6"]		//below code gives error: passing argument 3 of ‘pthread_create’ from incompatible pointer type
...
		//below code gives error: undefined reference to `pthread_join'
		ret = pthread_join(threadIds[i] , NULL );
...
I looked at my code properly but I could not find error.
Thanks in advance!
"Undefined reference" is a linker error, not a compiler error. It means that your code is correct (at least syntactically) but GCC can't find the definition of the pthread_create and pthread_join functions.

You probably need to tell gcc to link against the libpthread.so (or libpthread.a) library. The best way to do this is by adding a command-line argument like -lpthread.

Hope that helps,

—Robert J Lee
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the warning: passing arg 3 of `pthread_create' from incompatible pointer type wallwaters Linux - Software 3 06-01-2005 08:30 AM
Passing arg 3 of blah makes pointer from integer without a cast xconspirisist Programming 6 08-22-2004 08:04 AM
How does one surpress incompatible pointer type warnings? tonyfreeman Programming 1 08-18-2004 06:06 PM
Getting an incompatible pointer type error... JStew Programming 4 03-06-2003 05:08 PM
Installing Wine - incompatible pointer type orange400 Linux - Software 1 03-03-2003 12:57 AM


All times are GMT -5. The time now is 06:23 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration