免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7711 | 回复: 9
打印 上一主题 下一主题

为什么一个project内有很多main入口函数? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-04 20:28 |只看该作者 |倒序浏览
问题如上所述。
hash.c:main (void)
html-parse.c:int main ()
netrc.c:main (int argc, char **argv)
test.c:main (int argc, char *argv[])
main.c:       main()

论坛徽章:
0
2 [报告]
发表于 2011-10-04 20:35 |只看该作者
这个project生成多个可执行程序吧

论坛徽章:
0
3 [报告]
发表于 2011-10-04 20:36 |只看该作者
就是一个wget还会有多少个执行程序?不是运行wget 后面就是参数了吧。

论坛徽章:
0
4 [报告]
发表于 2011-10-05 09:00 |只看该作者
呵呵,wget和curl,axel这种工具,最终会生成一个提供给用户的Exe
但是,不可避免会有多个main,因为有些是用于测试的,比如axel中search.c中就是这样的,但是他们是一般是不会与Axel的main冲突的,因为search.c中的main是用宏定义来声明的,要不然的话,如果.o链接进text.o中,编译器会报错的,多个main。

另外一点就是,确实是想编译多个工具,但是这多个c文件生成的.o文件是不会link到一起的。
楼主不妨按照我说的第一点去分析下不同main函数的作用,找找define宏是干啥的。
比如axel的search.c中这个

  1. #ifdef STANDALONE
  2. int main( int argc, char *argv[] )
  3. {
  4.         conf_t conf[1];
  5.         search_t *res;
  6.         int i, j;
  7.        
  8.         if( argc != 2 )
  9.         {
  10.                 fprintf( stderr, "Incorrect amount of arguments\n" );
  11.                 return( 1 );
  12.         }
  13.        
  14.         conf_init( conf );
  15.        
  16.         res = malloc( sizeof( search_t ) * ( conf->search_amount + 1 ) );
  17.         memset( res, 0, sizeof( search_t ) * ( conf->search_amount + 1 ) );
  18.         res->conf = conf;
  19.        
  20.         i = search_makelist( res, argv[1] );
  21.         if( i == -1 )
  22.         {
  23.                 fprintf( stderr, "File not found\n" );
  24.                 return( 1 );
  25.         }
  26.         printf( "%i usable mirrors:\n", search_getspeeds( res, i ) );
  27.         search_sortlist( res, i );
  28.         for( j = 0; j < i; j ++ )
  29.                 printf( "%-70.70s %5i\n", res[j].url, res[j].speed );
  30.        
  31.         return( 0 );
  32. }
  33. #endif
复制代码

论坛徽章:
0
5 [报告]
发表于 2011-10-05 09:01 |只看该作者
那种不加define的main一般都是工程的main函数:wink:

论坛徽章:
0
6 [报告]
发表于 2011-10-05 15:37 |只看该作者
回复 5# duanjigang
是啊。

在hash.c中
31 /* With -DSTANDALONE, this file can be compiled outside Wget source
32    tree.  To test, also use -DTEST.   如果加上DSTANDLONE,此文件在外面编译。 */
33
34 #ifndef STANDALONE
35 # include "wget.h"
36 #endif

在html-parse.c
89 /* To test as standalone, compile with `-DSTANDALONE -I.'.  You'll
  90    still need Wget headers to compile.  */
  91
  92 #include "wget.h"
  93
  94 #ifdef STANDALONE
  95 # define I_REALLY_WANT_CTYPE_MACROS
  96 #endif
test.c中是TESTING
31 #include <stdio.h>
32
33 #include "test.h"
34
35 #ifndef TESTING
36 #error "TESTING not set!!!"
37 #endif

论坛徽章:
0
7 [报告]
发表于 2012-02-10 11:18 |只看该作者
回复 5# duanjigang
你好,我也在linux开发应用程序时遇到了多个main函数,但是这些main函数是如何被调用的?如何决定它被调用的时间,比如想加入一个led状态灯(不同颜色表示移动设备的各种状态)的控制程序,有一个main函数main (int argc, char **argv),想在开发的这款移动设备开机时就开始运行这个应用程序,就开启led灯。那我新写的这个应用程序要怎么加载进来?另外这个main函数是带参数的int argc, char **argv,它的参数是如何被传递进来的?是直接被调用吗?但是我在里面看其他类似的应用程序的main函数没有被调用过(已实现的功能)啊!



   

论坛徽章:
0
8 [报告]
发表于 2012-02-14 16:43 |只看该作者
回复 7# 847608498
多个main可能是为了编译生成多个exe程序
或者说是为了生成一些测试代码,其中很多main都是宏定义下声明的,没有包括在

  1. #ifdef _XX_

  2. #endif
复制代码
中的那个main才是工程的main函数

   

论坛徽章:
0
9 [报告]
发表于 2012-02-16 12:41 |只看该作者
回复 8# duanjigang


    那如何调用这个应用程序呢?直接调用它生成的exe文件吗?system();这个函数怎么用?主要是他的参数怎么写?

论坛徽章:
0
10 [报告]
发表于 2012-03-25 22:09 |只看该作者
看makefile就知道了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP