0

I have wrote below code:

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

int pthread_create(
        pthread_t*,
        const pthread_attr_t *,
        void* (*)(void *),
        void*)  __attribute__ ((weak))  ;


int main(){
    if (pthread_create) {
        printf("multi-thread");
    } else {
        printf("single-thread");
    }
}

When compiling the code with different ways and running it, it shows the same consequence . The codes are undermentioned:

zoushengfu@zoushengfudeMacBook-Pro:~/Documents/code/c++$ gcc -o pt
pthread.cpp  
zoushengfu@zoushengfudeMacBook-Pro:~/Documents/code/c++$ ./pt 
multi-thread
zoushengfu@zoushengfudeMacBook-Pro:~/Documents/code/c++$ gcc -o pt pthread.cpp -lpthread
zoushengfu@zoushengfudeMacBook-Pro:~/Documents/code/c++$ ./pt
multi-thread

I want to know why the result is same wheather I use library pthread when compile it. And do I some error when using __attribute__((weak))

shengfu zou
  • 560
  • 1
  • 7
  • 16
  • Maybe `gcc` is configured in a way that the `-lpthread` switch is used by default. Do you get a linker error when you remove the "weak" declaration? – Martin Rosenau Nov 06 '17 at 06:20
  • How to know weather -lpthread is used by default or not? When I emove the "weak" declaration, it show below :zoushengfu@zoushengfudeMacBook-Pro:~/Documents/code/c++$ gcc -o pt pthread.cpp -lpthread pthread.cpp:12:9: warning: address of function 'pthread_create' will always evaluate to 'true' [-Wpointer-bool-conversion] if (pthread_create) { ~~ ^~~~~~~~~~~~~~ pthread.cpp:12:9: note: prefix with the address-of operator to silence this warning if (pthread_create) { ^ & 1 warning generated. – shengfu zou Nov 06 '17 at 08:06
  • Can't reproduce. Works for me with `gcc (Ubuntu 5.4.1-2ubuntu1~16.04) 5.4.1 20160904`. Please post build logs with `--verbose` compilation. – Mike Kinghan Nov 06 '17 at 11:47
  • It is too long and can not show bellow – shengfu zou Nov 06 '17 at 13:01

0 Answers0