如何使用 libtool 创建二进制文件和 .so
我有一组 cpp 文件,我想将它们直接编译成二进制文件,并编译成共享库。
当我
bin_PROGRAMS=mybin
lib_LTLIBRARIES=libmylib.la
COMMON_SOURCES=f1.cpp f2.cpp f3.cpp
mybin_SOURCES=main.cpp $(COMMON_SOURCES)
libmylib_la_SOURCES=$(COMMON_SOURCES)
运行这个时,cpp文件被编译两次,一次使用libtool,一次不使用,有时libtool/automake抱怨
Makefile.am: object `f1.$(OBJEXT)' created both with libtool and without`
我尝试将COMMON_SOURCES放入.a文件中,但当我将.a与.la链接时libtool会抱怨(说它不可携带)。
我需要的是类似
bin_LTPROGRAMS=mybin
但不存在的
东西编辑:澄清 - 我正在使用 automake/autoconf。我上面展示的是我的 automake Makefile.am 的主要内容
I have a set of cpp files that I want to compile directly into a binary and also to compile into a shared library.
I have
bin_PROGRAMS=mybin
lib_LTLIBRARIES=libmylib.la
COMMON_SOURCES=f1.cpp f2.cpp f3.cpp
mybin_SOURCES=main.cpp $(COMMON_SOURCES)
libmylib_la_SOURCES=$(COMMON_SOURCES)
When I run this the cpp files are compiled twice, once with libtool and once without and sometimes libtool/automake complains
Makefile.am: object `f1.$(OBJEXT)' created both with libtool and without`
I tried putting COMMON_SOURCES into a .a file but then libtool complains when I link a .a with a .la (saying its not portable).
What I need is something like
bin_LTPROGRAMS=mybin
but that doesnt exist
edit: clarification - I am using automake/autoconf. What I have shown above is the meat of my automake Makefile.am
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
具体链接到公共源库:
如果
libmylib.la
最终使用了不应链接到mybin
的文件,请创建一个 libtool 便利库,使用Makefile.am
如下:这将链接
f1 .cpp
、f2.cpp
、f3.cpp
、f4.cpp
、f5.cpp
和f6.cpp
到libmylib.la
和main.cpp
、f1.cpp
、f2。 cpp
和f3.cpp
到mybin
中。Link against the library of common sources specifically:
If
libmylib.la
ends up using files that shouldn't be linked intomybin
, create a libtool convenience library, using aMakefile.am
something like this:This will link
f1.cpp
,f2.cpp
,f3.cpp
,f4.cpp
,f5.cpp
andf6.cpp
intolibmylib.la
andmain.cpp
,f1.cpp
,f2.cpp
andf3.cpp
intomybin
.问题在于,将公共源制作成共享对象时与制作成静态档案时需要进行不同的编译;例如,在前者的情况下,需要向
g++
传递-fPIC
标志。我建议使用两个构建目录。
假设这个源层次结构:
您将在
./src/Makefile.am
中使用类似的内容:然后您在
./src/Makefile.am
中创建目录Release
和ReleaseDisableShared
代码>./。在目录./Release
中运行:在
./ReleaseDisableShared
中运行:在每个构建目录中构建后,在
./ReleaseDisableShared
中使用mybin
code>./ReleaseDisableShared/src/mybin 和./Release/src/libmylib.so
处的libmylib.so
。另请参阅:
The issue is that the common sources need to be compiled differently when they are being made into a shared object than when they are being made into a static archive; in the case of the former, for example,
g++
needs to be passed the-fPIC
flag.What I suggest is using two build directories.
Assuming this source hierarchy:
you would use something like this in
./src/Makefile.am
:Then you create directories
Release
andReleaseDisableShared
in./
. In directory./Release
you run:and in
./ReleaseDisableShared
you run:After building in each build directory, you use the
mybin
at./ReleaseDisableShared/src/mybin
and thelibmylib.so
at./Release/src/libmylib.so
.See also:
如果目标包含每个目标
CFLAGS
(或类似的),automake
将创建单独的目标文件来构建该目标。尝试向mybin
添加一些无操作标志,例如:或
If a target contains per-target
CFLAGS
(or similar),automake
will make separate object files for building that target. Try adding some no-op flags tomybin
, something like:or
您必须为使用 libtool 创建的目标文件指定不同的扩展名,这样它们就不会发生冲突。事实上,这些文件是文本文件,其中包含具有可重定位和不可重定位代码的目标文件的元信息(这是通过 -fPIC gcc 命令行参数控制的)。 libtool创建的真实文件通常存储在“.libs”子目录中。基本的 makefile 如下所示:
You have to give object files created with libtool a different extension so they do not conflict. In fact, those files are text files containing meta information for both object files with relocatable and non-relocatable code (this is controlled with -fPIC gcc command line argument). The real files created by libtool are usually stored in ".libs" subdirectory. The basic makefile will look like this: