phön
phön

Reputation: 1255

visual studio c++ static linking what to do

i want to read from a pcap file and used the winpcap libary ( http://www.winpcap.org/ ) for that. after downloading i installed it (and therefore i think their dlls got copied somewhere my system is searching for them). i want to STATIC link the libary to my project, because i dont want to add the winpcap dlls everytime on another computer(which wouldnt be a problem if the computer had installed winpcap too i guess).

in visual studio i added the include path for the headerfiles and the libary path. then i add the libarys "libwpcap.a" and "libpacket.a" to the additional dependencies. (there are also the files "wpcap.lib" and "packet.lib" in the directory, i think they are used for dynamic linking?). my code works fine but when i moved the executeable to my other computer, where i didnt installed winpcap before, it says missing wpcap.dll.. so its just dynamic linked, am i right?

but this case i wanted to prevent. so how can i tell the linker, that he has to link it static.. damn i am searching for it quite some time now. but i dont know what i am doing wrong :( thought .a files get static linked and .lib files are used to link dynamicly.

Upvotes: 0

Views: 1676

Answers (1)

Asaf
Asaf

Reputation: 4407

Let's separate between three modes when linking on Windows using VS:

  1. "Full" Static Linkage - a .lib file which is completely embedded in the final image (executable). No other files will be needed.
  2. "Pure" Dynamic Linkage - no linkage is done when building. When you do this, you need at runtime to manually load the .dll file (dynamic library) using e.g. LoadLibrary(), and then to manually request the address of the function you want to call using e.g. GetProcAddress().
  3. "Mixture" mode, which means that you do link against a 'stub' .lib file which does not contain the implementations of the functions, but automatically causes the loader of your executable to load the relevant DLLs and get the addresses of all used functions ("Import table").

The .lib files of modes #1 and #3 are different. Which file is created depends on the settings the creator of the library built her project with: If its "Static Library", a full-implementation .lib file will be created. If it's "Dynamic Library", a stub .lib file and a .dll file will be created. If you use a stub .lib file, your executable will need the .dll file available at runtime.

To solve your problem: If the full-implementation .lib file is not available in the distribution you downloaded, you can download the full WinPCap source code (available from their website) and build it yourself as a static library.

Note, however, that the license might not allow you to do that. I have no idea - you should check this on your own.

Also see:

Upvotes: 7

Related Questions