Search code examples
cpcapundefined-reference

proj.c:(.text+0x140): undefined reference to `pcap_open_offline'


I'm trying to use pcap functions, but it giving me compiler error:

project.c:(.text+0x140): undefined reference to `pcap_open_offline'

I have installed library and while compiling I give "-lpcap" at the and as it advised in many forums.

What can be wrong, please?


Solution

  • You need to understand what the arguments evoke into the linker.

    I am supposing you are using Linux system with gcc, using ld as linker (note that this could change depending on the system and the linker used).

    In such case, -Lpath tell the linker where to look for the libraries that you tell it that are needed to be linked with your program to create the final binary. For example -L/usr/lib.

    when you type in for example:

    # gcc -L/usr/lib -lcap my_program.c -o my_program
    

    You are telling the linker to append /usr/lib to the list of paths to locate libraries, and to link the dynamic library "libcap.so" with your program.

    Other modifiers for the path used to locate libraries is LD_LIBRAY_PATH (the name of this environment variable could change from one system to another, review the manual of your linker).

    As you are using "-lcap" the error you get look to be related with the fact that no path is found where libcap.so exist. Locate that file into your system and pass the argument

    -L/path/to/the/directory/that/contain/libcap.so

    By the way, try to run this before any other thing and recompile:

    # sudo ldconfig