[완료]pthread_mutex_trylock 이 사라진 건 아니겠지요?

poplinux의 이미지

ubuntu 11.10 gcc 4.6.1 에서 pthread_mutex_trylock 사용하면 "undefined reference to `pthread_mutex_trylock" 이 떠 버립니다.

#include <pthread.h>
 
pthread_mutex_t lock=PTHREAD_MUTEX_INITIALIZER;
 
 
int main()
{
    int ret;
 
    ret = pthread_mutex_trylock(&lock);
 
}

(확인용으로 간단히 만들어 본 것이니 이해 부탁드립니다)

이 코드를 다른 서버 (gcc 4.4.3) 에서는 정상 컴파일 되는데 제 PC 에서는 되질 않네요.

이유가 있을까요?

oosap의 이미지


user@host:~$ gcc -o test test.c -lpthread
user@host:~$ ./test
user@host:~$ cat /etc/issue
Ubuntu 11.10 \n \l
 
user@host:~$ gcc --version
gcc (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

user@host:~$ gcc -o test test.c
/tmp/ccuhXsgy.o: In function `main':
test.c:(.text+0xe): undefined reference to `pthread_mutex_trylock'
collect2: ld returned 1 exit status

user@host:~$ gcc -o test -lpthread test.c
/tmp/ccuhXsgy.o: In function `main':
test.c:(.text+0xe): undefined reference to `pthread_mutex_trylock'
collect2: ld returned 1 exit status

같은 환경에서 제 테스트 결과로 해당 에러는 -lpthread 를 main.c 이후에 두지 않은 경우입니다.

Thanks for being one of those who care for people and mankind.
I'd like to be one of those as well.

poplinux의 이미지

감사합니다. 확인 되었습니다.

평소엔 아래와 같이 Makefile 을 만들어서 사용하였었는데

%:%.c
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< 

이젠 이렇게 해야 하나 봅니다.

%:%.c
    $(CC) $(CFLAGS)  -o $@ $< $(LDFLAGS) 

컴파일 옵션의 위치나 순서를 정확히 맞추는 것이 원래 표준이었었나요? 좀 불편하군요.

gcc 문서라도 좀 살펴보아야 겠습니다.

========================
조직 : E.L.D(Embedded Linux Developer/Designer)
블로그 : poplinux@tistory.com
카페 : cafe.naver.com/poplinux

임베디드 리눅스 관련 프리렌서 지향

poplinux의 이미지

LDFLAGS 위치 지정에 대한 내용 파악해서 뒤로 가는 것이 맞다면 지금까지 만들어 놓은 Makefile 들은 다 수정해 놓아야 할 것 같습니다.

이것참. 애매하네요.

========================
조직 : E.L.D(Embedded Linux Developer/Designer)
블로그 : poplinux@tistory.com
카페 : cafe.naver.com/poplinux

임베디드 리눅스 관련 프리렌서 지향

oosap의 이미지

gcc 에서 ld 가 링크할 때 오브젝트 파일들과 라이브러리 파일들로부터 심볼 스캔을 하는데 기본적으로는 1회의 스캔만 한다고 합니다. 그래서 오브젝트 파일들과 라이브러리 파일들이 gcc 의 명령줄에 나열될 때는 앞에서 뒤로 의존성을 가지도록 해야 한다고 합니다.

--start-group, --end-group 옵션으로 의존성이 얽혀있거나 불분명한 라이브러리들을 묶어주면 그 라이브러리 들의 심볼은 여러번 스캔하면서 링킹을 하게 된다고 합니다.
백창우씨 유틸리티 책 개정판 332 페이지 참조해보세요...

이 문제가 KLDP 에서 자주 QNA 에 올라오는 것같습니다.

Thanks for being one of those who care for people and mankind.
I'd like to be one of those as well.

oosap의 이미지

아 그리구 LDFLAGS 는 맨 뒤로 가게 하는게 맞는 것 같아요... 저두 이 문제 여러번 겪었습니다.

Thanks for being one of those who care for people and mankind.
I'd like to be one of those as well.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.