How do I specify in a Makefile.am script that I only want to compile object .o files?

不问归期 提交于 2019-12-03 17:44:21

问题


I have a Makefile.am which will be responsible for building a final application binary:

project/src/Makefile.am

Also in the src directory is a sub-directory called ctrnn which contains an additional Makefile.am:

project/src/ctrnn/Makefile.am

Now, ctrnn/Makefile.am should only generate object .o files with the idea being that the top-level Makefile.am should use the object files generated in subdirectory ctrnn to build the binary.

This is the ctrnn/Makefile.am:

SOURCES = network.cpp\
    neuron.cpp

AM_CPPFLAGS=  @CXXFLAGS@

Based on this Makefile.am file, I want to end up with network.o and neuron.o. I am generating the according Makefile using Automake etc, yet when I try and then execute the make file, it doesn't do anything and just says:

make: Nothing to be done for `all'

I know why this is, I need to specify the build target. But how do I do this in the ctrnn/Makefile.am script given that I don't want to build a binary which would require bin_PROGRAMS but actual object files network.o and neuron.o?

(Note if I do specify a bin_PROGRAMS name, it rightly ends up complaining of an undefined reference to main).

What am I do wrong?


回答1:


Automake cannot build objects without an explicit target (program, library) that will use these objects. One reason is that compilation options are specified per-target. If two targets, e.g. two binaries use the same object but have different compilation option, the same object may have to be compiled twice.

You have three ways to do what you want, they all involve tying your source files to some target.

  1. Do not use a src/ctrnn/Makefile.am, just make reference to the subdirectory source files from your src/Makefile.am:

    bin_PROGRAMS = foo
    foo_SOURCES = main.c crtnn/network.cpp crtnn/neuron.cpp
    
    Note that this will build network.o and neuron.o in the same directory as main.o. If you want objects in subdirectories, use AUTOMAKE_OPTIONS = subdir-objects.
  2. Use a convenience library. In src/crtnn/Makefile.am make a library of your two objects:

    noinst_LIBRARIES = libcrtnn.a
    libcrtnn_a_SOURCES = network.cpp neuron.cpp
    
    and in src/Makefile.am, link your executable to the library:
    bin_PROGRAMS = foo
    foo_SOURCES = main.c
    foo_LDADD = crtnn/libcrtnn.a
    SUBDIRS = crtnn
    
    It's called "convenience" when it is not going to be installed (you can tell because of the noinst_ prefix): it is just used during the build. And this is a static library, so the result is the same as if you had replaced crtnn/libcrtnn.a by crtnn/network.o and crtn/neuro.o when linking foo.
  3. Use a Libtool convenience library. This requires more setup if you are not using Libtool already. You should add a call LT_INIT in configure.ac and rerun autoreconf to install the libtool files. Then you can update src/crtnn/Makefile.am to make a library of your two objects as follows:

    noinst_LTLIBRARIES = libcrtnn.la
    libcrtnn_la_SOURCES = network.cpp neuron.cpp
    
    and src/Makefile.am as follows:
    bin_PROGRAMS = foo
    foo_SOURCES = main.c
    foo_LDADD = crtnn/libcrtnn.la
    SUBDIRS = crtnn
    
    What is the difference? you may ask, almost none. One advantage of using Libtool convenience libraries is that they can be nested: a Libtool library can include another Libtool library (this is convenient when you have a deep hierarchy of source code and you are building a library at each level). Also Libtool convenience libraries can be used to build a shared library if you want. Automake's static libraries cannot.



回答2:


You could simply specify source files in project/src/Makefile.am and not have a Makefile.am in ctrnn:

maude_SOURCES = ctrnn/network.cpp ctrnn/neuron.cpp

or you can use a libtool convenience library. In ctrnn/Makefile.am, put:

noinst_LTLIBRARIES = libctrnn.la
libctrnn_la_SOURCES = network.cpp neuron.cpp

and in src/Makefile.am, put

LDADD = ctrnn/libmylib.la

If you aren't already using libtool, you'll also need to add LT_INIT to configure.ac.




回答3:


Better yet, you could FORCE a make using a source-less target by doing this:

SUBDIRS = sub1 sub2 …
lib_LTLIBRARIES = libtop.la
libtop_la_SOURCES =
# Dummy C++ source to cause C++ linking.
nodist_EXTRA_libtop_la_SOURCES = dummy.cxx
libtop_la_LIBADD = \
  sub1/libsub1.la \
  sub2/libsub2.la \
...

The secret gravy is nodist_EXTRA_xxxx_la_SOURCES.

More details in here: https://www.gnu.org/software/automake/manual/html_node/Libtool-Convenience-Libraries.html



来源:https://stackoverflow.com/questions/2272697/how-do-i-specify-in-a-makefile-am-script-that-i-only-want-to-compile-object-o-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!