Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statically linking to libarchive on Windows with MinGW

I've been using libarchive in my project for some time now and it's working great, at the moment I am dynamically linking to it, so on Windows the libarchive.dll file has to present on the system. I would now like to statically link to the library so I don't have to bother distributing the DLL, but I'm having real trouble trying to achieve this!

Currently, in my make file, I have something like this: -Lpath/to//libarchive/ -larchive

And this works, but it does a dynamic link. I don't know how to enforce a static link.

I can see in the libarchive directory there are two a files, libarchive.dll.a and libarchive_static.a. I suppose I want to link to libarchive_static.a but I can't seem to specify this, doing -larchive_static in the make file results in linker errors.

I was under the impression that static libraries under windows are lib files, but I get no such file type when I build libarchive.

How can I make a .lib file from libarchive. Also, as an extra question, what is the difference between an a file and a lib file?

Update

To statically link to libarchive, your library command for make should contain:

-Lpath/to//libarchive/ -larchive_static

This will link to the libarchive_static.a file. However, you also need to define LIBARCHIVE_STATIC in your code.

Now the problem is that libarchive depends on the bzip2 libraries (as well as others), and if you do not have a static build of them you will get linker errors something like:

undefined reference to `BZ2_bzCompressInit'

You need a static build of the dependent libraries and a similar command to the linker after the libarchive command:

-Lpath/to/bzip2/ -lbzip2

You can either build bzip2 from source, or do it the easy way and get a pre-built binary from the Gnu32Win project here: http://gnuwin32.sourceforge.net/packages.html

like image 442
oggmonster Avatar asked Jul 26 '12 10:07

oggmonster


1 Answers

Just add libarchive_static.a explicitly to your link command.

gcc -o YourApp.exe $(OBJS) path/to/libarchive_static.a $(OtherLibs)

".lib" files differ from compiler to compiler (Borland, Microsoft etc.), ".a" is an old "archive" format from UNIX's ar tool. It is now used only for the bundling of static libraries.

Currently, in my make file, I have something ... And this works, but it does a dynamic link

The .a file actually contains some code for dynamic linking to the .dll file, not the libarchive itself. On the startup the pointers to functions are allocated and dynamic linking is done.

like image 79
Viktor Latypov Avatar answered Nov 04 '22 19:11

Viktor Latypov