使用ant构建c++ 程序

本文介绍如何使用Ant工具构建C++程序,并提供了一个具体的构建脚本示例。该脚本利用了cpptasks和ant-contrib插件来完成C++源代码的编译和链接过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用ant构建c++ 程序 需要先下载 cpptasks  为了一些附加的功能需要下载 ant-contrib ( http://ant-contrib.sourceforge.net/) ,安装这两个工具很简单,只需要降cpptasks.jar 和antcontrib.jar 拷贝到ant_home/lib目录底下即可 以下是一个例子,

build_cpp.xml

<? xml version="1.0" ?>
< project  name ="antcpp"  default ="test" >
<!--  引入外部文件 -->  
< import  file ="build.inc" />

< taskdef  resource ="cpptasks.tasks" />
< typedef  resource ="cpptasks.types" />
< taskdef  resource ="net/sf/antcontrib/antcontrib.properties" >
  
< classpath >
    
< pathelement  location ="/usr/share/java/ant-contrib.jar" />
  
</ classpath >
</ taskdef >
 
< property  name ="base.dir"  value ="." />
< property  name ="debug"  value ="true" />
< property  name ="compiler"  value ="gcc" />
< property  name ="src.dir"  location ="${base.dir}" />
< property  name ="samples.dir"  location ="${base.dir}/samples" />
< property  name ="build.dir"  location ="build" />
< property  name ="obj.dir"  location ="${build.dir}/obj" />
< property  name ="include.dir"  location ="${base.dir}/include" />
< property  name ="config.dir"  location ="${base.dir}/config" />
< property  name ="major"  value ="0" />
< property  name ="minor"  value ="9" />
< property  name ="build"  value ="8" />
<!--  specify api="unix" or api="win32" override platform default  -->
< property  name ="api"  value ="default" />
 
<!--  compiler flag define -->  
< property  name ="CXX_DFLAGS"  value ="${DFLAGS} -DXML_ERRORS -DHAVE_CONFIG_H -DFOR_LT " />  
< property  name ="RIGHT_HOME"  value ="${CGI_HOME}/right" />  
 
< target  name ="usage" >
        
        
< echo  message ="Usage:" />
        
< echo  message ="ant -f build_cpp.xml" />
</ target >
 
< target  name ="init" >
        
< echo  message ="build init" />
        
< mkdir  dir ="${build.dir}" />
        
< mkdir  dir ="${obj.dir}" />
        
< condition  property ="is-gcc" >
                
< or >
                        
< equals  arg1 ="${compiler}"  arg2 ="gcc" />
                        
< equals  arg1 ="${compiler}"  arg2 ="g++" />
                
</ or >
        
</ condition >
        
< condition  property ="is-msvc" >
                
< or >
                        
< equals  arg1 ="${compiler}"  arg2 ="msvc" />
                
</ or >
        
</ condition >
        
< condition  property ="is-windows" >< os  family ="windows" /></ condition >
        
< condition  property ="is-win32" >
                
< or >
                        
< equals  arg1 ="${api}"  arg2 ="win32" />
                        
< and >
                                
< equals  arg1 ="${api}"  arg2 ="default" />
                                
< isset  property ="is-windows" />
                        
</ and >
                
</ or >
        
</ condition >
        
< property  environment ="env" />
        
<!--   in case not set in environment, use an insignificant value  -->
        
< property  name ="env.LD_LIBRARY_PATH"  value ="." />
 
</ target >
 
< target  name ="clean" >
        
< echo  message ="build clean all" />
        
< delete  dir ="${build.dir}" />
 
</ target >
 

< target  name ="build-cpp"   >
       
< basename  property ="test.name"  file ="${a123}"  suffix ="cpp" />
       
< echo  message ="compile ${test.name}.cpp with ${compiler}" />
       
<!--   preprocesses .ui and .h files, places generated files on obj.dir   -->
        
< cc  subsystem ="console"
            objdir
="${obj.dir}"
            debug
="${debug}"
            outtype
="executable"
            name
="${compiler}"
            exceptions
="true"
            rtti
="true"
            optimize
="speed"
            outfile
="${build.dir}/${test.name}.cgi"
            
>
            
< fileset  dir ="${src.dir}"  includes ="${test.name}.cpp" />
 
            
< syslibset  libs ="z,m,cgicc,ghttp,ltapi${LTXML_VERSION},ltstd${LTXML_VERSION}" />
            
< libset  dir ="${MYSQL_LIB}"  libs ="mysqlclient" />
            
< libset  dir ="../../lib"  libs ="gspsright,myutil,db,share,webdev,xmlcpp,ini,net" />
            
< compilerarg  value ="-O2" />
            
< compilerarg  value ="-Wno-deprecated" />
            
< defineset >
                
< define  name ="FOR_LT" />  
                
< define  name ="HAVE_CONFIG_H" />
                
< define  name ="XML_ERRORS" />
                
< define  name ="SQLLOG" />  
                
< define  name ="GSPS_HOME"  value ="&quot;${PUB_HOME}&quot;" />  
            
</ defineset >
            
< includepath  location ="${MYSQL_INC}" />
            
< includepath  location ="/usr/local/include" />
            
< includepath  location ="${LTXML_INC}" />
            
< includepath  location ="${BOOST_HOME}" />
        
</ cc >
 
</ target >
 
< target  name ="build-all"  depends ="init,setProperties" >
    
< echo  message ="The first five letters of the alphabet are:" />
    
< foreach  param ="a123"  target ="build-cpp" >
        
< path >
        
< fileset  dir ="${src.dir}"  includes ="*.cpp" />
        
</ path >
    
</ foreach >
</ target >  

< target  name ="test"  depends ="build-all"   >
        
< exec  dir ="${build.dir}"
              executable
="ls"
              failonerror
="false" >
        
</ exec >
</ target >
</ project >  

build_cpp.inc

< project  name ="Const_variant"  default ="setProperties" >
        
< taskdef  resource ="cpptasks.tasks" />
        
< typedef  resource ="cpptasks.types" />
        
< taskdef  resource ="net/sf/antcontrib/antcontrib.properties" >
                
< classpath >
                        
< pathelement  location ="/usr/share/java/ant-contrib.jar" />
                
</ classpath >
        
</ taskdef >
        
< property  name ="GSPS_HOME"  value ="" />
        
< property  name ="PUB_HOME"  value ="" />
        
< property  name ="APACHE_HOME"  value ="" />
        
< property  name ="CGI_HOME"  value ="${APACHE_HOME}/cgi-bin/" />
        
< property  name ="HTML_HOME"  value ="${APACHE_HOME}/htdocs/" />
        
< property  name ="BIN_HOME"  value ="${PUB_HOME}/bin" />
        
< property  name ="TMPL_HOME"  value ="${PUB_HOME}/tmpl" />
        
< property  name ="DATA_HOME"  value ="${PUB_HOME}/data" />
        
< property  name ="LIB_HOME"  value ="${PUB_HOME}/lib" />
        
< property  name ="RPATH"  value ="${LIB_HOME}" />
        
< property  name ="GDB_FLAGS"  value ="-O2" />
      
        
< property  name ="DGSPS_HOME"  value ="-DGSPS_HOME=&quot;${PUB_HOME}&quot;" />
        
< property  name ="DSQLLOG"  value ="-DSQLLOG" />
        
< property  name ="MYSQL_HOME"  value ="/usr/local/mysql" />
 
 
        
< property  name ="BOOST_HOME"  value ="${PUB_HOME}/supports/boost-1.30.2" />   
        
< property  name ="PERL_INC"  value ="" />  
        
< property  name ="QUEUE_LIBS"  value ="-lqdoctransapi -lqpageapi -lqsubjectapi -lqcolumnapi 
                           -lqcrosspublishapi -lqautoadddocapi -lqpubapi -lqcgicallapi -lqcgicall 
                                      -lqemailcallapi -lqueue  -lqcreator -lurlencoder -lhttp"
/>  
 
        
< property  name ="LTXML_VERSION"  value ="12" />  
        
< property  name ="LTXML_INC"  value ="/usr/local/include/ltxml${LTXML_VERSION}" />   
 
        
< property  name ="MYSQL_INC"  value ="${MYSQL_HOME}/include/mysql" />
        
< property  name ="MYSQL_LIB"  value ="${MYSQL_HOME}/lib/mysql" />
 
        
<!--
                        <echo message="The OS is FreeBSD" />
                        <property name="MYSQL_INC" value="${MYSQL_HOME}/include" />
                        <property name="MYSQL_LIB" value="${MYSQL_HOME}/lib"/>
                
-->
 
        
< target  name ="setProperties" >
                
< shellscript  shell ="sh"  inputstring ="Magick++-config --libs"  outputproperty ="MAGICK_LIBS" />
                
< shellscript  shell ="sh"  inputstring ="uname"  outputproperty ="OS" />
                
< shellscript  shell ="sh"  inputstring ="/usr/local/bin/perl -MConfig -e 'print $$Config{archlib}'"                         outputproperty ="PERL_INC" />
                
< shellscript  shell ="sh"  inputstring ="/usr/local/bin/perl -MExtUtils::Embed -e ccopts -e ldopts"                         outputproperty ="PERL_LIBS" />
                
< echo  message ="${MAGICK_LIBS}" />
                
< echo  message ="${OS}" />  
                
< echo  message ="${PERL_INC}" />  
                
< echo  message ="${PERL_LIBS}" />  
        
</ target >
</ project >
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值