博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
写了一个最简单的 apr 程序: apr_skeleton.c , 编译时却碰到了问题:

$ gcc `apr-config --cflags --libs` apr-skeleton.c -o apr-skeleton
In file included from /usr/include/apr-1.0/apr_general.h:28,
from apr-skeleton.c:12:
/usr/include/apr-1.0/apr.h:270: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘apr_off_t’apr_off_t

居然不是 apr_skeleton.c 的问题, 于是跟着提示打开了 apr.h 的第 270 行:

typedef off64_t apr_off_t;

估计是 off64_t 没定义的缘故, 于是 grep 了 /usr/include 一把, 发现相关的定义位于 unistd.h 中:

# if defined __USE_LARGEFILE64 && !defined __off64_t_defined
typedef __off64_t off64_t;
# define __off64_t_defined
# endif

看来是 __USE_LARGEFILE64 没有被定义, 继续 grep, 在 /usr/include/features.h 中发现了:

#ifdef _LARGEFILE64_SOURCE
# define __USE_LARGEFILE64 1
#endif

似乎找到原因了, 在 apr_skeleton.c 的第一行 define 了 _LARGEFILE64_SOURCE , 编译...成功, 链接...失败!?

提示:

/tmp/ccdrJW18.o:在函数‘main’中:
/home/ht/docs/apr/apr-tutorial/sample/apr-skeleton.c:22:对‘apr_initialize’未定 义的引用
/home/ht/docs/apr/apr-tutorial/sample/apr-skeleton.c:30:对‘apr_terminate’未定义的引用
collect2: ld returned 1 exit status

估计是 apr-config 没有带上库导致, 查看了一下, 果然是这样:

$ apr-config --cflags --libs
-pipe -Wall -g -O2 -pthread -luuid -lrt -lcrypt -lpthread -ldl

在 /usr/lib/ 找到 libapr-1.a , 就知道再加上 -lapr-1 就能搞定了, 于是:

$gcc -lapr-1 `apr-config --cflags --libs` apr-skeleton.c -o apr-skeleton

编译成功!


PS:
1) apr_skeleton.c 来自于 http://dev.ariel-networks.com/apr/, 这是一个适合初学都的教程.
2) 编译环境: $cat /proc/version

Linux version 2.6.18-5-686 (Debian 2.6.18.dfsg.1-17) (dannf@debian.org) (gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)) #1 SMP Mon Dec 24 16:41:07 UTC 2007

 

上面是老版本的解决方案,新的。

新的如下  /usr/local/apr/bin/apr-1-config --cflags --libs --link-ld 生成对应的GCC 编译选项

-g -O2 -pthread -luuid -lrt -lcrypt  -lpthread -ldl -L/usr/local/apr/lib -lapr-1

然后gcc  -g -O2 -pthread -luuid -lrt -lcrypt  -lpthread -ldl -L/usr/local/apr/lib -lapr-1 apr-skeleton.c -o apr-skeleton

注意事项:

 在所有实例源代码第一行要加上这句#define  _LARGEFILE64_SOURCE 才能解决apr.h 报错。

可能有人会问 apr-1-config是什么,从网上下载apr:http://apr.apache.org/,然后安装。

 

posted on 2009-12-29 18:41  Likwo  阅读(1685)  评论(0编辑  收藏  举报