如何在Makefile.am脚本中指定仅希望编译对象.o文件? [英] How do I specify in a Makefile.am script that I only want to compile object .o files?

查看:115
本文介绍了如何在Makefile.am脚本中指定仅希望编译对象.o文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Makefile.am,它将负责构建最终的应用程序二进制文件:

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

project/src/Makefile.am

src目录中还有一个名为ctrnn的子目录,其中包含一个附加的Makefile.am:

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

project/src/ctrnn/Makefile.am

现在,ctrnn/Makefile.am应该只生成对象.o文件,其想法是顶级Makefile.am应该使用子目录ctrnn中生成的目标文件来构建二进制文件.

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.

这是ctrnn/Makefile.am:

SOURCES = network.cpp\
    neuron.cpp

AM_CPPFLAGS=  @CXXFLAGS@

基于此Makefile.am文件,我希望以network.o和Neuron.o结尾.我正在使用Automake等生成相应的Makefile,但是当我尝试然后执行make文件时,它没有执行任何操作,只是说:

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'

我知道为什么会这样,我需要指定构建目标.但是,如果我不想构建需要bin_PROGRAMS但需要实际目标文件network.oneuron.o的二进制文件,又该如何在ctrnn/Makefile.am脚本中执行此操作?

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?

(请注意,如果我确实指定了bin_PROGRAMS名称,则正确地以抱怨未定义对main的引用告终.)

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

我做错了什么?

推荐答案

在没有显式目标(程序,库)的情况下,Automake无法构建将使用这些对象的对象.原因之一是针对每个目标指定了编译选项.如果有两个目标,例如两个二进制文件使用相同的对象,但具有不同的编译选项,同一对象可能必须编译两次.

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. 请勿使用src/ctrnn/Makefile.am,只需引用src/Makefile.am中的子目录源文件即可:

  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

请注意,这将在与main.o相同的目录中构建network.oneuron.o.如果要在子目录中使用对象,请使用AUTOMAKE_OPTIONS = subdir-objects.

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.

使用便利库.在src/crtnn/Makefile.am中,建立两个对象的库:

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

并在src/Makefile.am中将可执行文件链接到库:

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

当它不被安装时,它被称为便利"(由于noinst_前缀,您可以知道):它仅在构建期间使用.这是一个静态库,因此结果与链接foo时用crtnn/network.ocrtn/neuro.o替换crtnn/libcrtnn.a的结果相同.

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.

使用Libtool便捷库.如果您尚未使用Libtool,则需要进行更多设置.您应该在configure.ac中添加呼叫LT_INIT,然后重新运行autoreconf以安装libtool文件.然后,您可以更新src/crtnn/Makefile.am以创建两个对象的库,如下所示:

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

src/Makefile.am如下:


bin_PROGRAMS = foo
foo_SOURCES = main.c
foo_LDADD = crtnn/libcrtnn.la
SUBDIRS = crtnn

有什么区别?您可能会问,几乎没有.使用Libtool便利库的一个优点是它们可以嵌套:Libtool库可以包含另一个Libtool库(当您具有深层的源代码层次并且要在每个级别构建一个库时,这很方便).如果需要,还可以使用Libtool便捷库来构建共享库. Automake的静态库不能.

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.

这篇关于如何在Makefile.am脚本中指定仅希望编译对象.o文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆