How to configure the source file directory and ins

2019-08-03 23:24发布

I want a project directory like this:

app/
 |
  src/
  Makefile 

All of the .cpp 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?

3条回答
看我几分像从前
2楼-- · 2019-08-03 23:33

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.)

查看更多
一纸荒年 Trace。
3楼-- · 2019-08-03 23:44

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.

查看更多
等我变得足够好
4楼-- · 2019-08-03 23:52

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.

查看更多
登录 后发表回答