[질문]c언어 라이브러리 링크(?)에 대해..

visualplus의 이미지

제가 이해가 안되서 질문을 올립니다.
관련자료는 찾았지만.. 도통 이해가 안되네요..ㅠㅠ

제가 하고싶은것은 pcap라이브러리도 사용하고 제가 만든 임의의 라이브러리도 사용하는것입니다.

gcc -fPIC -Wall -g -c netbios.c
 
gcc -g -shared -Wl,-soname,liblbprobe.so.0 -o liblbprobe.so.0.0 netbios.o -lc
 
ln -sf liblbprobe.so.0 liblbprobe.so
 
mv liblbprobe.so* /usr/lib/linkbox
 
gcc -Wall -g -c pcap_test.c -o pcap_test.o
 
gcc -g -o pcap_test pcap_test.o -L/usr/lib/linkbox/ -Ilbprobe

제가 나름 만들어본 스크립트인데요.
lbprobe라는 이름의 라이브러리를 만들고 pcap_test.c 를 컴파일 할 때 이 라이브러리를 사용해서 컴파일 하라는것이라고 전 이해를 했는데..
컴파일이 안되네요.. 그래서 어디서 안되나 봤더니

gcc -Wall -g -c pcap_test.c -o pcap_test.o

여기서 안되는것 같습니다. 아무래도 pcap라이브러리를 포함시키는 명령어가 없어서 그러는것 같은데..
어떤식으로 넣어줘야 할지 잘 모르겠습니다..

대강이라도 하는법을 알려주시면 감사하겠습니다.
라이브러리를 제대로 이해 못한상태에서 만들려니 이러는것 같네요 ..

dragonkun의 이미지

해당 라인에 -lpcap 옵션을 추가해 보세요.

--
Emerging the World!

Emerging the World!

visualplus의 이미지

이런 에러들이 뜹니다.

pcap_test.c: In function 'get_flags_value':
pcap_test.c:69: warning: statement with no effect
pcap_test.c: In function 'callback':
pcap_test.c:107: warning: implicit declaration of function 'inet_ntoa'
pcap_test.c:107: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
pcap_test.c:108: warning: format '%s' expects type 'char *', but argument 2 has type 'int'
pcap_test.c:82: warning: unused variable 'chcnt'
pcap_test.c:79: warning: unused variable 'count'
pcap_test.c: In function 'main':
pcap_test.c:194: warning: assignment makes pointer from integer without a cast
pcap_test.c:198: warning: assignment makes pointer from integer without a cast
pcap_test.c:167: warning: unused variable 'packet'
pcap_test.c:166: warning: unused variable 'eptr'
pcap_test.c:164: warning: unused variable 'hdr'
pcap_test.c:228: warning: control reaches end of non-void function
pcap_test.o: In function `main':
/root/lb_probe/pcap_test.c:185: undefined reference to `pcap_lookupnet'
/root/lb_probe/pcap_test.c:204: undefined reference to `pcap_open_live'
/root/lb_probe/pcap_test.c:212: undefined reference to `pcap_compile'
/root/lb_probe/pcap_test.c:218: undefined reference to `pcap_setfilter'
/root/lb_probe/pcap_test.c:227: undefined reference to `pcap_loop'

pcap관련 함수들이 정의되지 않았다는걸로 봐선.. 라이브러리가 포함 안된게 맞는것 같은데..
그 옵션을 줘도 같은 에러가 나네요 ㅠㅠ

gcc -o pcap_test pcap_test.c -lpcap -I/usr/include/pcap 이렇게 하면 정상적으로 됩니다..

gcc -Wall -g -c pcap_test.c -o pcap_test.o -lpcap 이런식으로 하면 안됩니다..

-Wall -g 이런것 때문에 안되는것일까요..?

SoulreaveR의 이미지

/root/lb_probe/pcap_test.c:185: undefined reference to `pcap_lookupnet'
/root/lb_probe/pcap_test.c:204: undefined reference to `pcap_open_live'
/root/lb_probe/pcap_test.c:212: undefined reference to `pcap_compile'
/root/lb_probe/pcap_test.c:218: undefined reference to `pcap_setfilter'
/root/lb_probe/pcap_test.c:227: undefined reference to `pcap_loop'

이 에러는 컴파일러가 뱉는 메시지이구요, 링커와는 관계 없습니다. pcap_test.c가 어떤 소스인지 봐야 알겠지만, 제 생각에는 저 함수들을 function pointer같은 곳에 집어넣는 것 같네요. 그 얘기는 그 함수에 대한 선언이나 정의를 pcap_test.c가 알아먹어야 한다는 뜻입니다. include하는 파일 중 일부가 /usr/include/pcap 내부에 있고, 그 파일에 해당 함수에 대한 선언이 있으면 pcap_test.c가 정상적으로 '컴파일' 되겠죠.

bushi의 이미지

틀렸습니다. 'undefined reference' 는 링커에 의해 출력되는 메시지 입니다.

질문하신 분이 컴파일과 링크를 구분 못하고 있는다는 점에는 동의합니다.
어떤 옵션을 어느 때 줘야 하는 지 알 수가 없어서 답답하신게죠.

OTL

SoulreaveR의 이미지

잘못봤습니다.

댓글 달기

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 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <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].
  • 사용할 수 있는 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>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <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].
  • You can use Textile markup to format text.
  • 사용할 수 있는 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>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <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].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 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>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.