Apache Portable Runtimeを少し調べてみる

Apache Portable Runtime (APR) は Apache HTTP Server のサポートライブラリである。 OSとソフトウェアの間でOSなどの環境の違いを吸収するAPIを提供する。そして、他のOSに一般的にある機能が存在しないOSでは、APRが代替を提供する。よって、APRを使うことにより真のクロスプラットフォームなプログラムを作ることが出来る。

http://ja.wikipedia.org/wiki/Apache_Portable_Runtime

だそうです。

APRを使っている有名なプロダクトといえばSubversionでしょう。

本家のページを見てもらえればわかりますが、APRには0.9系と1.3系があります。

そして

APR 0.9.X and 1.X are binary-incompatible.

http://svn.collab.net/repos/svn/trunk/INSTALL

だそうです。

ともあれ(なんのこっちゃ?)以下を参考にHello, Worldしてみました。

環境は以下のとおりです。
Mac OS X 10.5.7
gcc 4.0.1

それではやってみます。

まずはAPRをソースからインストール

$ wget http://www.meisei-u.ac.jp/mirror/apache/dist/apr/apr-1.3.3.tar.gz
$ tar zxvf apr-1.3.3.tar.gz
$ cd apr-1.3.3
$ ./configure 
$ make
$ sudo make install

インストール後のディレクトリ構成はこんなかんじ

$ ls /usr/local/apr/
bin     build-1 include lib
$ ls /usr/local/apr/include/apr-1/apr
apr.h                apr_hash.h           apr_signal.h
apr_allocator.h      apr_inherit.h        apr_strings.h
apr_atomic.h         apr_lib.h            apr_support.h
apr_dso.h            apr_mmap.h           apr_tables.h
apr_env.h            apr_network_io.h     apr_thread_cond.h
apr_errno.h          apr_poll.h           apr_thread_mutex.h
apr_file_info.h      apr_pools.h          apr_thread_proc.h
apr_file_io.h        apr_portable.h       apr_thread_rwlock.h
apr_fnmatch.h        apr_proc_mutex.h     apr_time.h
apr_general.h        apr_random.h         apr_user.h
apr_getopt.h         apr_ring.h           apr_version.h
apr_global_mutex.h   apr_shm.h            apr_want.h
$ ls /usr/local/apr/lib/
apr.exp               libapr-1.a            pkgconfig/
libapr-1.0.3.3.dylib  libapr-1.dylib        
libapr-1.0.dylib      libapr-1.la       

実行するソースはこんなかんじ

helloworld.c

#include <stdlib.h>
#include <apr.h>
#include <apr_general.h>
#include <apr_pools.h>
#include <apr_strings.h>
#include <apr_file_io.h>

static apr_file_t *apr_stdout, *apr_stdin, *apr_stderr;

int main(int argc, const char* const *argv)
{
  apr_pool_t *p;
  char *str;

  apr_app_initialize(&argc, &argv, NULL);
  apr_pool_create(&p, NULL);
  apr_file_open_stdout(&apr_stdout, p);
  apr_file_open_stdin(&apr_stdin, p);
  apr_file_open_stderr(&apr_stderr, p);

  str = apr_pstrcat(p, "Hello", ", ", "World", NULL);
  apr_file_printf(apr_stdout, "%s\n", str);
  return EXIT_SUCCESS;
}

コンパイル、実行。

$ gcc -I/usr/local/apr/include/apr-1/ -lapr-1 /usr/local/apr/lib/libapr-1.a helloworld.c -o helloworld
$ ./helloworld
Hello, World

ワー、パチパチ。。。