我想在不同的操作系统中测试我的程序,但是 gcc
在 Solaris 中并不能正常工作,但是有 cc
。
下面是我用 gcc 编译的方式:
gcc -c quicksort.c sched.c -g -pthread -O3
gcc -o quicksort quicksort.o sched.o -Wall -g -pthread -O3
我尝试使用相同的参数使用 cc 进行编译,但这是我得到的结果:
quicksort.c:
sched.c:
"sched.c", line 233: warning: argument #3 is incompatible with prototype:
prototype: pointer to function(pointer to void) returning pointer to void : "/usr/include/pthread.h", line 197
argument : pointer to void
ld: fatal : soname option (-h, --soname) is incompatible with building a dynamic executable
ld: fatal : flags processing errors
这是第一个错误的行:
pthread_create(&s->tab_thread[i], NULL, (void *) main_thread, new_args_sched(i, s));
new_args_sched
只是一个用于将 args 传递给函数 main_thread
的结构
我不知道应该使用什么选项,我尝试使用 -mt
和 -lpthread
但它没有用。我有 3 个文件 quicksort.c
,主要是 sched.h
和 sched.c
编辑
Solaris 计算机在 ssh
中,它不是我的,我无法配置它。 gcc
的版本是 3.4.3
,仅使用 C90
我的代码使用 C11
。只有 cc
应该可以工作,但我不知道如何正确编译...
我正在使用一个结构来传递 main_thread
,如下所示:
struct sched_args {
int i;
struct scheduler *s;
};
struct sched_args *
new_args_sched(int i, struct scheduler *s) {
struct sched_args *args = malloc(sizeof(struct sched_args));
if(args == NULL)
return NULL;
args->i = i;
args->s = s;
return args;
}
下面是我在使用 pthread_create 时如何在我的函数中获取它:
void main_thread(void *closure) {
struct sched_args *args = (struct sched_args *)closure;
int i = args->i;
struct scheduler *s = args->s
/* doing something */
}
这段代码
void main_thread(void *closure) {
struct sched_args *args = (struct sched_args *)closure;
int i = args->i;
struct scheduler *s = args->s
/* doing something */
}
需要
void *main_thread(void *closure) {
struct sched_args *args = (struct sched_args *)closure;
int i = args->i;
struct scheduler *s = args->s
/* doing something */
return( NULL );
}
pthread_create()
POSIX-standard function有原型(prototype)
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
请注意,第三个参数的类型为 void *(*start_routine)(void*)
- 采用 void *
参数的函数的地址 和返回一个 void *
。
我是一名优秀的程序员,十分优秀!