使用 automake 和 autoconf 安装 C++ 程序

Install C++ program with automake and autoconf

我已经用 C++ 编写了一个程序,但现在我必须使用 autoconf 和 automake 安装这个程序。

因此,当我 运行 命令“./configure && make && make install”时,它必须执行以下操作:

我写了这个 configure.ac 脚本:

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.69])
AC_INIT([my_program], [0.1], [my_mail])
AC_CONFIG_SRCDIR([AbsAlgorithm.hpp])
AM_INIT_AUTOMAKE

# Checks for programs.
AC_PROG_CXX
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
AC_CHECK_HEADERS([stdlib.h string.h sys/time.h unistd.h wchar.h wctype.h])

# Checks for typedefs, structures, and compiler characteristics.
AC_CHECK_HEADER_STDBOOL
AC_C_INLINE
AC_TYPE_SIZE_T

# Checks for library functions.
AC_FUNC_MALLOC
AC_FUNC_MKTIME
AC_CHECK_FUNCS([gettimeofday memset mkdir])

LIBS="-ldl"

AC_CONFIG_FILES([Makefile])
AC_OUTPUT

和这个 Makefile.am 脚本:

AUTOMAKE_OPTIONS = foreign

AM_CXXFLAGS=-Wall -std=gnu++11 -DVERSION=\"$(VERSION)\" -DPROG="\"$(PACKAGE)\""

bin_PROGRAMS = algatorc
noinst_LIBRARIES = libalgatorc.a
libalgatorc_a_SOURCES = Timer.cpp
include_HEADERS = Timer.hpp TestSetIterator.hpp TestCase.hpp ETestSet.hpp EParameter.hpp Entity.hpp ParameterSet.hpp AbsAlgorithm.hpp Log.hpp JSON.hpp
algatorc_SOURCES = ParameterSet.cpp TestCase.cpp EParameter.cpp ETestSet.cpp TestSetIterator.cpp Entity.cpp Timer.cpp  main.cpp JSON.cpp JSONValue.cpp

现在,当我 运行“./configure && make && make install”时,我没有在 /opt 中获得名为 my_program 的新文件夹。但是,我现在在 /usr/local/include 中确实有头文件。 /usr/local/lib 中没有 lib 文件。 python 只有一个文件夹。我想要一个名为 my_program 的文件夹,在该文件夹中我想要静态库。

我正在使用 Ubuntu 12.04 x64

如有任何帮助,我将不胜感激。谢谢

Autotools 并不是真正为将东西放在特定位置而设计的。这个想法是程序进入 programs 目录 $(bindir),库进入 libraries 目录 $(libdir) 等,并且安装一切的人可以决定这些位置。

所以你真的应该只关心相对于安装者 运行 安装者想要的地方的安装。

他们通过向 configure 脚本添加参数来做到这一点,例如:

configure --prefix=/opt/myprog

这通常会在 /opt/myprog/bin 中安装程序,在 /opt/myprog/lib 中安装库等...

您可以通过设置特殊的 dir 变量来添加安装的地方。例如,要将库放在 $(libdir)(默认 /usr/local/lib)的子目录中,您可以这样做:

myprog_librarydir = $(libdir)/myprog

对头文件做同样的事情并不少见:

myprog_includedir = $(prefix)/include/myprog

它定义了一些您可以参考的目标文件夹,而不是默认值:

myprog_include_HEADERS = \
    Timer.hpp \
    TestSetIterator.hpp \
    TestCase.hpp \
    ETestSet.hpp \
    EParameter.hpp \
    Entity.hpp \
    ParameterSet.hpp \
    AbsAlgorithm.hpp \
    Log.hpp \
    JSON.hpp

这些现在将安装到 $(prefix)/include/myprog

与对应库类似:

myprog_library_LIBRARIES = libmyprog.a

libmyprog_a_SOURCES = \
    ParameterSet.cpp \
    TestCase.cpp \
    EParameter.cpp \
    ETestSet.cpp \
    TestSetIterator.cpp \
    Entity.cpp \
    Timer.cpp \
     JSON.cpp \
    JSONValue.cpp

所以基本上你创建一个目标(安装)目录使用:

mynamedir = $(prefix)/path/... whatever

这允许您设置 bin_lib_include_ 等以外的目的地...

所以不用说 lib_LIBRARIES 你可以说 myname_LIBRARIES.

希望对您有所帮助。