代码之家  ›  专栏  ›  技术社区  ›  CoffeeMonster

如何编译apr测试脚本

  •  2
  • CoffeeMonster  · 技术社区  · 15 年前

    从使用C开始已经很长时间了,但现在我正试图编译一个从ApachePortableRuntime(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 回复  |  直到 14 年前
        1
  •  0
  •   Matthew Flaschen    15 年前
    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
    
        2
  •  0
  •   CoffeeMonster    14 年前

    我终于有时间研究这个问题了。

    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]
    

    我不完全确定我是否理解链接顺序以及为什么它以前不起作用(如果有人能解释一下,那就太好了),但现在我有足够的时间继续。

    推荐文章