<?xml version="1.0" encoding="UTF-8" ?>

<project name="winplay" default="build">

  <target name="build">
    <echo message="${bitoption}" />
    <mkdir dir="build" />
    <mkdir dir="build/obj" />
    <cc name="g++" objdir="build/obj" debug="${debug}">
      <fileset dir="src" includes="*.cpp" />
      <compiler name="g++">
        <compilerarg value="-std=c++11" />
        <compilerarg value="-m32" />
      </compiler>
    </cc>

    <condition property="debugoption" value="-g -O0" else="-O2">
      <isset property="debug" />
    </condition>
    <fileset dir="build/obj" id="objects">
      <include name="*.o" />
    </fileset>
    <pathconvert pathsep=" " property="objectslinker" refid="objects" />

    <!-- Due to a bug in GppLinker.java in cpptasks.jar, we must exec g++ because GppLinker erroneously uses gcc, which breaks exception handling. -->
    <exec command="g++ -std=c++11 -m32 -mwindows ${debugoption} -o build/winplay ${objectslinker}" failonerror="true" />
  </target>

  <target name="build-32" depends="build">
    <property name="bitoption" value="-m32" />
  </target>

  <target name="build-64" depends="build">
    <property name="bitoption" value="-m64" />
  </target>


  <target name="clean">
    <delete dir="build" />
  </target>
</project>


我试图使$ {bitoption}为-m32或-m64,这取决于我们是否调用了目标build-32或build-64。然后,我将$ {bitoption}传递给编译器和链接器args,以便生成适当的exe。我做了一个简单的回声测试,以查看是否正确设置了$ {bitoption},它似乎无法正常工作。我得到的是:

Buildfile: E:\_dev\windows\MinGW\msys\home\windoze\projects\Winplay\build.xml
build:
     [echo] ${bitoption}
       [cc] 1 total files to be compiled.
     [exec] The command attribute is deprecated.
     [exec] Please use the executable attribute and nested arg elements.
build-64:
BUILD SUCCESSFUL
Total time: 1 second

最佳答案

构建目标始终是第一个运行的,因为它是一个依赖项,因此$ {bitoption}尚无任何价值。

您可以改为:

<target name="build-32" depends="setup-32, build" />
<target name="build-64" depends="setup-64, build" />

<target name="setup-32">
    <property name="bitoption" value="-m32" />
</target>

<target name="setup-64">
    <property name="bitoption" value="-m64" />
</target>

关于c++ - 为什么这个 Ant 脚本不起作用?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22888667/

10-09 05:14