5

I want to use Autotools in order to create a .so file, so that I can load it using dlsym. I have read some similar topics, but none of the suggested solutions did the trick for me. Here is what i have:

I want to compile a simple hello.cpp to a .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 type in:

    autoreconf -i
    ./configure
    make

I get a libtest.la file, but sadly no .so file. If it helps this is how i normally compile the hello.cpp into a hello.so:

    g++ -Wall -shared -rdynamic -fPIC hello.cpp -o hello.so

I would be grateful if anyone told me what I am doing wrong, so that i finally get the .so file.

Community
  • 1
  • 1
bl618515
  • 107
  • 7

1 Answers1

5

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 this is a plugin? (dlsym) then I have given 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

That produces a *.so for me.

EDIT:

I just had a thought, where are you looking for your *.so file? It is located in the .libs/ sub-folder that gets created (don't miss the . at 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*
Galik
  • 47,303
  • 4
  • 80
  • 117
  • Can I generate the .so in another directory? – bl618515 Aug 22 '14 at 09:14
  • Sorry I don't know if it is possible to generate the files in another directory other than `.libs/` – Galik Aug 22 '14 at 09:24
  • Okay, but still that works for me. I tried it with a more complicated plug-in and it opened with dlsym. – bl618515 Aug 22 '14 at 09:29
  • thank you!, ps, is there any non-libtool tooling for making so files with autoconf? I cant find anything in the gnu manual that isn't libtool-centric . maybe its harmless to produce la files in addition to so files though, clearly idk. – ThorSummoner Mar 04 '22 at 05:21
  • @ThorSummoner I'm sorry, I don't know how to build `.so` files in autotools without `libtool`. – Galik Mar 04 '22 at 05:57