简体   繁体   English

使用autotools和替代品的优点

[英]Advantages of using autotools and alternates

I have a moderate C++ project. 我有一个温和的C ++项目。 I'm trying to use autotools for it, but find the complexity overwhelming. 我正在尝试使用autotools,但发现复杂性令人难以招架。

What are guidelines for when to use autotools and when you can do without it? 何时使用autotools以及何时可以不使用autotools的准则是什么? What are (simple) alternatives? 什么是(简单)替代品?

The main reason I want to use autotools is for its full make install support. 我想使用autotools的主要原因是它的完全make install支持。 Is there a simpler alternative? 有更简单的替代方案吗?

Ideally, I'd like something supported by Eclipse CDT. 理想情况下,我喜欢Eclipse CDT支持的东西。

For the make install support, you only need automake . 对于make install支持,您只需要automake And a simple Makefile.am file is quite easy to make: 一个简单的Makefile.am文件很容易制作:

LIBS += -lsome-lib -lsome_other_lib

bin_PROGRAMS = hello

noinst_HEADERS = some.h header.h files.h

hello_SOURCES = hello.c some.c other.c source.c file.c

That's about it. 就是这样。


The autoconf tool is useful if you want to make your program more platform-independent. 如果您希望使程序更加独立于平台,则autoconf工具非常有用。 You write a simple script to test the existence of the system header files and system libraries you use. 您编写了一个简单的脚本来测试您使用的系统头文件和系统库的存在。 If not found you either give an error or use copies provided in your package. 如果没有找到,您要么出错,要么使用包装中提供的副本。

For trivial, self-contained (no dependencies, nothing to be installed), projects with up to 3 source code files, write a Makefile: 对于简单,自包含(无依赖,无需安装),具有最多3个源代码文件的项目,编写一个Makefile:

.PHONY: all clean

all: program

clean:
    rm -f *.o

program: sourceA.o sourceB.o
    $(CXX) -o $@ $^ $(LDFLAGS)

You can define variables for CPPFLAGS, CFLAGS, CXXFLAGS, LDFLAGS, at least GNU make fills in the blanks in the expected way. 您可以为CPPFLAGS,CFLAGS,CXXFLAGS,LDFLAGS定义变量,至少GNU以预期的方式填充空白。

It of course doesn't address the problem of dependency tracking, nor checking for system capabilities. 它当然不能解决依赖性跟踪问题,也不能检查系统功能。 A common problem for non-trivial projects is to interact with the system: making sure certain libraries are usable, and installing/uninstalling. 非平凡项目的一个常见问题是与系统交互:确保某些库可用,以及安装/卸载。 Most tools fail on both. 大多数工具都失败了。

Here's a few examples I have experience with: 以下是我遇到过的几个例子:

Premake Premake

It generates consistent project files for different IDEs (and also Makefiles if you just need to build the code). 它为不同的IDE生成一致的项目文件(如果您只需要构建代码,还可以生成Makefile)。 Doesn't do anything else (no install target, no way to check for availability of libraries). 不做任何其他事情(没有安装目标,无法检查库的可用性)。 Only useful if you need to supply project files for an IDE that you don't use (eg you use Eclipse and somebody else wants to compile it on Visual Studio). 仅在您需要为不使用的IDE提供项目文件时才有用(例如,您使用Eclipse而其他人想在Visual Studio上编译它)。 Anything non-trivial requires LUA scripting. 任何非平凡的事情都需要LUA脚本。

CMAKE cmake的

If all you need is to call the compiler to process your files, it's acceptable. 如果您只需要调用编译器来处理文件,那么这是可以接受的。 For almost anything you'll have to memorize (or copy/paste) sequences of low-level macros (which form a programming language of sorts). 对于几乎任何你必须记住(或复制/粘贴)低级宏序列(形成各种编程语言)的东西。 Finding libraries on the system is convoluted and messy (some libraries were blessed by the creators and have hard-coded tests to find them, so you might be lucky). 在系统上查找库是令人费解和混乱的(一些库被创建者祝福并且有硬编码测试来找到它们,所以你可能很幸运)。 Considering the amount of broken cmake scripts I had to deal with to this date, I think most people just copy/paste the macros around without trying to understand it. 考虑到我在这个日期必须处理的破坏的cmake脚本的数量,我想大多数人只是复制/粘贴宏而不试图理解它。 Can install, but not uninstall. 可以安装,但不能卸载。 With some cmake scripts you might have to run cmake multiple times until it generates the correct output (eg VTK). 使用一些cmake脚本,您可能需要多次运行cmake,直到它生成正确的输出(例如VTK)。

SCons 使用SCons

Seems to be CMAKE and Premake done better: no macros, uses a well-known programming language (Python) to provide a fair amount of useful functionality. 似乎是CMAKE和Premake做得更好:没有宏,使用着名的编程语言(Python)来提供相当多的有用功能。 Still fails in a number of ways; 仍然以多种方式失败; specifying a non-hardcoded installation prefix requires non-trivial effort, because it's not built-in. 指定非硬编码安装前缀需要非常重要的工作,因为它不是内置的。

Autotools 自动工具

Does far more than the tools mentioned above. 远远超过上面提到的工具。 And most of them, conveniently. 而且大部分都很方便。 Has sane defaults; 有理智的默认值; some highlights: 一些亮点:

  • AC_CHECK_LIB(foobar, function_to_check_linking) - this finds a library named foobar, and put it in the $LIBS environment variable. AC_CHECK_LIB(foobar, function_to_check_linking) - 它找到一个名为foobar的库,并将其放在$LIBS环境变量中。 Yes, detecting libraries is an important common task; 是的,检测图书馆是一项重要的共同任务; if your tool doesn't consider this a first-class use-case, ditch it. 如果你的工具不认为这是一流的用例,就抛弃它。 (It's more convenient to use PKG_CHECK_MODULES though.) (尽管使用PKG_CHECK_MODULES更方便。)

  • Every action during ./configure is logged in config.log , so you can actually figure out what went wrong. ./configure每个操作都记录在config.log ,因此您可以实际找出出错的地方。 For most other tools, at best you'll get "Boost_dir-NOTFOUND". 对于大多数其他工具,最多你会得到“Boost_dir-NOTFOUND”。

  • Already comes with built-in make install , make uninstall (what do you mean, your tool can put stuffs on my system but can't remove them?), make check (if you specified test_ programs), make dist-gzip (packages the source files into a tar.gz), make distcheck (creates a tar.gz and makes sure everything builds correctly and all tests pass). 已经内置make installmake uninstall (你的意思是什么,你的工具可以把东西放在我的系统上但不能删除它们?), make check (如果你指定了test_程序), make dist-gzip (包将源文件放入tar.gz), make distcheck (创建tar.gz并确保所有内容都正确构建并且所有测试都通过)。 Even better, it plays nicely with checkinstall so you can create and distribute .rpms and .debs from it. 更好的是,它可以很好地与checkinstall配合使用,因此您可以从中创建和分发.rpms和.debs。

  • You can mix in good old Makefile rules inside automake Makefiles if needed. 如果需要,您可以在automake Makefile中混合使用旧的Makefile规则。

  • Autotools files are tracked as well as source files, so helper scripts and Makefiles are generated again if needed, by simply invoking make . 跟踪Autotools文件以及源文件,因此只需调用make即可在需要时再次生成辅助脚本和Makefile。

Yes, it's "hard" to learn, but no more than learning all the specific macros/functions to call in CMAKE/SCons, in my opinion. 是的,它很难学习,但仅仅是学习在CMAKE / SCons中调用的所有特定宏/函数。 An initial Makefile.am for a project is just a list of files assigned to a variable, and an initial configure.ac can be generated by autoscan ; 项目的初始Makefile.am只是分配给变量的文件列表,初始configure.ac可以由autoscan生成; I find it convenient even for more trivial projects. 我发现即使对于更琐碎的项目也很方便。

The autobook is the best source I know to learn it, but it's unfortunately outdated (autoconf will complain a lot about using deprecated macros). 自动提醒是我所知道的最好的来源,但不幸的是它过时了(autoconf会抱怨使用已弃用的宏)。

You can pick and choose the parts of autotools that you want to use. 您可以选择要使用的自动工具部件。 Many projects use only autoconf , the part of autotools that generates a configure script, and if you just want to generate a Makefile with an install target that is configurable by the end user, then that's probably all you need. 许多项目仅使用autoconf ,即生成configure脚本的autotools的一部分,如果您只想生成具有可由最终用户配置的install目标的Makefile ,那么这可能就是您所需要的。

I don't know if this is too far off answer-wise, but I'd consider scons. 我不知道答案是否过于遥远,但我会考虑scons。 It takes a bit to get going, but does many, many things automatically that has to be hand-teased with make. 它需要一点时间才能开始,但是许多很多东西都是自动的,必须由make手工处理。

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

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