Programming

Linux에서 pthread_create에 대한 정의되지 않은 참조

procodes 2020. 3. 3. 23:10
반응형

Linux에서 pthread_create에 대한 정의되지 않은 참조


https://computing.llnl.gov/tutorials/pthreads/ 에서 웹에서 다음 데모를 선택했습니다.

#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main (int argc, char *argv[])
{
   pthread_t threads[NUM_THREADS];
   int rc;
   long t;
   for(t=0; t<NUM_THREADS; t++){
      printf("In main: creating thread %ld\n", t);
      rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
      if (rc){
         printf("ERROR; return code from pthread_create() is %d\n", rc);
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

그러나 내 컴퓨터에서 컴파일하면 (Ubuntu Linux 9.04 실행) 다음 오류가 발생합니다.

corey@ubuntu:~/demo$ gcc -o term term.c
term.c: In function ‘main’:
term.c:23: warning: incompatible implicit declaration of built-in function ‘exit’
/tmp/cc8BMzwx.o: In function `main':
term.c:(.text+0x82): undefined reference to `pthread_create'
collect2: ld returned 1 exit status

헤더에는 기능 pthread.h이 있어야하는을 포함하기 때문에 나에게 의미가 없습니다 pthread_create. 무슨 일이 일어나고 있는지 어떤 아이디어?


지금까지이 질문에 대한 대답은 모두 틀립니다 .
Linux의 경우 올바른 명령은 다음과 같습니다.

gcc -pthread -o term term.c

일반적으로 라이브러리는 명령 행에서 소스 및 오브젝트를 따라야 -lpthread하며 "옵션"이 아니며 라이브러리 스펙입니다. libpthread.a설치된 시스템에서만

gcc -lpthread ...

연결에 실패합니다.


일식으로

속성-> c / c ++ 빌드-> 설정-> GCC C ++ 링커-> 상단의 라이브러리에 "pthread"추가


다음 튜토리얼을 계속 읽으면 pthreads 코드에 사용되는 컴파일 명령의 몇 가지 예가 아래 표에 나열되어 있습니다.

https://computing.llnl.gov/tutorials/pthreads/#Compiling

여기에 이미지 설명을 입력하십시오


Linux 터미널에서 실행하면 다음 명령을 사용하여 컴파일했습니다 (컴파일하려는 c 파일을 test.c라고 가정하십시오).

gcc -o test test.c -pthread

그것이 누군가를 돕기를 바랍니다!


Linux의 경우 올바른 명령은 다음과 같습니다.

gcc -o term term.c -lpthread
  1. 컴파일 명령 바로 뒤에 -lpthread를 넣어야합니다.이 명령은 pthread.h 라이브러리로 프로그램을 실행하도록 컴파일러에 지시합니다.
  2. gcc -l은 라이브러리 파일과 연결합니다 .lib 접두사가없는 라이브러리 이름과 -l을 연결하십시오.

다음과 같이 컴파일하십시오 : gcc demo.c -o demo -pthread


cmake를 사용하는 경우 다음을 사용할 수 있습니다.

add_compile_options(-pthread)

또는

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

-lpthreadgcc와 함께 옵션을 사용해야합니다 .


속성 => C / C ++ 빌드 => GCC C ++ 링커 => 라이브러리 => 상단 부분 "라이브러리 (-l)"에 "pthread"만 추가하면됩니다. 그게 다야


맨 페이지를 확인하면 얻을 수 있습니다.

-pthread로 컴파일하고 링크하십시오.

SYNOPSIS
       #include <pthread.h>

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


       Compile and link with -pthread.
       ....

Visual Studio 2019 -pthread에서 프로젝트의 속성 페이지에서 다음을 지정 하십시오.

링커-> 명령 줄-> 추가 옵션

에서 입력 -pthread텍스트 상자에.


Anjuta에서 빌드 메뉴로 이동 한 다음 프로젝트 구성으로 이동하십시오. 구성 옵션 상자에서 다음을 추가하십시오.

LDFLAGS='-lpthread'

누군가도 도울 수 있기를 바랍니다 ...


여러 라이브러리를 사용하는 경우 라이브러리 종속성을 확인하십시오. (예 : -lpthread -lSDL ... <==> ... -lSDL -lpthread)


내가 추가 할 수있는 적절한 방법 생각 pthread으로는 CMake다음과 함께

find_package (Threads REQUIRED)

target_link_libraries(helloworld
    ${CMAKE_THREAD_LIBS_INIT}
)

참고 URL : https://stackoverflow.com/questions/1662909/undefined-reference-to-pthread-create-in-linux



반응형