将对象文件放在非递归构建中的不同目录中 [英] placing object files in different directories in nonrecursive build

查看:90
本文介绍了将对象文件放在非递归构建中的不同目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景

我们为Matlab和Octave都构建了.mex文件.源代码对于Matlab和Octave都是通用的,但是在这里和那里进行了一些细微的修改(请注意代码中的宏条件).供参考,源结构为:

We build .mex files for both Matlab and Octave. The source code is common to both Matlab and Octave but with slight modifications here and there (taken care of via macro conditionals in the code). For reference, the source structure is:

                      mex/
                       |
              ------------------------------
              |                            |
           sources/                      build/
              |                            |
        -----------            -------------------------------
        |         |            |          |        |         |
       mex1/ ... mexN/       octave/  mex1.am ... mexN.am  matlab/
        |                      |                             |
    -----------           -------------                -------------
    |         |           |           |                |           |
file1.cc ... fileM.cc  configure.ac  Makefile.am   configure.ac  Makefile.am

.mex源文件位于mex/sources/mex*/中,我们编译了多个.mex文件.

The .mex source files are located in mex/sources/mex*/ and we compile multiple .mex files.

Matlab构建系统位于mex/build/matlab/中,我们在其中运行'./configure'来找到编译Matlab .mex文件所需链接的头文件/库. /mex/build/octave/中存在类似的configure.acMakefile.am文件,已对其进行了修改以查找Octave标头和库.

The Matlab build system is located in mex/build/matlab/, where we run './configure' to locate the headers/libraries needed to link against for the compilation of Matlab .mex files. Analogous configure.ac and Makefile.am files exist in /mex/build/octave/, modified to find the Octave headers and libraries.

Matlab和Octave共有的automake构建指令(源文件的路径,要构建的.mex文件的路径)存在于名为/mex/build/mex*.am的文件中,并且通常如下所示:

The automake build instructions (the paths to the source files, the .mex files to be built) common to both Matlab and Octave exist in the files named /mex/build/mex*.am and typically look like:

mexI_PROGRAMS += mexI

nodist_mexI_SOURCES = \
    ../../sources/mexI/mexI.c \
    ../../sources/mexI/mexI_file1.c \
    ../../sources/mexI/mexI_fileN.c

递归make在我们的设置中效果很好(我们曾经有名为/mex/build/matlab/mex*//mex/build/octave/mex*/的目录包含Makefile.am文件;目标文件将在此处编译,而输出文件将在此处).现在,鉴于GNU subdir-objects所做的更改,我们正在转向非递归构建.从Matlab构建开始,我能够对其进行修改以使其以非递归方式工作,而不会出现任何问题.

Recursive make worked perfectly for our setup (we used to have directories named /mex/build/matlab/mex*/ and /mex/build/octave/mex*/ that contained Makefile.am files; object files would be compiled there and the output files would be located there). Now we are moving to a non-recursive build given the changes that GNU subdir-objects change. Starting with the Matlab build, I was able to modify it to work non-recursively without any problems.

问题

在转向非递归构建时,目标文件现在与其源文件 /mex/sources/mex*/在相同的目录中生成.因为我已经编译了Matlab .mex文件,所以*.o文件存在于这些源目录中.因此,当我编译八度文件时,make直接跳到链接步骤,因为它已经在目录中看到了最新的*.o文件.当然,问题在于这些*.o文件是使用Matlab标头编译的,因此无法在Octave版本中使用.

In moving to the non-recursive build, object files are now produced in the same directories as their source files, i.e., /mex/sources/mex*/. As I have already compiled the Matlab .mex files, the *.o files exist in these source directories. So, when I go compile the Octave files, make skips directly to the linking step as it already sees the recent *.o files in the directory. The problem is, of course, that these *.o files were compiled with Matlab headers and hence will not work in an Octave build.

最简单的解决方案是简单地定义一个目录,在该目录中将放置目标文件.当前,一个典型的构建编译命令如下所示(删除标志):

The easiest solution would be to simply define a directory in which the object file would be placed. Currently, a typical build compilation command looks like (removing flags):

gcc ... -o ../../sources/mex1/mex1.o ../../sources/mex1/mex1.c

我只想更改传递给-o标志的文件的位置,但是看不到如何在automake中执行此操作.当然,除非有一个更优雅的方法解决方案.

I would just like to change the location of the file passed to the -o flag, but don't see how to do this in automake. Unless, of course, there's a more elegant solution.

推荐答案

我只想更改传递给-o标志的文件的位置,但是看不到如何在automake中执行此操作.当然,除非有更优雅的解决方案.

I would just like to change the location of the file passed to the -o flag, but don't see how to do this in automake. Unless, of course, there's a more elegant solution.

首先,我观察到已编译的目标文件被放置在源文件旁边,因为这就是您所说的,方法是为Automake提供subdir-objects选项并执行源内构建(或实际上两个).将它们放置在其他位置有多种选择,但其中有两种脱颖而出,因为它们与问题的原因直接相关:

I observe in the first place that the compiled object files are being placed next to the source files because that's what you said to do by giving Automake the subdir-objects option and performing an in-source build (or in fact two). There are several options for having them placed somewhere else, but two of them stand out, as they are tied directly to the reasons for the problem:

  1. 删除subdir-objects Automake选项.然后,您的中间.o文件应最终位于运行make的构建目录中.这有点杂乱,但是不必担心,因为生成的Makefile知道它们是哪些文件,因此可以在必要时将它们与可安装产品区分开.这里的主要问题是,与为同一目标构建的不同.mex文件相关联的目标文件之间可能会发生冲突.

  1. Remove the subdir-objects Automake option. Your intermediate .o files should then end up in the build directory from which you run make. This is a little messy, but that should not be a concern, as the generated Makefile knows which files they are, and can therefore distinguish them from the installable products where necessary. The main issue here is that you could run into trouble with collisions among the object files associated with different .mex files built for the same target.

执行源外构建而不是源内构建,每个主要目标(八度/matlab)一个,每个都在自己的构建树中.与您可能想到的相反,这不是您现在正在做的,至少从Autotools的角度来看不是.当您执行源代码外构建时,所有生成的文件都将被写入构建树,因此为octave和matlab设置单独的构建树应该可以彻底解决您的问题.这是我推荐的选项.

Perform out-of-source instead of in-source builds, one for each main target (octave / matlab), each in its own build tree. Contrary to what you may think, that's not what you're doing now, at least not from the Autotools' perspective. When you perform an out-of-source build, all generated files are written to the build tree, so having separate build trees for octave and matlab should resolve your issue cleanly. This is the option I recommend.

Autotools自动支持源外构建.您需要做的就是创建所需的构建目录,创建您的工作目录,然后从此处运行项目的configure脚本.如果需要,构建目录可以是源根目录的子目录,尽管不一定要这样.要理解的关键是顶级源目录"是包含configure脚本的目录,而顶级构建目录"是您从中运行该脚本的工作目录的目录.因此,如果您在项目根目录中,则可以执行以下操作:

The Autotools provide support for out-of-source building automatically. All you need to do is create the desired build directory, make that your working directory, and then run the project's configure script from there. The build directory can be a subdirectory of the source root if you wish, though it doesn't have to be. The key thing to understand is the the "top source directory" is the one containing the configure script, and the "top build directory" is the one that was the working directory from which you ran that script. Thus, if you were in the project root, you might do this:

$ cd build
$ mkdir build-octave build-matlab

$ cd build-octave
$ ../octave/configure
$ make
$ make install

$ cd ../build-matlab
$ ../matlab/configure
$ make
$ make install

但是可以通过假设在源代码内构建来破坏支持的方式编写Makefile.am文件.您呈现的Makefile.am摘录正是通过依赖于从构建目录到源代码的特定相对路径来做到这一点的.如果您的Makefile.am要引用相对于顶部源目录的源文件(而不是构建文件),则可以使用Automake提供的自动$(top_srcdir)变量. (而且,这也不会干扰源代码内的构建.)例如,您应该能够做到这一点:

But it is possible to write your Makefile.am files in ways that break that support by assuming in-source building. The Makefile.am excerpt you present does precisely that by depending on a specific relative path from the build directory to the sources. If your Makefile.am wants to refer to a source file (as opposed to a built file) relative to the top source directory, then it can use the automatic $(top_srcdir) variable provided by Automake. (And that will not interfere with in-source building, either.) For example, you ought to be able to do this:

nodist_mexI_SOURCES = \
    $(top_srcdir)/../../sources/mexI/mexI.c \
    $(top_srcdir)/../../sources/mexI/mexI_file1.c \
    $(top_srcdir)/../../sources/mexI/mexI_fileN.c

我不太喜欢引用资源上方的顶级目录,但它应该可以工作.

I don't so much like referring to resources above the top source directory, but it ought to work.

更新:

还考虑将自动工具修改为针对两种目标类型仅具有一个构建系统. Autoconf支持定义自己的--enable--with选项,您可以在运行configure时使用它们来选择目标类型. Autoconf和Automake支持各种条件,可以使用这些条件和其他条件来调整构建的细节.与单独维护并行构建系统相比,这可能是一个巨大的胜利.这些都可以与源外构建很好地协同工作.

Consider also modifying the Autotooling to have only one build system for both target types. Autoconf supports defining your own --enable and --with options, which you can use to select the target type at the time you run configure. Autoconf and Automake support conditionals of various kinds that can use these and other criteria to adjust the details of the build. This could be a big win over maintaining parallel build systems separately. This could all work together very nicely with out-of-source building.

这篇关于将对象文件放在非递归构建中的不同目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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