views:

234

answers:

3

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  A: 

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
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?
Vítor Baptista
`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
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
And after all, `make install` will probably put them in a place like `$(prefix)/bin` anyway ...
honk
+1  A: 

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
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...
Vítor Baptista
+3  A: 

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
Ok, you all convinced me. Thanks :)
Vítor Baptista