C++ 如何在 automake 脚本中创建共享库 (.so)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8916425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:19:00  来源:igfitidea点击:

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

c++shared-librariesautotoolsautomake

提问by skimon

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.

给定一些源文件 test.cpp 我想创建一个共享库 libtest.so 。我正在尝试在 automake 文件的范围内执行此操作,但是我似乎无法使其正常工作。

For example under g++ I do the following:

例如在 g++ 下,我执行以下操作:

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 scriupt 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:

我读过 automake 只支持通过 libtool 创建共享库。我试图让我的 automake 脚本按如下方式工作,但它似乎从未产生 .so 。我得到的最接近的是它生成一个 .la 和 .o 文件:

in configure.ac:

在configure.ac:

AC_ENABLE_SHARED
AC_DISABLE_STATIC
AC_PROG_LIBTOOL(libtool)

in Makefile.am

在 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 ?

有人可以给我一个基于上述构建 .so 的示例吗?

采纳答案by William Pursell

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

如果您只是将 LT_INIT 放在 configure.ac 和 Makefile.am 中,请执行以下操作:

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.

你应该得到一个.so。您不应该为 CFLAGS 等指定 -fPIC。 -version-info 说明符不是必需的,但它是一个好主意。