Contenu | Rechercher | Menus

Annonce

Si vous avez des soucis pour rester connecté, déconnectez-vous puis reconnectez-vous depuis ce lien en cochant la case
Me connecter automatiquement lors de mes prochaines visites.

À propos de l'équipe du forum.

#1 Le 08/05/2017, à 14:49

Bensuperpc

[résolu]Problème lors de la compilation avec GCC6 ou 7.

Bonjour,

J'ai trouvé quelques programmes sur internet pour créer des threads(en  C), juste pour expérimenter un peu.
Mais lorsque que compile ce programme avec GCC 6 ou 7:

gcc Test2.c -o Ben2
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
 
#define NUM_THREADS 2
 
/* create thread argument struct for thr_func() */
typedef struct _thread_data_t {
  int tid;
  double stuff;
} thread_data_t;
 
/* thread function */
void *thr_func(void *arg) {
  thread_data_t *data = (thread_data_t *)arg;
 
  printf("hello from thr_func, thread id: %d\n", data->tid);
 
  pthread_exit(NULL);
}
 
int main(int argc, char **argv) {
  pthread_t thr[NUM_THREADS];
  int i, rc;
  /* create a thread_data_t argument array */
  thread_data_t thr_data[NUM_THREADS];
 
  /* create threads */
  for (i = 0; i < NUM_THREADS; ++i) {
    thr_data[i].tid = i;
    if ((rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))) {
      fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
      return EXIT_FAILURE;
    }
  }
  /* block until all threads complete */
  for (i = 0; i < NUM_THREADS; ++i) {
    pthread_join(thr[i], NULL);
  }
 
  return EXIT_SUCCESS;
}

J'ai cette erreur:

/tmp/ccrh09ZG.o : Dans la fonction « main » :
Test2.c:(.text+0xab) : référence indéfinie vers « pthread_create »
Test2.c:(.text+0x107) : référence indéfinie vers « pthread_join »
collect2: error: ld returned 1 exit status

Pour le GCC7:

gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7-20170407-0ubuntu2' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.0.1 20170407 (experimental) [trunk revision 246759] (Ubuntu 7-20170407-0ubuntu2) 

Merci pour vos réponses,

Dernière modification par Bensuperpc (Le 08/05/2017, à 18:35)

Hors ligne

#2 Le 08/05/2017, à 16:58

claudius01

Re : [résolu]Problème lors de la compilation avec GCC6 ou 7.

Bonjour,

Manque tout simplement un '-l pthread' dans l'édition de liens ;-)

pi@raspberrypi:~/Tmp $ gcc Test2.c -o Ben2 -l pthread && echo $?
0
pi@raspberrypi:~/Tmp $ ./Ben2 && echo $?
hello from thr_func, thread id: 1
hello from thr_func, thread id: 0
0

A suivre...

Hors ligne

#3 Le 08/05/2017, à 18:34

Bensuperpc

Re : [résolu]Problème lors de la compilation avec GCC6 ou 7.

Merci ça marche ! c'est bizarre que sur le site ils n'ont pas mis

-l pthread

mais bon smile encore merci,

Hors ligne