简体   繁体   English

使用自动工具为共享库正确安装config.h

[英]Correct installation of config.h for shared library using autotools

I am converting a C++ program which uses the autotools build system to use a shared library, introducing the use of libtool. 我正在转换一个使用自动工具构建系统的C ++程序来使用共享库,并介绍了libtool的用法。 Most of the program functionality is being placed in the shared library, which is loaded by the main program, so that the common code can be accessed by other programs in future. 程序的大多数功能都放置在共享库中,该共享库由主程序加载,以便将来其他程序可以访问公共代码。

Throughout the program and library sources the autoheader generated config.h is used with the usual macro: 在整个程序和库源代码中,自动标头生成的config.h与通常的宏一起使用:

#if HAVE_CONFIG_H
# include <config.h>
#endif

In configure.ac I use the macro to generate it: 在configure.ac中,我使用宏来生成它:

AC_CONFIG_HEADERS([config.h])

My question is, do I need to install config.h for others to be able to use my library, and if so, what is the appropriate way to do it, and should it be renamed to avoid conflicts etc? 我的问题是,我是否需要安装config.h以使其他人能够使用我的库?如果是,执行该操作的适当方法是什么?是否应将其重命名以避免冲突等?

The most information I have found on this is here: 我在此找到的最多信息是:

http://www.openismus.com/documents/linux/building_libraries/building_libraries#installingheaders http://www.openismus.com/documents/linux/building_libraries/building_libraries#installingheaders

But this is hardly an official source. 但这几乎不是官方消息。

Never ever install autoheader's config.h . 永远不要安装和autoheader的config.h

The last thing the users of your library need is interference from the macros leaking out of your config.h . 库用户需要的最后一件事是宏从config.h泄漏出来的干扰。 Your library may have HAVE_FOOBAR , but my software might be compiled in a way that foobar is disabled, so that HAVE_FOOBAR will break my compilation. 您的库可能具有HAVE_FOOBAR ,但是我的软件可能以禁用foob​​ar的方式进行编译,因此HAVE_FOOBAR将破坏我的编译。

The AX_PREFIX_CONFIG macro from the archive is a workaround, where everything gets prefixed. 存档中AX_PREFIX_CONFIG宏是一种变通方法,其中所有内容都添加了前缀。

A better approach is to create a template file (eg blargconfig.h.in ) with lines like: 更好的方法是使用以下行创建模板文件(例如blargconfig.h.in ):

typedef @BLARG_TYPE@ blarg_int_t;

@BLARG_RANDOM_INCLUDE@

And then AC_SUBST() those variables in configure.ac : 然后AC_SUBST()configure.ac那些变量:

AC_SUBST(BLARG_TYPE, ["unsigned short"])
AC_SUBST(BLARG_RANDOM_INCLUDE, ["#include <somerandomheader.h>"])

Then list it as an output file: 然后将其列为输出文件:

AC_CONFIG_FILES([Makefile
                 src/Makefile
                 ...
                 include/blargconfig.h])

The .h file should be listed with nodist_include_HEADERS ; .h文件应使用nodist_include_HEADERS列出; the .h.in file will be automatically distributed because it's listed in AC_CONFIG_FILES . .h.in文件将自动分发,因为它已在AC_CONFIG_FILES列出。

Destination for such files is commonly $libdir/packagename/include . 此类文件的目标位置通常是$libdir/packagename/include See GLib for example , although they generate glibconfig.h without a template (by writing the whole creation code inline in configure.ac , as the autobook suggests ). 例如 ,请参阅GLib ,尽管它们会生成没有模板的glibconfig.h (通过自动将整个创建代码内嵌在configure.ac ,如自动书中所建议的那样 )。 I find this approach less maintainable than using AC_SUBST , but it's more flexible. 与使用AC_SUBST ,我发现这种方法难以维护,但是更灵活。

Of course, to help the compiler find the platform-dependent header you'll probably also want to write a pkgconfig script, like GLib does. 当然,为了帮助编译器找到依赖于平台的标头,您可能还希望像GLib一样编写pkgconfig脚本。

You will need to install config.h if it affects the interface . 如果它影响界面,则需要安装config.h In practical terms, if the #define 's are required by the header(s), not just the .cc implementation / compilation units. 实际上,如果标头需要#define ,则不仅需要.cc实现/编译单元。

If config.h is a problem, you can specify another name in the AC_CONFIG_HEADERS macro. 如果config.h有问题,则可以在AC_CONFIG_HEADERS宏中指定另一个名称。 eg, AC_CONFIG_HEADERS([foo_config.h]) . 例如AC_CONFIG_HEADERS([foo_config.h])

The easiest way to install the header, assuming automake , is with: 假设automake ,安装标头的最简单方法是:

nodist_include_HEADERS = foo_config.h

in the top-level Makefile.am . 在顶层Makefile.am the nodist prefix tells automake that foo_config.h is generated rather than distributed with the package. nodist前缀告诉automake生成了foo_config.h而不是与软件包一起分发。

If not using automake, install foo_config.h in $includedir . 如果不使用automake,请在$includedir安装foo_config.h $exec_prefix/include is arguably a more correct location for a generated header, but in practice the former location is fine. 对于生成的标头, $exec_prefix/include可以说是一个更正确的位置,但实际上,前一个位置很好。


I avoid using config.h , by passing relevant definitions in CPPFLAGS or foo_CPPFLAGS along with AC_SUBST to Makefile.am for source builds, or AC_SUBST to foo.h.in to generate headers at configure-time. 我避免使用config.h ,通过将CPPFLAGSfoo_CPPFLAGS相关定义与foo_CPPFLAGS一起AC_SUBSTMakefile.am进行源代码构建,或者将AC_SUBSTfoo.h.in来在配置时生成标头。 A lot of config.h is test-generated noise. 很多config.h是测试生成的噪声。 It requires more infrastructure, but it's what I prefer. 它需要更多基础架构,但这是我的首选。 I wouldn't recommend this approach unless you're comfortable with the autotools. 除非您对自动工具感到满意,否则我不建议您使用这种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM