math.h의 log 함수를 사용하는 라이브러리가 있습니다. 이 라이브러리를 컴파일하고 패키징 할 때 컴파일 오류가 발생하지 않습니다. 이는 정상적인 것입니다 (필자는 생각합니다).정적 라이브러리를 C 수학 라이브러리에 올바르게 연결
Compiling mytestlist using "mytestlist.o":
gcc mytestlist.o -I/student/cmpt332/pthreads -I. -std=c99 -Wall -pedantic -L. -L/student/cmpt332/pthreads/lib/linuxx86_64/ -llist -o mytestlist
./liblist.a(list_adders.o): In function `NodeCreate':
list_adders.c:(.text+0x343): undefined reference to `log'
./liblist.a(list_adders.o): In function `ListCreate':
list_adders.c:(.text+0x62f): undefined reference to `log'
./liblist.a(list_adders.o): In function `ListFree':
list_adders.c:(.text+0xdcc): undefined reference to `log'
list_adders.c:(.text+0xe55): undefined reference to `log'
list_adders.c:(.text+0xefb): undefined reference to `log'
./liblist.a(list_adders.o):list_adders.c:(.text+0xf96): more undefined references to `log' follow
collect2: error: ld returned 1 exit status
Makefile:47: recipe for target 'mytestlist' failed
make: *** [mytestlist] Error 1
왜 이런 일이 : 나는 응용 프로그램에서 라이브러리를 사용하려고하면
지금, GCC는 나에게 링커 오류를 준다? 유일한 해결책은 라이브러리를 사용하는 프로그램을 컴파일 할 때 (비록 프로그램 자체가 math.h를 사용하지 않지만) gcc에 -lm
옵션을 제공해야한다는 것입니다.하지만이 작업이 번거롭다 고 생각합니다.
또한 라이브러리를 컴파일 할 때 -lm
옵션을 제공하려고했지만 응용 프로그램이 라이브러리를 사용하여 컴파일 될 때 동일한 링커 오류가 발생합니다.
라이브러리를 사용하는 다른 프로그램에 -lm
을 제공하지 않고 math.h로 라이브러리를 컴파일 할 수있는 방법이 있습니까? 당신이 궁금해하는 경우
, 내가 사용하여 라이브러리 구성하는 각 개체 컴파일 :
gcc -std=c99 -Wall -pedantic -static -I. -c list_adders.c -o list_something.o -lm
을 그리고 라이브러리를 사용하여 패키지됩니다 : 당신의 gcc -c
명령에서
ar cvfr liblist.a list_something.o ...
정적 라이브러리가 연결되어 있지 않습니다. 수학 라이브러리와 연결하는 방법은 없기 때문에 링크하는 방법이 없습니다. 응용 프로그램이나 공유 라이브러리는 링크하지만 정적 라이브러리는 링크하지 않습니다. 정적 라이브러리의 의존성을 라이브러리 자체에 표시하거나 기록하는 방법도 없습니다. –