0

herehereの手順に従って、 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 のインストールではこのフォルダーは作成されませんでした。

4

1 に答える 1

0

<apr.h>ヘッダーはプラットフォーム定義用です。APR で利用できるもの (WinAPI はありますか? OpenSSL はありますか?) と標準データ型 ( apr_byte_tapr_uint32_t.. など) を定義します。によってビルド中に自動的に生成され./configureます。

含める必要があるのはapr_pools.hapr_file_io.hメモリ プールとファイル IO のそれぞれです。

さらにatexit(3)、関数ポインターが必要で、関数を渡しました。代わりに、次のように言う必要があります。

atexit(apr_terminate);

このようなエラーは、自分で簡単にデバッグできます。使用しているライブラリからの宣言されていない識別子についてコンパイラが文句を言うとき、ほとんどの場合、適切なヘッダーを含めるのを忘れています。

于 2016-06-25T07:06:32.500 に答える