How to create a shared library (.so) in an automake script?
Asked Answered
K

1

29

Given some source file test.cpp I would like to create a shared library libtest.so . I am trying to do this within the scope of an automake file however I cannot seem to get this to work.

For example under g++ I do the following:

g++ -shared -fPIC test.cpp -o libtest.so

Then I can create another file that will depend on the shared library as follows:

g++ mytest.cpp libtest.so -o blah

I have read that automake only supports making shared libraries via libtool. I have tried to get my automake script to work as follows but it never seems to produce an .so . The closest I have gotten is for it to produce an .la and .o file:

In configure.ac:

AC_ENABLE_SHARED
AC_DISABLE_STATIC
AC_PROG_LIBTOOL(libtool)

in Makefile.am

lib_LTLIBRARIES=libtest.la
libtest_la_SOURCES=test.cpp
libtest_la_CFLAGS=-fPIC
libtest_la_CPPFLAGS=-fPIC
libtest_la_CXXFLAGS=-fPIC
libtest_la_LDFLAGS= -shared -fPIC

Could someone give me an example of building an .so based on the above?

Kingkingbird answered 18/1, 2012 at 19:49 Comment(3)
You should replace AC_PROG_LIBTOOL with LT_INITGambell
Bah... after writing this i realized that the above did in fact create the .so file in a hidden .libs directory of my source directory. Hopefully this helps someone else who wonders about this.Kingkingbird
Can someone post the complete examples of the files here? Not the source files, but configure.ac and Makefile.am. I can't make this work using the information in this thread.Nevermore
G
25

If you just put LT_INIT in configure.ac and in Makefile.am, do:

lib_LTLIBRARIES = libtest.la
libtest_la_SOURCES = test.cpp
libtest_la_LDFLAGS = -version-info 0:0:0

you should get a .so. You should not specify -fPIC to CFLAGS, etc. The -version-info specifier is not necessary, but is a good idea.

Gambell answered 18/1, 2012 at 19:59 Comment(2)
If it's for a test and not to be installed, shouldn't it be check_LTLIBRARIES?Dees
@Jack check_LTLIBRARIES will not build a .so! I think the "test" in the question is not referring to the same sort of test as the check_ prefix implies.Gambell

© 2022 - 2024 — McMap. All rights reserved.