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
?
Usually, you just add
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.)
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.
Typically for executable files you'll have something like:
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:
where foo_SOURCES is as above.
For libraries it's just:
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:
will make the lib install (when make install is called) in /usr/lib
or
will make the lib install in /usr/local/lib.