views:

506

answers:

4

I am new to Automake and I am attempting to compile without linking. My goal is to generate a simple Makefile as shown below using Automake.

CFLAG = -Wall

build: Thread.o

Thread.o: Thread.cc Thread.h
    g++ $(CFLAG) -c Thread.cc

clean:
    rm -f *.o

My attempt so far has brought me to the following Makefile.ac.

noinst_PROGRAMS = thread

thread_SOURCES = Thread.cc

EXTRA_DIST= Thread.h

How can I simulate my original Makefile?

A: 

One way is to do this is to fool Automake by providing link command that does not link:

thread_LINK = true

Other than that, I wouldn't be suprised if Automake did not have such feature.

Laurynas Biveinis
This was a simple solution to the problem. Thanks for the advice.
+2  A: 

For your example, you can just ask Automake to build your .o file directly, e.g.:

$ make Thread.o

I believe this is an implicit rule, so you won't see it in the output Makefile.

In general, Automake generates variables containing all the objects required for each executable or library target. It's pretty straightforward to use them in your Makefile, since it just generates their names by appending _OBJECTS to the target name. You could make your own target in Makefile.am like this:

build-thread: $(thread_OBJECTS)

Then you could build just Thread.o (and any other objects needed for thread) like this:

$ make build-thread

Or if you had multiple targets foo, bar, and baz, you could make your compile-only target in Makefile.am like this:

build: $(foo_OBJECTS) $(bar_OBJECTS) $(baz_OBJECTS)

The only pain here is that you'll need to maintain this list yourself based on the targets in your Makefile.am. You can invoke it at the command line like this:

$ make build
tgamblin
+2  A: 

Automake is not designed to produce object. It will build either programs or libraries. It's hard to answer your question without knowing why you'd want to compile a single object file and not something else. Maybe there is a cleaner answer to your "real" problem.

A Makefile.am you could write is

noinst_LIBRARIES = libThread.a
libThread_a_SOURCES = Thread.cc Thread.h   # No need to put headers in EXTRA_DIST

The resulting Makefile would build a library libThread.a containing only libThread.o, ans because *.a libraries are just a collection of object files there is no linking involved. The above Makefile.am also causes the emitted Makefile to contain rules to compile libThread.o, so you can add a build: rule if you like.

If you really want Automake to emit this compile rule, but not build the library, you could go with

EXTRA_LIBRARIES = libThread.a  # EXTRA here means "output build rules but don't
                               # build unless something depends on it".
libThread_a_SOURCES = Thread.cc Thread.h
build: Thread.$(OBJEXT)

Now you are explicitely requiring the file Thread.$(OBJEXT) to be built only when you type make build, as in your original Makefile.

(Automake uses .$(OBJEXT) rather than .o to support extensions like .obj in DOS variants.)

adl
A: 

First off, automake is a tool to auto make making Makefiles; make in and of itself is a whole different beast (and I'm pretty sure that what you were looking for was a make solution).

Here's the easiest GNU based Makefile to accomplish what you want:

all: Thread.o

This fills in something (by default) like the following (please change 4-space whitespace to hard tabs):

all: Thread.o

Thread.o: Thread.cc
     $(COMPILE.cpp) $(OUTPUT_OPTION) $<

The COMPILE.cpp and OUTPUT_OPTION macros of course expand by default to GNU make specified values and aren't portable; $< is AT&T Make standard syntax though according to pmake(1)'s manpage though.

GNU make has a concept of implicit vs explicit rules, patterns, suffixes, etc that you could use, but that's not portable to all versions of make, and hence that's why all of the Makefile is plainly spelled out in terms of targets and variables as POSIX doesn't describe many of the desired scenarios for how one should write a Makefile.

Run gmake -p for more details and take a look at the texinfo manual for gmake in the topic of implicit, explicit rules, patterns, suffixes, etc.

yaneurabeya