如何编译APR测试脚本

时间:2010-11-04 18:12:59

标签: c linux gentoo apr apache-portable-runtime

自从我使用C以来很长一段时间,但现在我正在尝试编译一个从Apache-Portable-Runtime(APR)获取服务器统计信息的短脚本。

位于/usr/include/apr-1/apr*.h和libs的头文件位于/ usr / lib64 / libapr-1。*

源文件可在官方APR网站http://apr.apache.org/docs/apr/1.3/files.html上找到。

/* test.c */
#include <stdio.h>
#include <stdlib.h>
#include <apr_general.h>
#include <apr_time.h>

int main(int argc, const char *argv[])
{
    apr_time_t t;
    t = apr_time_now();
    printf("The current time: %" APR_TIME_T_FMT "[us]\n", t);
    return 0;
}

当我尝试编译时,我收到以下错误(我认为这是一个链接问题):

~> gcc -Wall $(apr-1-config --cflags --cppflags --includes --link-ld) test.c -o test.bin
/tmp/cc4DYD2W.o: In function `main':
test.c:(.text+0x10): undefined reference to `apr_time_now'
collect2: ld returned 1 exit status

我的环境是gentoo:

~> uname -a
Linux alister 2.6.32.21-grsec-gt-r2 #1 SMP Tue Sep 7 23:54:49 PDT 2010\
x86_64 Intel(R) Xeon(R) CPU L5640 @ 2.27GHz GenuineIntel GNU/Linux`
~> gcc -v 
gcc version 4.3.4 (Gentoo 4.3.4 p1.1, pie-10.1.5)
~> emerge --search "%@^dev-lib.*apr"
*  dev-libs/apr
   Latest version installed: 1.3.9
*  dev-libs/apr-util
   Latest version installed: 1.3.9

在Linux上有更多C经验的人对我有什么建议吗?

一如既往地感谢。

2 个答案:

答案 0 :(得分:0)

gcc -Wall -I/usr/include/apr-1 -L/usr/lib64 -lapr-1 test.c -o test.bin

-l指定要链接到的共享库,而-L指定查找共享库的位置。

APR提供了一种工具,使这更容易,apr-1-config。这样的事情应该有效:

gcc -Wall $(apr-1-config --cflags --cppflags --includes --link-ld) test.c -o test.bin

答案 1 :(得分:0)

我终于开始研究这个了。

gcc在不同的情境中提到两次:

Linker Options
object-file-name -llibrary ...
Directory Options
... -Idir -Ldir ...

所以我在对象名称之后移动-llib(以获取第二个上下文)并编译它!

APR_CFG=$(apr-1-config --cflags --cppflags --includes --link-ld) 
gcc -Wall test.c -o test.bin $APR_CFG
./test.bin
The current time: 1332999950442660[us]

我不完全确定我理解链接顺序以及它之前没有用的原因(如果有人可以说明它会很棒)但是现在我已经足够继续了。