如何在 Makefile.am 脚本中指定我只想编译对象 .o 文件?
我有一个 Makefile.am
,它将负责构建最终的应用程序二进制文件:
project/src/Makefile.am
在 src 目录中还有一个名为 ctrnn
的子目录,其中包含一个附加的 Makefile.am
:
project/src/ctrnn/Makefile.am
现在,ctrnn/Makefile.am
应该只生成对象 .o
文件,其想法是顶级 Makefile。 am
应该使用子目录 ctrnn
中生成的目标文件来构建二进制文件。
这是 ctrnn/Makefile.am
:
SOURCES = network.cpp\
neuron.cpp
AM_CPPFLAGS= @CXXFLAGS@
基于此 Makefile.am
文件,我希望最终得到 network.o 和 Neuron.o。我正在使用 Automake 等生成相应的 Makefile,但是当我尝试然后执行 make 文件时,它不会执行任何操作,只是说:
make: Nothing to be done for `all'
我知道这是为什么,我需要指定构建目标。但是,鉴于我不想构建需要 bin_PROGRAMS
但实际目标文件 的二进制文件,我该如何在
和 ctrnn/Makefile.am
脚本中执行此操作network.oneuron.o
?
(请注意,如果我确实指定了 bin_PROGRAMS 名称,它最终会抱怨对 main 的未定义引用)。
我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果没有使用这些对象的显式目标(程序、库),Automake 无法构建对象。原因之一是编译选项是按目标指定的。如果两个目标(例如两个二进制文件)使用相同的对象但具有不同的编译选项,则同一对象可能必须编译两次。
您可以通过三种方法来做您想做的事情,它们都涉及将源文件绑定到某个目标。
不要使用
src/ctrnn/Makefile.am
,只需引用src/Makefile.am
中的子目录源文件:<前>bin_PROGRAMS = foo
foo_SOURCES = main.c crtnn/network.cpp crtnn/neuron.cpp
请注意,这将在与
main.o
相同的目录中构建network.o
和neuron.o
。如果您想要子目录中的对象,请使用AUTOMAKE_OPTIONS = subdir-objects
。使用便利库。在
src/crtnn/Makefile.am
中创建两个对象的库:使用 Libtool 便利库。如果您尚未使用 Libtool,这需要更多设置。您应该在
configure.ac
中添加调用LT_INIT
并重新运行autoreconf
来安装 libtool 文件。然后,您可以更新src/crtnn/Makefile.am
以创建两个对象的库,如下所示: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.
Do not use a
src/ctrnn/Makefile.am
, just make reference to the subdirectory source files from yoursrc/Makefile.am
:Note that this will build
network.o
andneuron.o
in the same directory asmain.o
. If you want objects in subdirectories, useAUTOMAKE_OPTIONS = subdir-objects
.Use a convenience library. In
src/crtnn/Makefile.am
make a library of your two objects:and in
src/Makefile.am
, link your executable to the library: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 replacedcrtnn/libcrtnn.a
bycrtnn/network.o
andcrtn/neuro.o
when linkingfoo
.Use a Libtool convenience library. This requires more setup if you are not using Libtool already. You should add a call
LT_INIT
inconfigure.ac
and rerunautoreconf
to install the libtool files. Then you can updatesrc/crtnn/Makefile.am
to make a library of your two objects as follows:and
src/Makefile.am
as follows: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.
您可以简单地在 project/src/Makefile.am 中指定源文件,而在 ctrnn: 中不包含 Makefile.am:
或者您可以使用 libtool 便利库。在 ctrnn/Makefile.am 中,放置:
在 src/Makefile.am 中,放置
如果您尚未使用 libtool,则还需要将 LT_INIT 添加到 configure.ac。
You could simply specify source files in project/src/Makefile.am and not have a Makefile.am in ctrnn:
or you can use a libtool convenience library. In ctrnn/Makefile.am, put:
and in src/Makefile.am, put
If you aren't already using libtool, you'll also need to add LT_INIT to configure.ac.
更好的是,您可以通过执行以下操作来强制使用无源目标进行 make:
秘密肉汁是
nodist_EXTRA_xxxx_la_SOURCES
。更多详细信息请参见:https://www.gnu。 org/software/automake/manual/html_node/Libtool-Convenience-Libraries.html
Better yet, you could FORCE a make using a source-less target by doing this:
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