How to configure the source file directory and install location using GNU Autotools
I want a project directory like this:
app/ | src/ Makefile
All of the .cpp开发者_C百科 and .hpp, .h files are in the src
subdirectory. How can I configure Makefile.am
or configure.ac
to let the generated Makefile using the directory /src
as the source directory? And how to let the generated executable into a specified directory like /usr/opt
?
Another question is that If I want to make my program as a .so library, how to achieve my goal to make it install into /usr/lib
or /usr/local/lib
? And how to make the header files into like /usr/local/include/app
?
Usually, you just add
SUBDIRS = src
to the toplevel Makefile.am, and mention src/Makefile in AC_CONFIG_FILES. There are some reasonable complaints about recursive make, but if all of your source is in src then the single level of recursion in make is not really an issue. Putting the executable in /usr/opt is done by the user when setting prefix at configure time, and should not be a consideration when building the package. To build an executable, use bin_PROGRAMS in the Makefile.am, and to build a library you should use either lib_LIBRARIES or lib_LTLIBRARIES (personally, I tend to use libtool and go with the latter.)
I think your questions about installing in /usr/lib or in /usr/opt indicate a fundamental misunderstanding. When building the package, you absolutely do not care about that. As the package maintainer, you indicate that the program should be installed in $(prefix)/bin (or $exec_prefix/bin, or $bindir) and that libraries should be in $libdir, but the user determines the location of $prefix and $libdir and $bindir when they run configure. (Usually, people just set prefix, and bindir defaults to $prefix/bin while libdir defaults to $prefix/lib.)
Typically for executable files you'll have something like:
bin_PROGRAMS = foo
foo_SOURCES = $(top_srcdir)/src/foo_main.c \
$(top_srcdir)/src/foo_main.h \
$(top_srcdir)/src/something_else.c
This will install in the prefix directory's /bin directory, though which isn't what you want, but it's kind of what you users will expect.
To do what you want you'll need to do this:
myexedir = $(prefix)/opt
myexe_PROGRAMS = foo
foo_SOURCES = ...
where foo_SOURCES is as above.
For libraries it's just:
lib_LTLIBRARIES = libfoo.la
libfoo_la_SOURCES = ...
and it will install in the prefix directory's /lib directory. Setting up where the lib installs isn't up to you (directly) but whoever invokes configure:
./configure --prefix=/usr
will make the lib install (when make install is called) in /usr/lib
./configure
or
./configure --prefix=/usr/local
will make the lib install in /usr/local/lib.
Put the autoconf and automake files configure.ac and Makefile.am in the "src" directory and write in your documentation that the "./configure" command should be run there instead of in the top level directory? Otherwise you can not really avoid having at least some of the autoconf/automake files at the top level.
精彩评论