编译简单的 Apache Portable Runtime 1.5.2 程序

标签 c makefile compilation apache-portable-runtime

按照 here 执行以下步骤和 here我尝试使用 apr 1.5.2 和 apr-utils 1.5.4 编译一个简单的程序。程序代码如下:

    #include <apr.h>
    #include <stdlib.h>

    int main(int argc, char *argv[]) {
        apr_pool_t *pool;
        apr_file_t *out;

        apr_initialize();
        atexit(apr_terminate());

        apr_pool_create(&pool, NULL);
        apr_file_open_stdout(&out, pool);

        apr_file_printf(out, "Hello World\n");

        apr_pool_destroy(pool);
        return EXIT_SUCCESS;
    }

现在,当我尝试使用以下 Makefile 编译它时,编译器返回错误。

生成文件:

    PREFIX?=/usr/local/apr
    CFLAGS=-g -Wall -I${PREFIX}/include/apr-1
    LDFLAGS=-L${PREFIX}/apr/lib -lapr-1 -pthread -laprutil-1
    CC=clang

    .PHONY: all clean
    all: hello

    clean:
            rm -f *.o

编译器输出:

 $ make
 clang -g -Wall -I/usr/local/apr/include/apr-1  -L/usr/local/apr/apr/lib -lapr-1 -pthread -laprutil-1  hello.c   -o hello
hello.c:5:5: error: use of undeclared identifier 'apr_pool_t'
    apr_pool_t *pool;
    ^
hello.c:5:17: error: use of undeclared identifier 'pool'
    apr_pool_t *pool;
                ^
hello.c:6:5: error: unknown type name 'apr_file_t'; did you mean 'apr_size_t'?
    apr_file_t *out;
    ^~~~~~~~~~
    apr_size_t
/usr/local/apr/include/apr-1/apr.h:356:26: note: 'apr_size_t' declared here
typedef  size_t          apr_size_t;
                         ^
hello.c:8:5: warning: implicit declaration of function 'apr_initialize' is
      invalid in C99 [-Wimplicit-function-declaration]
    apr_initialize();
    ^
hello.c:9:12: warning: implicit declaration of function 'apr_terminate' is
      invalid in C99 [-Wimplicit-function-declaration]
    atexit(apr_terminate());
           ^
hello.c:9:12: warning: incompatible integer to pointer conversion passing 'int'
      to parameter of type 'void (*)(void)' [-Wint-conversion]
    atexit(apr_terminate());
           ^~~~~~~~~~~~~~~
/usr/include/stdlib.h:519:27: note: passing argument to parameter '__func' here
extern int atexit (void (*__func) (void)) __THROW __nonnull ((1));
                          ^
hello.c:11:5: warning: implicit declaration of function 'apr_pool_create' is
      invalid in C99 [-Wimplicit-function-declaration]
    apr_pool_create(&pool, NULL);
    ^
hello.c:11:22: error: use of undeclared identifier 'pool'
    apr_pool_create(&pool, NULL);
                     ^
hello.c:12:5: warning: implicit declaration of function 'apr_file_open_stdout'
      is invalid in C99 [-Wimplicit-function-declaration]
    apr_file_open_stdout(&out, pool);
    ^
hello.c:12:32: error: use of undeclared identifier 'pool'
    apr_file_open_stdout(&out, pool);
                               ^
hello.c:13:5: warning: implicit declaration of function 'apr_file_printf' is
      invalid in C99 [-Wimplicit-function-declaration]
    apr_file_printf(out, "Hello World\n");
    ^
hello.c:14:5: warning: implicit declaration of function 'apr_pool_destroy' is
      invalid in C99 [-Wimplicit-function-declaration]
    apr_pool_destroy(pool);
    ^
hello.c:14:22: error: use of undeclared identifier 'pool'
    apr_pool_destroy(pool);
                     ^
7 warnings and 6 errors generated.
make: *** [hello] Error 1

注意:在示例中,我从中获取了Makefile,还有一个链接-I${PREFIX}/apr/include/apr-util-1,但是安装了apr-utils没有创建此文件夹。

最佳答案

<apr.h> header 用于 platform definitions 。它定义了 APR 可用的内容(有 WinAPI?有 OpenSSL?)和标准数据类型( apr_byte_tapr_uint32_t .. 等)。它是在构建过程中自动生成的 ./configure .

您需要包含的内容是 apr_pools.hapr_file_io.h分别用于内存池和文件IO。

进一步 atexit(3) 需要一个函数指针并且您传入了一个函数。相反,它应该说:

atexit(apr_terminate);
<小时/>

像这样的错误很容易自行调试。当编译器提示您正在使用的库中存在未声明的标识符时,您几乎肯定忘记了包含适当的 header 。

关于编译简单的 Apache Portable Runtime 1.5.2 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38021860/

相关文章:

java - 编译在运行时引用当前程序类的Java代码

c - 指针寻址行为

c - 理解 C : Incrementing pointers

c - 为什么需要 OpenMP 减少子句来使减少并发?

makefile - 如何在 include 的子目录中安装 automake 安装头文件

Java.io在Debian

python - 规范的嵌入式交互式 Python 解释器示例?

c - make 使用 autotools 给出 undefined reference 错误,而自定义 Makefile 工作正常

shell - 在 Makefile 中做简单的数学运算

c++ - 如何一起使用编译和使用多个类似的 c++ 库?