including libraries in Makefile.am

hi,
I have the following code in my Makefile.am that works fine for simple programs:

bin_PROGRAMS=test
test_SOURCES=test.cpp

so, when I run 'make test', it runs the following command:

g++ -o test test.cpp

however, when I tried to run a program that includes the QuantLib library, I have to give the following command:

g++ -o test test.cpp -L/usr/lib -lQuantLib

so, I modified my Makefile.am to include the following:

test_LDADD = /usr/lib/libQuantLib.so

but that doesn't work; it gives me the same bunch of errors as it does if I only ran my test program without the -L and -l flags.

Can you please help me fix my Makefile.am

thanks!

-lQuantLib tells the linker to link the file libQuantLib.a, that is a static library, not a dynamic one (shared object) .so, which, being a dynamic
lib, would get loaded at runtime. Just specify the -lQuantLib in the rule:

LDFLAGS += -L/usr/lib
test_LDADD = -lQuantLib
bin_PROGRAMS:
         ${CXX} ${CXXFLAGS} ${LDFLAGS} -o $@ ${test_SOURCES} ${test_LDADD}

should do the trick, although /usr/lib should be in the default path to check for libs, and shouldn't need to be explicitly defined.
Obviously, file libQuantLib.a needs to be present in /usr/lib.

hi mirni, thx. for your response.
I edited my Makefile with that code, but it still gives me the undefined reference errors.
Makefile.am

LDFLAGS+ = -L/usr/lib
bin_PROGRAMS = test
test_SOURCES=test.cpp
test_LDADD = -lQuantLib
bin_PROGRAMS:
    ${CXX} ${CXXFLAGS} ${LDFLAGS} -o $@ ${test_SOURCES} ${test_LDADD}

here's a sample of the errors:

test.o:(.rodata._ZTIN8QuantLib13TermStructureE[typeinfo for QuantLib::TermStructure]+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
test.o:(.rodata._ZTIN8QuantLib12ExtrapolatorE[typeinfo for QuantLib::Extrapolator]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
test.o:(.rodata._ZTIN8QuantLib19StochasticProcess1D14discretizationE[typeinfo for QuantLib::StochasticProcess1D::discretization]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
test.o:(.rodata._ZTIN8QuantLib17StochasticProcess14discretizationE[typeinfo for QuantLib::StochasticProcess::discretization]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'
test.o:(.rodata._ZTIN8QuantLib5QuoteE[typeinfo for QuantLib::Quote]+0x0): undefined reference to `vtable for __cxxabiv1::__vmi_class_type_info'
test.o:(.rodata._ZTIN8QuantLib7VisitorINS_6PayoffEEE[typeinfo for QuantLib::Visitor<QuantLib::Payoff>]+0x0): undefined reference to `vtable for __cxxabiv1::__class_type_info'

when at the $ prompt, I give the following command:
g++ -o test test.cpp -L/usr/lib -lQuantLib
it works perfectly fine??

LDFLAGS += -L/usr/lib

The plus and equal need to be together, it's one operator, just like in C.
To debug Makefile, you should look at the command that's executed when you run 'make'. The command that's being run is echoed on stdout. Look carefully at that command -- it should be exactly what you want:

g++ -L/usr/lib -o test test.cpp -lQuantLib

Since you are getting the errors, you will see something else:
it'll be missing either the -L/usr/lib or -lQuantLib.

Also, just to be safe, delete the old object file test.o before recompiling.

hi mirni,
thanks for the correction.
I was getting an error message with LD_FLAGS, wherein automake warned me to use AM_LDFLAGS since LD_FLAGS is a user defined variable. then I got warned to use = for AM_LDFLAGS before using +=, so I rewrote Makefile.am as:

AM_LDFLAGS = -L/usr/lib
bin_PROGRAMS = test
test_SOURCES=test.cpp
test_LDADD = -lQuantLib
bin_PROGRAMS:
    ${CXX} ${CXXFLAGS} ${AM_LDFLAGS} -o $@ ${test_SOURCES} ${test_LDADD}

and when I run make test,
this is what it shows:

g++ -c -o test.o test.cpp

I deleted the old test.o file, and re-ran 'make test'

g++ test.cpp -o test

so, obviously it is not reading the -L or -l flags....any thoughts
why and how I can fix it...

Sorry bacpp, I mistakenly thought you were writing a Makefile and using GNU make. My advice was for that case.
Usage of GNU automake and Makefile.am is different, and I don't have experience with writing Makefile.am myself, but from automake it seems that all you need is:

test_LDADD = /usr/lib/libQuantLib.a

thanks mirni, but it still ain't working :confused:....I think I will go and study the Libtool and try it again :wall:....if it works, I'll post the code here.

Is there a reason you are using automake? It requires you have configure.ac set-up and is a tool for complex projects.
When invoking 'make test', you are invoking GNU make, and that can't find any Makefile, so is applying a default rule.

Maybe you want to write a Makefile instead of using automake.

---------- Post updated at 05:35 AM ---------- Previous update was at 05:31 AM ----------

simple Makefile works just fine:

$ cat Makefile
bin_PROGRAMS=test
test_SOURCES=test.cpp
LDFLAGS += -L/usr/lib
test_LDADD = -lQuantLib

${bin_PROGRAMS}: 
    ${CXX} ${CXXFLAGS} ${LDFLAGS} -o $@ ${test_SOURCES} ${test_LDADD}

$ make
g++  -L/usr/lib -o test test.cpp -lQuantLib

Is there a reason you are using automake? for learning-sake...I have setup configure.ac, and my Makefile.in file has also been created, so I wanted to test it out.

When invoking 'make test', you are invoking GNU make, and that can't find any Makefile, so is applying a default rule. ....I see....there should be a way to fix that, right....

That's a good reason... ok.

There's nothing to fix. You're mixing apples and oranges. You want to run 'automake' not 'make'.

Also, there is a section 'programming' on this forum, where your post would be more appropriate and would be looked at by competent people, that, unlike myself, have experience with automake.
In any case, good luck!

I am obviously not doing something right then....I basically followed the steps for automake from
Automake - Wikipedia, the free encyclopedia
after creating the Makefile.am, I ran automake, and then ./configure, followed by 'make test'...and that's where it started...
this might be a dumb thing I did, but nevertheless, when I type 'automake test', it returns:

automake: no Automake input file found for `test'
automake: no input file found among supplied arguments

can you please tell me how i should run automake :slight_smile:

So your Makefile has been generated? And since you're getting the error, the library hasn't been included in there, right? So you'll have to go back, and fix <something> to have the correct rule (with the lib) in generated Makefile.

The manual to automake is here:
automake

As much as I'd like to help you, I have no experience with automake and don't know how I could be of more help...

1 Like

thanks mirni,
yes, it has been generated and just got that fixed....I had to rename Makefile.am to Makefile, and it worked.
here's what I have in my Makefile:

AM_LDFLAGS = -L/usr/lib
bin_PROGRAMS = test
_SOURCES = test.cpp
_LDADD = -lQuantLib
${bin_PROGRAMS}:
    ${CXX} ${CXXFLAGS} -o $@ ${_SOURCES} ${AM_LDFLAGS} ${_LDADD}

all I did was issue 'make' and it did it's thing:)