2010-11-04 49 views
2

自從我使用C之後很長一段時間,但現在我正在嘗試編譯一個從Apache-Portable-Runtime(APR)獲取服務器狀態的短腳本。位於/usr/include/apr-1/apr*.h如何編譯APR測試腳本

頭文件和庫位於/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的經驗對我有什麼建議可以使它工作?

一如既往的在此先感謝。

+0

如果有人認爲有幫助,我可以添加'gcc -v'輸出。 – CoffeeMonster 2010-11-05 21:15:53

+1

「apr-1-config --cflags --cppflags --includes --link-ld」的輸出可能會有幫助 – 2010-11-06 19:04:54

回答

0

我終於繞過來看看這個。

gcc提到-l兩次在不同的語境:

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] 

我不能完全肯定我理解的鏈接順序,爲什麼它沒有工作之前(如果有人能闡明,這將是夢幻般一些輕),但現在我有足夠的繼續。

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 
+0

謝謝,我不知道apr-1-config。 運行這個過程給了我一個類似的標誌'-pthread -DLINUX = 2 -D_REENTRANT -D_GNU_SOURCE -I/usr/include/apr-1 -L/usr/lib64 -lapr-1',但不幸的是,這並不能解決我的問題。更新原始問題以使用apr-1-config。 – CoffeeMonster 2010-11-05 21:08:05

+0

@咖啡,這很奇怪。你確認'/ usr/lib64/libapr-1.so'是否是一個有效的文件(或符號鏈接)? – 2010-11-06 18:26:52

+0

其符號鏈接'/usr/lib64/libapr-1.so - > libapr-1.so.0.3' – CoffeeMonster 2010-11-10 19:04:54