Creating a .so file with Autotools
I want to use Autotools to create a .so file so that I can load it using dlsym. I've read several similar threads, but none of the suggested solutions did it for me. Here's what I have:
I want to compile a simple hello.cpp for .so.
configure.ac
AC_INIT(My Project, 0.1, my@email, myproject)
AC_PREREQ(2.68)
AC_COPYRIGHT(GNU General Public License)
AM_CONFIG_HEADER([config.h])
AC_PROG_CXX
AM_INIT_AUTOMAKE([1.9 foreign])
AC_CONFIG_FILES(Makefile)
AC_ENABLE_SHARED
AC_DISABLE_STATIC
LT_INIT
AC_OUTPUT
Makefile.am
lib_LTLIBRARIES = libtest.la
libtest_la_SOURCES = hello.cpp
libtest_la_LDFLAGS = -version-info 0:0:0
I got this idea from here , but when I enter:
autoreconf -i
./configure
make
I am getting libtest.la file, but unfortunately there is no .so file. If it helps, this is how I usually compile hello.cpp to hello.so:
g++ -Wall -shared -rdynamic -fPIC hello.cpp -o hello.so
I would be grateful if someone could tell me what I am doing wrong so that I finally get the .so file.
source to share
I think you need to add AM_PROG_LIBTOOL
to your configure.ac
:
AC_INIT(My Project, 0.1, my@email, myproject)
AC_PREREQ(2.68)
AC_COPYRIGHT(GNU General Public License)
AM_CONFIG_HEADER([config.h])
# I would add these three
AC_CONFIG_MACRO_DIR([m4])
AM_PROG_LIBTOOL
AC_PROG_INSTALL
AC_PROG_CXX
AM_INIT_AUTOMAKE([1.9 foreign])
AC_CONFIG_FILES(Makefile)
AC_ENABLE_SHARED
AC_DISABLE_STATIC
LT_INIT
AC_OUTPUT
Also in your Makefile.am if it's a plugin? (dlsym), then I gave the flags recommended for plugins:
ACLOCAL_AMFLAGS = -I m4
lib_LTLIBRARIES = libtest.la
libtest_la_SOURCES = hello.cpp
libtest_la_LDFLAGS = -module -avoid-version -export-dynamic
This creates a * .so for me.
EDIT:
I just had a thought, where are you looking for your * .so file? It is located in a subfolder .libs/
that is created (don't miss .
the beginning of the name (its hidden)):
ls -l .libs/
total 24K
-rw-rw-r-- 1 galik galik 2.6K Aug 22 10:00 hello.o
-rw-rw-r-- 1 galik galik 2.7K Aug 22 10:00 libtest.a
lrwxrwxrwx 1 galik galik 13 Aug 22 10:00 libtest.la -> ../libtest.la
-rw-rw-r-- 1 galik galik 908 Aug 22 10:00 libtest.lai
-rwxrwxr-x 1 galik galik 8.6K Aug 22 10:00 libtest.so*
source to share