How to compile a APR test script

http://stackoverflow.com/questions/4099803/how-to-compile-a-apr-test-script

Its a long time since I've used C but now I'm trying to compile a short script that gets server-stats from the Apache-Portable-Runtime (APR).

Header files located at /usr/include/apr-1/apr*.h and libs are located at /usr/lib64/libapr-1.*

Source files can be found on the official APR site 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;
}

When I try and compile I get the following error (which I believe is a linking issue):

~> 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

My environment is 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

Does anyone with more experience with C on Linux have any suggestions for me to get this working?

As always thanks in advance.

share | improve this question

 
I can add the gcc -v output if someone thinks that will help. –  CoffeeMonster  Nov 5 '10 at 21:15
1  
The output of "apr-1-config --cflags --cppflags --includes --link-ld" might be helpful –  mu is too short  Nov 6 '10 at 19:04
feedback

2 Answers

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

-l specifies which shared library to link to, while -L specifies where to look for shared libraries.

APR provides a tool to make this easier, apr-1-config. Something like this should work:

gcc -Wall $(apr-1-config --cflags --cppflags --includes --link-ld) test.c -o test.bin
share | improve this answer
 
Thanks, I didn't know about apr-1-config. Running this gives me a similar set of flags -pthread -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -I/usr/include/apr-1 -L/usr/lib64 -lapr-1 but unfortunately doesn't solve my problem. Updating original question to use apr-1-config. –  CoffeeMonster  Nov 5 '10 at 21:08
 
@Coffee, that's odd. Did you confirm /usr/lib64/libapr-1.so is a valid file (or symbolic link)? –  Matthew Flaschen  Nov 6 '10 at 18:26
 
Its a symbolic link /usr/lib64/libapr-1.so -> libapr-1.so.0.3 –  CoffeeMonster  Nov 10 '10 at 19:04
feedback
up vote 0 down vote accepted

I finally got around to looking into this.

gcc mentions -l twice in different contexts:

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

so I moved the -llib after the object name (to get the 2nd context) and it compiled!

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]

I'm not fully sure I understand the linking order and why it didn't work before (if someone could shed some light on that it would be fantastic) but for now I have enough to continue.

share | improve this answer
 
Was this post useful to you?      

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值