www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Trying to compile weather program

reply "Tony" <tonytdominguez aol.com> writes:
I found this weather program on the main page (it seems to rotate 
what it here):

// Get your local weather report
pragma(lib, "curl");
import std.functional, std.json, std.net.curl,
     std.stdio, std.string;

alias getJSON = pipe!(get, parseJSON);
auto K2C = (float f) => f - 273.15;
auto K2F = (float f) => f / 5 * 9 - 459.67;

void main()
{
     auto loc = getJSON("ipinfo.io/")["loc"]
         .str.split(",");
     auto resp = getJSON(
         "api.openweathermap.org/data/2.5/weather" ~
         "?lat=" ~ loc[0] ~ "&lon=" ~ loc[1]);

     auto city = resp["name"].str;
     auto country = resp["sys"]["country"].str;
     auto desc = resp["weather"][0]["description"].str;
     auto temp = resp["main"]["temp"].floating;

     writefln(`
         +-----------------------------------------+
         |%s|
         +-----------------------------------------+
         |  weather      |  %-23s|
         +-----------------------------------------+
         |  temperature  |  %.2f°C (%.2f°F)      |
         +-----------------------------------------+
         `.outdent,
         centerJustifier(city ~ ", " ~ country, 41),
         desc, temp.K2C, temp.K2F);
}

I am compiling on Ubuntu 14.02 with DMD v2.066.1

I get this compile error:

weather_report.d(32): Error: undefined identifier centerJustifier
Aug 23 2015
next sibling parent anonymous <anonymous example.com> writes:
On Sunday 23 August 2015 11:54, Tony wrote:

 weather_report.d(32): Error: undefined identifier centerJustifier
`centerJustifier` is new in 2.068. You're probably using an older version of D. You can replace `centerJustifier` with `center` here.
Aug 23 2015
prev sibling next sibling parent "BBasile" <bb.temp gmx.com> writes:
On Sunday, 23 August 2015 at 09:54:37 UTC, Tony wrote:
 I found this weather program on the main page (it seems to 
 rotate what it here):

 [...]
try with `center()` or update the compiler. centerJustifier() was added on 25 Apr 2015 so after 2.066.1 release: https://github.com/D-Programming-Language/phobos/commit/f85101eea1b875311e5716143cd6346fe4655f02
Aug 23 2015
prev sibling next sibling parent reply "Tony" <tonytdominguez aol.com> writes:
Thanks for the replies. It compiles OK with just. However, it 
isn't linking:

/usr/bin/ld: cannot find -lcurl


I do have some versions of libcurl on my system:

/usr/lib/x86_64-linux-gnu/libcurl.so.3
/usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0
/usr/lib/x86_64-linux-gnu/libcurl.so.4

I see there is a -L option to pass things to the linker

-Llinkerflag   pass linkerflag to link

but I am not sure how to use it.
Aug 23 2015
next sibling parent "Tony" <tonytdominguez aol.com> writes:
On Sunday, 23 August 2015 at 16:00:19 UTC, Tony wrote:
 Thanks for the replies. It compiles OK with just. However, it 
 isn't linking:

 /usr/bin/ld: cannot find -lcurl


 I do have some versions of libcurl on my system:

 /usr/lib/x86_64-linux-gnu/libcurl.so.3
 /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0
 /usr/lib/x86_64-linux-gnu/libcurl.so.4

 I see there is a -L option to pass things to the linker

 -Llinkerflag   pass linkerflag to link

 but I am not sure how to use it.
I tried ld weather_report.o -lcurl -L/usr/lib/x86_64-linux-gnu and ld weather_report.o -lcurl -L /usr/lib/x86_64-linux-gnu but it also says it can't find libcurl: ld: cannot find -lcurl
Aug 23 2015
prev sibling next sibling parent reply "Gerald Jansen" <gjansen ownmail.net> writes:
On Sunday, 23 August 2015 at 16:00:19 UTC, Tony wrote:
 /usr/bin/ld: cannot find -lcurl
Just the other day I had a similar problem (compiling vibenews, ld complained of missing -levent and -lssl), which I managed to solve simply by installing the development versions of the libraries (i.e. libevent-dev and libssl-dev). Maybe if you install libcurl-dev you will have the same luck I had. (But hopefully someone will give a more enlightened reply.)
Aug 23 2015
parent "Tony" <tonytdominguez aol.com> writes:
On Sunday, 23 August 2015 at 16:20:04 UTC, Gerald Jansen wrote:
 On Sunday, 23 August 2015 at 16:00:19 UTC, Tony wrote:
 /usr/bin/ld: cannot find -lcurl
Just the other day I had a similar problem (compiling vibenews, ld complained of missing -levent and -lssl), which I managed to solve simply by installing the development versions of the libraries (i.e. libevent-dev and libssl-dev). Maybe if you install libcurl-dev you will have the same luck I had. (But hopefully someone will give a more enlightened reply.)
Synaptic gave me a choice of 3 libcurl* libcurl4-nss-dev (NSS flavour) libcurl4-gnutls-dev (GnuTLS flavour) libcurl4-openssl-dev (OpenSSL flavour) Confusing that there are 3 choices. I downloaded the OpenSSL one and the "can't find lcurl" has gone away. But I am now getting linking errors with regard to curl certain curl functions not being found.
Aug 24 2015
prev sibling parent reply Yazan D <invalid email.com> writes:
On Sun, 23 Aug 2015 16:00:16 +0000, Tony wrote:

 Thanks for the replies. It compiles OK with just. However, it isn't
 linking:
 
 /usr/bin/ld: cannot find -lcurl
 
 
 I do have some versions of libcurl on my system:
 
 /usr/lib/x86_64-linux-gnu/libcurl.so.3
 /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0
 /usr/lib/x86_64-linux-gnu/libcurl.so.4
 
 I see there is a -L option to pass things to the linker
 
 -Llinkerflag   pass linkerflag to link
 
 but I am not sure how to use it.
I've had the same problem recently. What I did was that I ran `dmd main.d -L-L/usr/lib/x86_64-linux-gnu/ -L-lcurl -v`. It would still fail to link, but I can find the linking command from the verbose output. It was something like this: `gcc main.o -o main -m64 -L/usr/lib/x86_64-linux- gnu/ -lcurl -L/usr/lib/x86_64-linux-gnu -Xlinker --export-dynamic - l:libphobos2.a -lpthread -lm -lrt`. As you can see, -lcurl is there, but it still needs to be added again after -l:libphobos2.a. So just add it again so the command becomes: `gcc main.o -o main -m64 -L/usr/lib/x86_64-linux-gnu/ -lcurl -L/usr/lib/ x86_64-linux-gnu -Xlinker --export-dynamic -l:libphobos2.a -lpthread -lm - lrt -lcurl`. And it links and runs.
Aug 23 2015
parent reply "Tony" <tonytdominguez aol.com> writes:
On Monday, 24 August 2015 at 06:28:34 UTC, Yazan D wrote:
 On Sun, 23 Aug 2015 16:00:16 +0000, Tony wrote:

 Thanks for the replies. It compiles OK with just. However, it 
 isn't linking:
 
 /usr/bin/ld: cannot find -lcurl
 
 
 I do have some versions of libcurl on my system:
 
 /usr/lib/x86_64-linux-gnu/libcurl.so.3 
 /usr/lib/x86_64-linux-gnu/libcurl.so.4.3.0 
 /usr/lib/x86_64-linux-gnu/libcurl.so.4
 
 I see there is a -L option to pass things to the linker
 
 -Llinkerflag   pass linkerflag to link
 
 but I am not sure how to use it.
I've had the same problem recently. What I did was that I ran `dmd main.d -L-L/usr/lib/x86_64-linux-gnu/ -L-lcurl -v`. It would still fail to link, but I can find the linking command from the verbose output. It was something like this: `gcc main.o -o main -m64 -L/usr/lib/x86_64-linux- gnu/ -lcurl -L/usr/lib/x86_64-linux-gnu -Xlinker --export-dynamic - l:libphobos2.a -lpthread -lm -lrt`. As you can see, -lcurl is there, but it still needs to be added again after -l:libphobos2.a. So just add it again so the command becomes: `gcc main.o -o main -m64 -L/usr/lib/x86_64-linux-gnu/ -lcurl -L/usr/lib/ x86_64-linux-gnu -Xlinker --export-dynamic -l:libphobos2.a -lpthread -lm - lrt -lcurl`. And it links and runs.
I had an additional -lcurl in already in my output, but added another at the end: gcc weather_report.o -o weather_report -m64 -L/usr/lib/x86_64-linux-gnu/ -lcurl -L/usr/lib/x86_64-linux-gnu -Xlinker --export-dynamic -lcurl -l:libphobos2.a -lpthread -lm -lrt -lcurl but it still complains that it cannot find -lcurl. It even does it if I add -lcurl everywhere: gcc weather_report.o -o weather_report -m64 -L/usr/lib/x86_64-linux-gnu/ -lcurl -L/usr/lib/x86_64-linux-gnu -Xlinker --export-dynamic -lcurl -l:libphobos2.a -lcurl -lpthread -lcurl -lm -lrt -lcurl As an aside, I remember having to add additional mentions of a library in a link line years ago on Unix. I am surprised they haven't changed the way it works so that you only have to mention it once.
Aug 24 2015
parent reply "Tony" <tonytdominguez aol.com> writes:
I happened to notice that among my libcurl*s

libcurl-gnutls.so.3
libcurl-gnutls.so.4
libcurl-gnutls.so.4.3.0
libcurl.so.3
libcurl.so.4
libcurl.so.4.3.0

none were just libcurl.so. So I made a link for libcurl.so to the 
latest version and now I am getting the same link errors I got 
after downloading the -dev version. So apparently my "can't find 
-lcurl" was because I didn't have a non-versioned libcurl.so 
available as it went away when I created one (and there was 
probably one created by the -dev download).
Aug 24 2015
parent "Martin Nowak" <code dawg.eu> writes:
On Tuesday, 25 August 2015 at 05:27:16 UTC, Tony wrote:
 I happened to notice that among my libcurl*s

 libcurl-gnutls.so.3
 libcurl-gnutls.so.4
 libcurl-gnutls.so.4.3.0
 libcurl.so.3
 libcurl.so.4
 libcurl.so.4.3.0

 none were just libcurl.so. So I made a link for libcurl.so to 
 the latest version and now I am getting the same link errors I 
 got after downloading the -dev version. So apparently my "can't 
 find -lcurl" was because I didn't have a non-versioned 
 libcurl.so available as it went away when I created one
Yes, it's not particularly trivial to use libcurl currently, but thankfully all the libcurl complications will be gone in about 6 weeks with 2.069.0. https://github.com/D-Programming-Language/phobos/pull/3009
 (and there was probably one created by the -dev download).
Yes, the -dev packages come with unversioned symlinks and a pkg-config file (try `pkg-config --libs libcurl`).
Aug 25 2015
prev sibling parent "Martin Nowak" <code dawg.eu> writes:
On Sunday, 23 August 2015 at 09:54:37 UTC, Tony wrote:
     auto loc = getJSON("ipinfo.io/")["loc"]
         .str.split(",");
BTW, the IP location doesn't work too reliably, if someone knows a better alternative...
Aug 25 2015