Problem: failed to create task or type cc

Help
sharadb
2007-10-25
2013-04-24
  • sharadb
    sharadb
    2007-10-25

    Hi,

    I am to Ant. We would like to use Ant for building C++ apps.
    I first tried a simple example with java and it worked fine, but when I try to execute a 'hello world' program in C++ I get an error.

    Here is what I am using on Microsoft Windows.
    apache-ant-1.7.0
    ant-contrib-1.0b3
    cpptasks-1.0b4

    I have set the following env variables:
    - set ANT_HOME: C:\Ant
    - set JAVA_HOME: C:\j2sdk1.4.2_16\bin
    - set PATH: included JAVA_HOME and ANT_HOME

    I have the ant-contrib-1.0b3.jar and cpptasks.jar in C:\Ant\lib folder.

    This is my build.xml

    <?xml version="1.0"?>
    <project xmlns:cpptasks="antlib:org.sf.net.antcontrib.cpptasks" name="Hello" default="hello" basedir=".">
    <target name="init">
      <condition property="cc" value="msvc">
       <os family="windows"/>
      </condition>
      <condition property="cc" value="gcc">
       <os family="unix"/>
      </condition>
    </target>
    <target name="hello" depends="init">
      <cc name="${cc}" outfile="hello">
       <fileset dir="." includes="hello.cpp"/>
      </cc>
    </target>
    </project>

    hello.cpp is in C:\Ant\src folder.

    The error I get is:::
    C:\Ant>ant -verbose
    Apache Ant version 1.7.0 compiled on December 13 2006
    Buildfile: build.xml
    Detected Java version: 1.4 in: C:\j2sdk1.4.2_16\jre
    Detected OS: Windows XP
    parsing buildfile C:\Ant\build.xml with URI = file:/C:/Ant/build.xml
    Project base dir set to: C:\Ant
    Build sequence for target(s) `hello' is [init, hello]
    Complete build sequence is [init, hello, ]

    init:
    [antlib:org.apache.tools.ant] Could not load definitions from resource org/apach
    e/tools/ant/antlib.xml. It could not be found.

    hello:

    BUILD FAILED
    C:\Ant\build.xml:12: Problem: failed to create task or type cc
    Cause: The name is undefined.
    Action: Check the spelling.
    Action: Check that any custom tasks/types have been declared.
    Action: Check that any <presetdef>/<macrodef> declarations have taken place.

            at org.apache.tools.ant.UnknownElement.getNotFoundException(UnknownElement.java:481)
            at org.apache.tools.ant.UnknownElement.makeObject(UnknownElement.java:413)
            at org.apache.tools.ant.UnknownElement.maybeConfigure(UnknownElement.java:160)
            at org.apache.tools.ant.Task.perform(Task.java:347)
            at org.apache.tools.ant.Target.execute(Target.java:357)
            at org.apache.tools.ant.Target.performTasks(Target.java:385)
            at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1329)
            at org.apache.tools.ant.Project.executeTarget(Project.java:1298)
            at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
            at org.apache.tools.ant.Project.executeTargets(Project.java:1181)
            at org.apache.tools.ant.Main.runBuild(Main.java:698)
            at org.apache.tools.ant.Main.startAnt(Main.java:199)
            at org.apache.tools.ant.launch.Launcher.run(Launcher.java:257)
            at org.apache.tools.ant.launch.Launcher.main(Launcher.java:104)

    Total time: 0 seconds

    Can anyone tell me where am I going wrong?

    Thanks,
    Sharad.

     
    • Michael
      Michael
      2007-10-25

      I put the taskdef for cpptasks in a separate target, upon which the other targets can depend if they need it.  Try adding this target and adding it to the depends list for the hello target.

         <!-- Pick up the cpptasks definitions -->
         <!-- Documentation at: http://ant-contrib.sourceforge.net/cc.html -->
         <target name="cpptasks">
           <taskdef resource="net/sf/antcontrib/cpptasks/antlib.xml">
             <classpath>
               <pathelement location="<path to cpptasks.jar>"/>
             </classpath>
           </taskdef>
        </target>

      -Michael

       
    • sharadb
      sharadb
      2007-10-25

      Thanks for the quick reply.
      After I included the above target I get errors:
      1) Could not load definitions from resource org/apache/tools/ant/antlib.xml. It could not be found
      2) Could not launch cl: java.io.IOException: CreateProcess: cl /c /nologo /DNDEBUG /MD C:\Ant\hello.cpp error=2

      I later used apache-ant-1.6.5, and the first issue was resolved, probably the antlib.xml is not present in the apache-ant-1.7.0 version.

      But I am facing the below issue now.

      C:\Ant>ant -verbose -noclasspath
      Apache Ant version 1.6.5 compiled on June 2 2005
      Buildfile: build.xml
      Detected Java version: 1.4 in: C:\j2sdk1.4.2_16\jre
      Detected OS: Windows XP
      parsing buildfile C:\Ant\build.xml with URI = file:///C:/Ant/build.xml
      Project base dir set to: C:\Ant
      Build sequence for target(s) `hello' is [cpptasks, init, hello]
      Complete build sequence is [cpptasks, init, hello, ]

      cpptasks:
      parsing buildfile jar:file:/C:/Ant/lib/cpptasks.jar!/net/sf/antcontrib/cpptasks/
      antlib.xml with URI = jar:file:/C:/Ant/lib/cpptasks.jar!/net/sf/antcontrib/cppta
      sks/antlib.xml

      init:

      hello:
             [cc] 1 total files to be compiled.
             [cc] cl /c /nologo /DNDEBUG /MD C:\Ant\hello.cpp

      BUILD FAILED
      C:\Ant\build.xml:19: Could not launch cl: java.io.IOException: CreateProcess: cl
      /c /nologo /DNDEBUG /MD C:\Ant\hello.cpp error=2
              at net.sf.antcontrib.cpptasks.CUtil.runCommand(CUtil.java:363)
              at net.sf.antcontrib.cpptasks.compiler.CommandLineCompiler.runCommand(CommandLineCompiler.java:432)
              at net.sf.antcontrib.cpptasks.compiler.CommandLineCompiler.compile(CommandLineCompiler.java:202)
              at net.sf.antcontrib.cpptasks.compiler.CommandLineCompilerConfiguration.compile(CommandLineCompilerConfiguration.java:133)
              at net.sf.antcontrib.cpptasks.CCTask.execute(CCTask.java:702)
              at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
              at org.apache.tools.ant.Task.perform(Task.java:364)
              at org.apache.tools.ant.Target.execute(Target.java:341)
              at org.apache.tools.ant.Target.performTasks(Target.java:369)
              at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
              at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
              at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
              at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
              at org.apache.tools.ant.Main.runBuild(Main.java:668)
              at org.apache.tools.ant.Main.startAnt(Main.java:187)
              at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
              at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)

      Total time: 0 seconds

      Would appreciate if anyone can help me with this.

      Thanks,
      Sharad.

       
      • Michael
        Michael
        2007-10-25

        Is the compiler executable 'cl' in your path?

        Ant is trying to call: "cl /c /nologo /DNDEBUG /MD C:\Ant\hello.cpp" but 'cl' isn't being found.  What happens if you run the above command from the command line?

         
      • Adrian Ber
        Adrian Ber
        2009-02-01

        Probably it is a little bit late to answer, but I had the same problem and here it is how I solved it: http://beradrian.wordpress.com/2009/02/01/visual-c-in-ant/.

         
    • sharadb
      sharadb
      2007-10-25

      Thanks Micheal,

      The cl executable was not in the path. Adding it to the path resolved that issue. But now I am facing a linker issue. Ant idea?

      C:\Ant>ant -verbose -noclasspath
      Apache Ant version 1.6.5 compiled on June 2 2005
      Buildfile: build.xml
      Detected Java version: 1.4 in: C:\j2sdk1.4.2_16\jre
      Detected OS: Windows XP
      parsing buildfile C:\Ant\build.xml with URI = file:///C:/Ant/build.xml
      Project base dir set to: C:\Ant
      Build sequence for target(s) `hello' is [cpptasks, init, hello]
      Complete build sequence is [cpptasks, init, hello, ]

      cpptasks:
      parsing buildfile jar:file:/C:/Ant/lib/cpptasks.jar!/net/sf/antcontrib/cpptasks/
      antlib.xml with URI = jar:file:/C:/Ant/lib/cpptasks.jar!/net/sf/antcontrib/cppta
      sks/antlib.xml

      init:

      hello:
             [cc] Starting dependency analysis for 1 files.
             [cc] Parsing hello.cpp
             [cc] 1 files are up to date.
             [cc] 0 files to be recompiled from dependency analysis.
             [cc] 0 total files to be compiled.
             [cc] Starting link
             [cc] link /NOLOGO /SUBSYSTEM:WINDOWS /INCREMENTAL:NO /OUT:hello.exe hello.obj
             [cc] MSVCRT.lib(crtexew.obj) : error LNK2019: unresolved external symbol_WinMain@16 referenced in function ___tmainCRTStartup
             [cc] hello.exe : fatal error LNK1120: 1 unresolved externals

      BUILD FAILED
      C:\Ant\build.xml:19: link failed with return code 1120
      at net.sf.antcontrib.cpptasks.compiler.CommandLineLinker.link(CommandLineLinker.java:278)
              at net.sf.antcontrib.cpptasks.compiler.CommandLineLinkerConfiguration.link(CommandLineLinkerConfiguration.java:117)
              at net.sf.antcontrib.cpptasks.CCTask.execute(CCTask.java:773)
              at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
              at org.apache.tools.ant.Task.perform(Task.java:364)
              at org.apache.tools.ant.Target.execute(Target.java:341)
              at org.apache.tools.ant.Target.performTasks(Target.java:369)
              at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
              at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
              at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
              at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
              at org.apache.tools.ant.Main.runBuild(Main.java:668)
              at org.apache.tools.ant.Main.startAnt(Main.java:187)
              at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
              at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)

      Total time: 1 second

      Thanks,
      Sharad

       
      • Michael
        Michael
        2007-10-25

        It looks like the linker needs to be told some additional information to pick up some additional linker dependencies.  I don't see it as problem directly related to cpptasks.  You will probably need to add some compilerarg, linkerarg, defineset, includepath, or libset fields so that it picks up the resources your application needs.

        I don't use cpptasks with Visual Studio myself, but I'd start by creating a similar VS project, turning on verbose compile/link output, and copying the arguments into your cpptasks-based Ant target.  I'd start with adding a <libset .../> and point it at the MSVCRT.lib library.  The cpptasks target does a lot for you, but it still requires a little tweaking in the beginning.

         
      • Hi !

        You need to set the subsystem to 'console', I believe the default is 'gui', you do this by adding a subsystem="console" to <cc>.
        See http://ant-contrib.sourceforge.net/cc.html for more information.

        MAT.

         
    • sharadb
      sharadb
      2007-10-26

      Awesome!!!
      That worked.
      Thanks a ton for helping me out with my first cc task.

      --Sharad.

       
    • sharadb
      sharadb
      2007-11-09

      Now I am trying to execute this in UNIX, facing some issues there..

      1) Downloaded 'apache-ant-1.6.5-bin.tar.gz' and extracted into my home dir

      2) Modified the ant script as follows

      ANT_HOME=<home_dir>/apache-ant-1.6.5
      JAVA_HOME=<java_home>
      PATH=${PATH}:${ANT_HOME}/bin:${JAVA_HOME}/bin:$/usr/local/bin/cc  (/usr/blocal/bin/cc is path to C++ compiler)
      export CLASSPATH ANT_HOME JAVA_HOME

      3) Added the above build.xml and hello.cpp file

      4) Executed using the following

      -> sh ant -verbose -noclasspath
      Apache Ant version 1.6.5 compiled on June 2 2005
      Buildfile: build.xml
      Detected OS: SunOS
      parsing buildfile <home_dir>/apache-ant-1.6.5/bin/build.xml with URI = file://<home_dir>/apache-ant-1.6.5/bin/build.xml
      Project base dir set to: <home_dir>/apache-ant-1.6.5/bin
      Build sequence for target(s) `hello' is [cpptasks, init, hello]
      Complete build sequence is [cpptasks, init, hello, ]

      cpptasks:
      dropping <home_dir>/apache-ant-1.6.5/bin/lib from path as it doesn't exist
      parsing buildfile jar:file:<home_dir>/apache-ant-1.6.5/lib/cpptasks.jar!/net/sf/antcontrib/cpptasks/antlib.xml with URI = jar:file:<home_dir>/apache-ant-1.6.5/lib/cpptasks.jar!/net/sf/antcontrib/cpptasks/antlib.xml

      init:

      hello:
             [cc] Starting dependency analysis for 1 files.
             [cc] Parsing hello.cpp
             [cc] 1 files are up to date.
             [cc] 0 files to be recompiled from dependency analysis.
             [cc] 0 total files to be compiled.
             [cc] Starting link
             [cc] gcc -o hello hello.o
             [cc] Undefined                   first referenced
             [cc]  symbol                         in file
             [cc] std::cout                           hello.o
             [cc] std::ios_base::Init::~Init [in-charge]()hello.o
             [cc] std::ios_base::Init::Init[in-charge]()hello.o
             [cc] __gxx_personality_v0                hello.o
             [cc] std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)hello.o
             [cc] ld: fatal: Symbol referencing errors. No output written to hello
             [cc] collect2: ld returned 1 exit status

      BUILD FAILED
      <home_dir>/apache-ant-1.6.5/bin/build.xml:19: gcc failed with return code 1
              at net.sf.antcontrib.cpptasks.compiler.CommandLineLinker.link(CommandLineLinker.java:278)
              at net.sf.antcontrib.cpptasks.compiler.CommandLineLinkerConfiguration.link(CommandLineLinkerConfiguration.java:117)
              at net.sf.antcontrib.cpptasks.CCTask.execute(CCTask.java:773)
              at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:275)
              at org.apache.tools.ant.Task.perform(Task.java:364)
              at org.apache.tools.ant.Target.execute(Target.java:341)
              at org.apache.tools.ant.Target.performTasks(Target.java:369)
              at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1216)
              at org.apache.tools.ant.Project.executeTarget(Project.java:1185)
              at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:40)
              at org.apache.tools.ant.Project.executeTargets(Project.java:1068)
              at org.apache.tools.ant.Main.runBuild(Main.java:668)
              at org.apache.tools.ant.Main.startAnt(Main.java:187)
              at org.apache.tools.ant.launch.Launcher.run(Launcher.java:246)
              at org.apache.tools.ant.launch.Launcher.main(Launcher.java:67)

      Total time: 2 seconds

      Can somebody tell me from where does it pick up this path?
      "dropping <home_dir>/apache-ant-1.6.5/bin/lib from path as it doesn't exist"

      And is this again some issue with the C++ compiler/linker?

      What could be the issue and resolution?

      Thanks,
      Sharad.