Search code examples
csolariscc

How to compile c using cc in Solaris?


I want to test my program in different OS but gcc doesn't really work in Solaris but there's cc.

Here's how I compile with gcc :

gcc -c quicksort.c sched.c -g -pthread -O3
gcc -o quicksort quicksort.o sched.o -Wall -g -pthread -O3

I tried to compile with cc using the same arguments but here's what I get :

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

Here's the line that make the first error :

pthread_create(&s->tab_thread[i], NULL, (void *) main_thread, new_args_sched(i, s));

new_args_sched is just a struct for passing args to the function main_thread

I don't know what option should I use, I tried with -mt and -lpthread but it didn't work. I have 3 file quicksort.c with the main, sched.h and sched.c

Edit

The Solaris computer is in ssh it's not mine and I can't configure it. The version of gcc is 3.4.3 using only C90 my code work with C11. Only cc should work but I don't know how to compile with it correctly...

I'm using a struct to pass in main_thread like this :

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;
}

and so here's how I get it in my function when I use 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 */
}

Solution

  • This code

    void main_thread(void *closure) {
        struct sched_args *args = (struct sched_args *)closure;
        int i = args->i;
        struct scheduler *s = args->s
        /* doing something */
    }
    

    needs to be

    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 );
    }
    

    The pthread_create() POSIX-standard function has as a prototype

    int pthread_create(pthread_t *restrict thread,
       const pthread_attr_t *restrict attr,
       void *(*start_routine)(void*), void *restrict arg);
    

    Note that the third argument is of type void *(*start_routine)(void*) - the address of a function taking a void * parameter and returning a void *.