Search code examples
dlibcurl

libcurl issue when using D's std.net.curl on Mac


I'm trying to get up to speed using libcurl with D on Mac.

I'm using the latest dmd2 compiler (DMD64 D Compiler v2.062).

The following example compiles and works fine on Windows:

import std.net.curl, std.conv, std.stdio;

void main() {
     string content = to!string(get("dlang.org"));
     writeln(content);
}

When compiling it on Mac I get the following output:

~/src $ dmd dcurl.d
Undefined symbols for architecture x86_64:
  "_curl_easy_cleanup", referenced from:
      _D3std3net4curl4Curl8shutdownMFZv in libphobos2.a(curl_1522_37c.o)
  "_curl_easy_duphandle", referenced from:
      _D3std3net4curl4Curl3dupMFZS3std3net4curl4Curl in libphobos2.a(curl_151e_149.o)
  "_curl_easy_init", referenced from:
      _D3std3net4curl4Curl10initializeMFZv in libphobos2.a(curl_151d_432.o)
  "_curl_easy_perform", referenced from:
      _D3std3net4curl4Curl7performMFbZi in libphobos2.a(curl_1528_2fb.o)
  "_curl_easy_setopt", referenced from:
      _D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionlZv in libphobos2.a(curl_1525_14c.o)
      _D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionAxaZv in libphobos2.a(curl_1524_14c.o)
      _D3std3net4curl4Curl3setMFE3etc1c4curl10CurlOptionPvZv in libphobos2.a(curl_1526_14c.o)
      _D3std3net4curl4Curl5clearMFE3etc1c4curl10CurlOptionZv in libphobos2.a(curl_1527_207.o)
  "_curl_easy_strerror", referenced from:
      _D3std3net4curl4Curl11errorStringMFiZAya in libphobos2.a(curl_1520_4a1.o)
  "_curl_global_cleanup", referenced from:
      _D3std3net4curl4Curl19_sharedStaticDtor29FZv in libphobos2.a(curl.o)
  "_curl_global_init", referenced from:
      _D3std3net4curl4Curl19_sharedStaticCtor28FZv in libphobos2.a(curl.o)
  "_curl_slist_append", referenced from:
      _D3std3net4curl3FTP3dupMFZS3std3net4curl3FTP in libphobos2.a(curl_1518_ea.o)
      _D3std3net4curl3FTP10addCommandMFAxaZv in libphobos2.a(curl_1518_ea.o)
      _D3std3net4curl4HTTP3dupMFZS3std3net4curl4HTTP in libphobos2.a(curl_1517_140.o)
      _D3std3net4curl4HTTP16addRequestHeaderMFAxaAxaZv in libphobos2.a(curl_1517_140.o)
  "_curl_slist_free_all", referenced from:
      _D3std3net4curl3FTP4Impl6__dtorMFZv in libphobos2.a(curl_1518_ea.o)
      _D3std3net4curl3FTP13clearCommandsMFZv in libphobos2.a(curl_1518_ea.o)
      _D3std3net4curl4HTTP4Impl6__dtorMFZv in libphobos2.a(curl_1517_140.o)
      _D3std3net4curl4HTTP19clearRequestHeadersMFZv in libphobos2.a(curl_1517_140.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
--- errorlevel 1

Which seems to imply a linking issue.

I have libcurl at: /usr/lib/libcurl.3.dylib /usr/lib/libcurl.4.dylib /usr/lib/libcurl.dylib

But I'm not even sure whether dmd is trying to use them.

I've done this before with C/C++ (on the same machine using these libraries) so I think it must be a versioning issue or else I need to pass some flags to the compiler.

Any help would be most appreciated.


Solution

  • You need to link against libcurl. It's not linked against by default. So, instead of

    dmd dcurl.d
    

    you need to do

    dmd -L-lcurl dcurl.d