Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

c++ - How to create a shared library (.so) in an automake script?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...