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.

Vitor Baptista
  • 2,016
  • 1
  • 23
  • 28
  • 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 '10 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 03 '14 at 16:17
  • http://stackoverflow.com/questions/1015700/autotools-library-and-object-file-output-control – Ciro Santilli OurBigBook.com Feb 20 '16 at 20:45

3 Answers3

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.

Braden
  • 1,448
  • 10
  • 11
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.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • 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? – Vitor Baptista Jan 28 '10 at 02: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 '10 at 08: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 : http://sources.redhat.com/automake/automake.html#Use-Cases – adl Jan 28 '10 at 10:13
  • 4
    And after all, `make install` will probably put them in a place like `$(prefix)/bin` anyway ... – Benjamin Bannier Feb 05 '10 at 08: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...

Niels Lohmann
  • 2,054
  • 1
  • 24
  • 49
  • 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... – Vitor Baptista Feb 10 '10 at 02: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 05 '13 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. – Niels Lohmann Dec 05 '13 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 '14 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. – Franklin Yu Aug 27 '18 at 17:35