简体   繁体   中英

std.net.curl linker errors in Linux

I'm trying to use std.net.curl on 32-bit Ubuntu Linux (11.10 Ocelot) using DMD 2.058.

When I build ( dmd source.d ) I get:

/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticCtor30FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticCtor30FZv+0x6): undefined reference to `curl_global_init'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl.o): In function `_D3std3net4curl4Curl19_sharedStaticDtor31FZv':
std/net/curl.d:(.text._D3std3net4curl4Curl19_sharedStaticDtor31FZv+0x4): undefined reference to `curl_global_cleanup'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP4Impl6__dtorMFZv':
std/net/curl.d:(.text._D3std3net4curl4HTTP4Impl6__dtorMFZv+0x12): undefined reference to `curl_slist_free_all'
/usr/lib/gcc/i686-linux-gnu/4.6.1/../../../../lib/libphobos2.a(curl_12fd_140.o): In function `_D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP':
std/net/curl.d:(.text._D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP+0x53): undefined reference to `curl_slist_append'

[snip]

I added -L-lcurl to my command line (full command: dmd source.d -L-lcurl ) but I get the exact same result. I have libcurl4-openssl installed. I can build a simple C curl program on this computer without issue ( gcc simple.c -lcurl ). I'm not sure where to look from here to figure this out.

Update: Here is how the linker is invoked according to dmd -v :

gcc source.o -o source -m32 -lcurl -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt

As you yourself discovered, the order of libraries is incorrect. A quote from "An Introduction to GCC" : "A library which calls an external function defined in another library should appear before the library containing the function." In your case libphobos calls external library's (libcurl) function(s), so I guess libphobos should go before libcurl .

The book is available online. Chapter related to the OT is here: http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html

The DMD compiler is clearly broken in this respect. I published a patch to fix it (maybe not the best) but the maintainer of the compiler is not convinced this needs to be fixed at all so there is no much discussion about it.

Seemingly the simplest way to fix the phobos2/curl linker order problem, as mentioned elsewhere in this thread:

dmd source.d -L-lphobos2 -L-lcurl

Works like a charm for me.

I figured out if I obtain the linker command through -v and reorder it so the -lcurl is at the end it links without issue.

gcc source.o -o source -m32 -Xlinker -L/usr/lib -Xlinker -L/usr/lib64 -Xlinker --no-warn-search-mismatch -Xlinker --export-dynamic -lphobos2 -lpthread -lm -lrt -lcurl

I'm still curious if there is a way to fix this without manually running the linker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM