gcc working with libcurl

Brian Dessent brian@dessent.net
Sat Nov 15 05:42:00 GMT 2008


c0d3walk3r wrote:

> Any suggestions/advice would be extremely welcome, this is one of my first
> times trying to use an external library and it's been something I've tried
> using for a while without success.  Keep in mind I am new to using gcc, so
> even if the answer is really simple, I'd appreciate a little more than just
> an issue of commands (unless it's extremely simple).

-L adds a directory to the list of search paths but it doesn't otherwise
do anything else.  You need to specify the libraries to link with -l.

Notice that that archive contains both static and shared versions of the
library.  If you want to link with the shared version of the library
(i.e. the import lib), use -lcurldll.  Note that order of arguments on
the command line matters, so put -l options after objects that reference
symbols in those libraries.  This works for me (run from the root of the
package):

$ gcc simple.c -o bin/simple -Iinclude -Llib -lcurldll

This will create a simple.exe that depends on libcurl.dll and the other
DLLs.  They must either be in the same directory as the executable or
somewhere in the PATH at runtime (that's why I create the output in the
bin directory.)

In theory if you wanted to statically link you'd use -lcurl instead. 
However it's a little more complicated than that.  Firstly libcurl uses
explicit dllexport/dllimport decorations in its headers.  This means if
you want to statically link you have to use the corresponding documented
define to tell the curl headers to disable dllimport of the symbols,
i.e. -DCURL_STATICLIB.  Also when statically linking you have to spell
out every library dependency, as opposed to above where you just have to
specify -lcurldll.  If you want to statically link you have to know that
this version of libcurl depends on libssh2 and openssl and zlib as well
as various Win32 APIs.  (Also this distribution lacks a static version
of the openssl libraries so it's not even possible to fully statically
link.  And the import libs for the openssl DLLs are missing to so you
can't use -l for them, but you can link directly to a DLL without an
import lib with the GNU linker.)

Anyway, for reference the following works for me to statically link:

$ gcc simple.c -o bin/simple_static -Iinclude -Llib -DCURL_STATICLIB
-lcurl -lssh2 bin/libeay32.dll bin/libssl32.dll -lwldap32 -lws2_32 -lz

Brian



More information about the Gcc-help mailing list