Neozaru
Neozaru

Reputation: 1130

Autotools & Libtool - How to compile shared library from multiple source directories?

I'm trying to build a library from Autotools (Automake/Libtool)

My directory structure is :

src/MyLib/*some_sources*.cpp/.h + a Makefile.am
src/MyLib/Parsers/*some_other_sources.cpp/.h Makefile.am

When I put all my source files in the same directory (so, no "Parsers" subdir), allthing works fine. (compilation, then linkage from other programs)

But when I try my "ideal structure" (a base source folder + a "Parsers" subdir), I get a linkage error when I try to compile a program with the library :

<< usr/local/lib/mylib.so: undefined reference to <a function from "/Parsers" subdir > >>

It seems that Libtool doesn't link statically my subdir "Parsers" during compilation (maybe considering it as an external shared library ?)

Here is my base "Makefile.am" :

lib_LTLIBRARIES = mylib.la
SUBDIRS = Parsers .
mylib_la_SOURCES = <base_dir_source_files>

And my "Makefile.am" in Parsers subdir :

mylib_la_SOURCES = <parsers_dir_source_files>

Of course, I libtoolize-d, autoreconf-ed allthing, after creating "Parser" subdir.

Thank you in advance for your help.

Upvotes: 1

Views: 1207

Answers (1)

rodrigo
rodrigo

Reputation: 98328

The easiest thing in your case is simply not to add a Makefile.am file to the Parsers directory. Instead, add the source file names to MyLib/Makefile.am prefixed with $(srcdir)/Parsers/ (better with a variable).

Something like this:

src/
|
\--MyLib/
   |
   |--Makefile.am
   |--base.cpp
   |--base.h
   \--Parsers/
      |
      |--parser.cpp
      \--parser.h

And in src/MyLib/Makefile.am:

PARSERS=$(srcdir)/Parsers
foo_SOURCES: base.cpp base.h \
    $(PARSERS)/parse.cpp $(PARSERS)/parse.h

Upvotes: 2

Related Questions