自动制作第三方库 [英] automake third party libraries

查看:133
本文介绍了自动制作第三方库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用automake编译和链接第三方库?

How to compile and link third party libraries with automake?

我的文件结构是:

program/
  |
  +--src/
  |   |
  |   +--Makefile.am
  |   +--main.cpp
  |
  +--lib/
  |   |
  |   +--Makefile.am
  |   +--library.cpp
  |
  +--Makefile.am
  +--configure.ac
  +--README

automake文件的内容非常通用:

Contents of automake files are pretty generic:

# src/Makefile.am
bin_PROGRAMS = program
program_SOURCES = main.cpp


# Makefile.am
SUBDIRS = src lib
dist_doc_DATA = README


# configure.ac
AC_INIT([program], [1.0])
AM_INIT_AUTOMAKE([-Wall])
AC_PROG_CXX
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile src/Makefile lib/Makefile])
AC_OUTPUT

lib/Makefile.am的内容应该是什么?

推荐答案

(不知道为什么您似乎自己可以控制库代码时​​为什么说第三方" ...有关的更多信息使用Automake创建和使用库,请参考 GNU Automake手册中有关库的部分)

lib/Makefile.am

lib_LIBRARIES = libYOURLIB.a
libYOURLIB_a_SOURCES = library.cpp

如果您不想安装库本身,则可以使用noinst_lib_LIBRARIES.请注意,我假设您只想构建一个静态库.请参阅以下网站的构建共享库部分GNU Automake手册,用于与Libtool集成以生成共享库.当然,您可以手动执行此操作,但是使用Libtool可以解决各种平台差异,因此要容易得多.

You can use noinst_lib_LIBRARIES if you don't want to install the library itself. Note that I'm assuming you want to build a static library only. See the Building A Shared Library section of the GNU Automake manual for integrating with Libtool to produce a shared library. You can do it manually of course, but it's a lot easier with Libtool as it takes care of various platform differences.

要将您的库链接到program,您可以在
src/Makefile.am 中添加以下几行:

To link your library to program, you'd add the following lines in
src/Makefile.am:

program_DEPENDENCIES = $(top_builddir)/lib/libYOURLIB.a
program_LDADD = $(top_builddir)/lib/libYOURLIB.a

_DEPENDENCIES行仅告诉Automake program首先依赖于lib/libYOURLIB.a,而_LDADD行仅将库添加到链接器命令.

The _DEPENDENCIES line simply tells Automake that program relies on lib/libYOURLIB.a being built first, and the _LDADD line simply adds the library to the linker command.

以上假设您已经有一个规则来构建库.由于使用的是SUBDIRS,因此收到创建目标XXXXXX没有规则"的构建失败,表明您没有这样做(至少从src子目录中Makefile的角度来看).为了解决这个问题,您可以在 src/Makefile.am 中尝试以下操作(取自上的GNU Automake邮件列表档案):

The above assumes that you have a rule to build the library already. Since you're using SUBDIRS, you received a "no rule to make target XXXXXX" build failure, which indicates that you don't (at least from the perspective of the Makefile in the src subdirectory). To remedy this, you can try the following in src/Makefile.am (taken from "Re: library dependency" on the GNU Automake mailing list archives):

FORCE:

$(top_builddir)/lib/libYOURLIB.a: FORCE
<TAB>(cd $(top_builddir)/lib && $(MAKE) $(AM_MAKEFLAGS) libYOURLIB.a)

您也可以简单地将lib用作src的子目录,当然也可以使注释更简单.

You can also simply make lib a subdirectory of src as your comment indicated of course and make it simpler.

或者,您可以停止使用递归构建设置,而可以使用更简单的非递归构建设置.请参见 GNU Automake手册§ 7.3:子目录的替代方法非递归Automake 以获得一些信息,但是一般的想法是改变事物以允许:

Alternatively, you can stop using a recursive build setup and use what is perhaps a simpler non-recursive build setup. See GNU Automake Manual §7.3: An Alternative Approach to Subdirectories and Non-recursive Automake for some information on that, but the general idea would be to alter things to allow for :

configure.ac

AM_INIT_AUTOMAKE([-Wall subdir-objects])
...
AC_CONFIG_FILES([Makefile])

Makefile.am

# Instead of using the SUBDIRS variable.
include src/Makefile.am.inc
include lib/Makefile.am.inc
dist_doc_DATA = README

lib/Makefile.am 重命名为 lib/Makefile.am.inc

# Full path relative to the top directory.
lib_LIBRARIES = lib/libYOURLIB.a
lib_libYOURLIB_a_SOURCES = lib/library.cpp

src/Makefile.am 重命名为 src/Makefile.am.inc

# Full path relative to the top directory.
bin_PROGRAMS = bin/program
bin_program_SOURCES = src/main.cpp
bin_program_DEPENDENCIES = lib/libYOURLIB.a
bin_program_LDADD = lib/libYOURLIB.a

重命名文件是可选的(您总是可以使用include src/Makefile.am命名),但这有助于表明它并不意味着是独立的Automake源文件.

Renaming the files is optional (you could always just include src/Makefile.am), but it helps to denote that it isn't meant to be a standalone Automake source file.

此外,假设lib/library.cppsrc/main.cpp都位于#include "library.hpp"中,并且位于另一个目录中,您可能还希望对所有文件使用AM_CPPFLAGS = -I $(top_srcdir)/include或对构建中使用的所有源文件使用obj_program_CPPFLAGS = -I include bin/program,假设library.hppprogram/include中.当另一个项目在其自己的SUBDIRS变量中包含整个program源目录时,我不确定$(top_srcdir)是否正确,但是在以下情况下,$(srcdir)将始终引用顶级program目录非递归的自动制作,使其在将此程序包作为组件的大型项目中可能更有用.

Also, supposing that lib/library.cpp and src/main.cpp both #include "library.hpp", and it's in another directory, you might also want to use AM_CPPFLAGS = -I $(top_srcdir)/include for all files or obj_program_CPPFLAGS = -I include for all source files that are used in building bin/program, assuming library.hpp is in program/include. I'm not sure if $(top_srcdir) is right when another project includes your entire program source directory in its own SUBDIRS variable, but $(srcdir) will always refer to the top-level program directory in the case of a non-recursive automake, making it perhaps more useful in larger projects that include this package as a component.

这篇关于自动制作第三方库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆