0

I'm trying to learn QuantLib, this is my first program with which i intend to check that my environment is ok and i'm able to link to quantlib:

#include <ql/time/all.hpp>

using namespace QuantLib;

int main ()
{
  Date d1(14, February, 2014);
  return 0;
}

I've got my quantlib libraries installed under /usr/local/lib, headers under /usr/local/include/ql. I try to compile this little program with:

$ LC_ALL=C g++ -Wall -lQuantLib -o sample1 quantlib-sample-1.cpp
/tmp/cc4Z2xsf.o: In function `main':
quantlib-sample-1.cpp:(.text+0x1f): undefined reference to `QuantLib::Date::Date(int, QuantLib::Month, int)'
collect2: error: ld returned 1 exit status

The thing gets worse if i include "ql/quantlib.hpp" (much more errors like the one above). I tried passing "-L/usr/local/lib" for if my ldconfig is not ok.

I'm a bit lost here... Any clue?

roirodriguez
  • 1,685
  • 2
  • 17
  • 31

2 Answers2

1

The command used to compile is malformed. Library linking options need to go after outputs and inputs. This works:

$ LC_ALL=C g++ -Wall -o sample1 quantlib-sample-1.cpp -lQuantLib

With '-lQuantLib' at the end of the command.

It works both including 'ql/quantlib.hpp' or 'ql/time/all.hpp'.

roirodriguez
  • 1,685
  • 2
  • 17
  • 31
0

It works for me if I change the first line to the more general (and recommended) catch-all include:

edd@max:/tmp$ g++ -o qldate qldate.cpp -lQuantLib    ## no errors or warnings
edd@max:/tmp$ cat qldate.cpp 
#include <ql/quantlib.hpp>

using namespace QuantLib;

int main ()
{
  Date d1(14, February, 2014);
  return 0;
}
edd@max:/tmp$ 
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • This does not work for me. As i told in the question the thing gets worse (a lot of "Undefined reference" linker errors). – roirodriguez Feb 14 '14 at 19:42
  • Well that is then no longer reproducible. Also, you said that you *can* build the example so you may want to compare more closely what they do relative to what you do. You had one error which I fixed. – Dirk Eddelbuettel Feb 14 '14 at 19:44
  • Thanks. Let me make it work and test that it does not compile with "ql/time/all.hpp", after that i'll mark this correct if that's the case. – roirodriguez Feb 14 '14 at 19:45
  • I've posted my own answer, the problem was with compilation flags order. It works including 'ql/time/all.hpp'. Thanks for your time! – roirodriguez Feb 14 '14 at 20:08
  • 1
    Confirmed. I had a -l/-L typo. So it was command-order, and I showed you that too ;-) – Dirk Eddelbuettel Feb 14 '14 at 20:10