在gcc编译环境下应该注意源代码的后缀是.C还是.CPP,因为gcc是根据后缀决定编译器属性的。而C++在类型转换等多个方面比C语言有更严格的检查机制,如下面的范例代码,在使用.C为后缀的时候可以正常的编译通过,但是在.CPP的时候就不能正常的编译通过。
#include <stdio.h>
#include <pthread.h>
void thread(void)
{
int i;
for(i=0;i< 3;i++)
printf("This is a pthread.\n");
}
int main(void)
{
pthread_t id;
int i,ret;
ret=pthread_create(&id,NULL,(void *) thread,NULL);
if(ret!=0){
printf ("Create pthread error!\n");
exit (1);
}
for(i=0;i< 3;i++)
printf("This is the main process.\n");
pthread_join(id,NULL);
return (0);
}
使用gcc编译这段代码,会有如下的错误
gcc test.cpp -o test.exe -lpthread
test.cpp: In function `int main ()':
test.cpp:14: cannot convert `void *' to `void *(*) (void *)' for
argument `3' to `pthread_create (pthread_t *, const pthread_attr_t *,
void *(*) (void *), void *)'
test.cpp:17: `exit' undeclared (first use this function)
test.cpp:17: (Each undeclared identifier is reported only once for each
function it appears in.)
代码中显示第14行
ret=pthread_create(&id,NULL,(void *) thread,NULL);
pthread_create的函数原型是
int pthread_create(pthread_t * thread,
pthread_attr_t * attr,
void * (*start_routine)(void *),
void * arg);
浙公网安备 33010602011771号