29

I searched for the answer to this question but couldn't find any good. Maybe they're old and something has changed, so I ask again.

I have a directory structure as:

my_project

  • src

  • bin

I want that, when I do make in the root dir, the binaries are put in ./bin, instead of cluttering ./src. But how?

EDIT: I am using C++. My Makefile.am has nothing special. Just the bin_PROGRAM and _SOURCES variables.

When I run make, the binaries generated are put into ./src. I simply want them in ./bin.

3
  • post what you have done (your makefile), clarify your directory structure above, then it will be much easier to help (also, the programming language you are using could be relevant)
    – KevinDTimm
    Jan 26, 2010 at 14:43
  • Are you using the full Autotools stack? If so, as @Braden mentioned, just use the configure shell script generated by Autoconf.
    – Plamen
    Sep 3, 2014 at 16:17

3 Answers 3

37

You've got the wrong idea here.

Your build tree is wherever you run configure. That's how autoconf is designed to work. Users of your package (who do not want to clutter their source tree) will expect it to work this way.

This approach is a more general solution with a lot more flexibility than the organization you're imagining. For instance, it's not terribly unusual to want to maintain sources and build files on separate filesystems.

0
28

Automake doesn't cope very well if you try to set up your directories in a different way than it expects. What you want would involve writing extra rules to move the binaries to ../bin after compiling them, which is needlessly complicated.

If you don't want to clutter your source directory, try this:

cd my_project
mkdir build
cd build
../configure
make

That will put all the generated files (like makefiles, binaries, object files) in subdirectories of my_project/build.

4
  • It works, but is a bit clusmy. I could do it, but what if others use my software? They'll have their ./src cluttered. Maybe I could simply add these commands to Makefile's all rule? Jan 28, 2010 at 2:48
  • 1
    configure is what generates the makefiles, so you can't put that command in the makefile. I can safely say that most people don't care about having their ./src cluttered, because Automake always works that way. This is the standard solution for those who do care. And what if they prefer having the binaries in the same directory? If you still want to set it up your way, I don't think there's any way to do that without giving up Automake (although you can still use Autoconf.)
    – ptomato
    Jan 28, 2010 at 8:53
  • 2
    Plus, if other people use your software and see that it uses autoconf/automake, they won't expected it to build stuff in a separate directory (unless they arrange for it by running configure elsewere as ptomato has shown). It's just the philosophy of the GNU Build System, and Autoconf/Automake are tools to implement the GNU Build System and not something else. You may want to look at the introductory chapter of Automake for user typical use cases of the GNU Build System : sources.redhat.com/automake/automake.html#Use-Cases
    – adl
    Jan 28, 2010 at 10:13
  • 4
    And after all, make install will probably put them in a place like $(prefix)/bin anyway ... Feb 5, 2010 at 8:40
18

One way to tell Automake to create binaries in a certain directory is to add this directory right to the name in the "bin_PROGRAMS" variable.

Consider the following src/Makefile.am:

bin_PROGRAMS = foo
foo_SOURCES = ...
foo_CPPFLAGS = ...
foo_LDFLAGS = ...

It creates a binary "src/foo", but you can tell Automake to use the sources in src to create a binary "bin/foo":

bin_PROGRAMS = $(top_builddir)/bin/foo
__top_builddir__bin_foo_SOURCES = ...
__top_builddir__bin_foo_CPPFLAGS = ...
__top_builddir__bin_foo_LDFLAGS = ...

I tried it with some packages and even "make distcheck" swallows it. Can't be that much of a hack though...

5
  • It's the best solution, I think. Not perfect, because all the .o and files created by the compilation process are still in src, but... Feb 10, 2010 at 2:13
  • 2
    Is there anyway to move everything build related to a subfolder such as build/ (including object files, generated text files, .desktop files, etc.), similar to what I common in the Java world with for example Maven.
    – lanoxx
    Dec 5, 2013 at 10:01
  • 2
    You could try to execute the configure script from another folder; for instance, you could create a "build" subfolder, execute "cd build; ../configure ; make". This will leave everything outside "build" untouched and generate all files inside "build", including objects and binaries. Dec 5, 2013 at 13:59
  • 1
    @NielsLohmann good answer, some projects (LLVM iirc) expect users to call the configure script from a build directory, other packages say that it's possible, but unsupported, and most don't mention it. I admire the people who have to tend their project's build systems...
    – MauganRa
    Feb 11, 2014 at 22:58
  • @MauganRa If out-of-source build is unsupported in a project, then the project is evil and better off without Autotools. The project authors don't know what is Autotools. Aug 27, 2018 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.