g++とgccの違い

Leopardで空きメモリの量を知りたいと思って、
http://d.hatena.ne.jp/Christopher-727/20071110
を参考に(というか、そのままコピー)、下記のようなコードを書いてみる。

// 空きメモリ量を標準出力に表示する
#include <mach/mach_init.h>
#include <mach/host_info.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
	struct vm_statistics vm_info;
	mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
	host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_info, &count);
	printf("%d\n", vm_info.free_count*vm_page_size/1024/1024);

	return 0;
}

早速、コンパイル

% gcc leopard.c
% g++ leopard.c
leopard.c: In function ‘int main(int, char**)’:
leopard.c:14: error: ‘host_statistics’ was not declared in this scope

なむ? gccではコンパイルできるのに、g++ではできないですと?

う〜ん、C++から無理矢理Cの関数を読もうとしているからかなぁ。と言うことで、二つのmachのインクルードファイルをextern "C" {} で囲ってみるが、やはり同じエラーが出る。

??? そもそも、リンクで失敗しているわけではなくて、Parserで失敗している感じ。てことは、mach/mach_init.hがincludeされていないのだろうか?
でも、includeされない理由がわかんね〜。。。

と言うことで、力技でmach/mach_init.hのファイルの中から、host_statisticsの部分を抜き出して、ソースに貼付けてみる。

// 空きメモリ量を標準出力に表示する

extern "C" {
#include <mach/mach_init.h>
#include <mach/host_info.h>
}

#include <stdio.h>

kern_return_t host_statistics
(
        host_t host_priv,
        host_flavor_t flavor,
        host_info_t host_info_out,
        mach_msg_type_number_t *host_info_outCnt
);

さて、コンパイルだ!!

tomopple% g++ leopard.c                                 ~/Program/cpp/GetRUsage
Undefined symbols:
  "host_statistics(unsigned int, int, int*, unsigned int*)", referenced from:
      _main in cc0UiFay.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

うん、リンカのエラーになった。とすると、こりゃC++からCの問題だな、ってことで、host_statisticsのプロトタイプ宣言に対して、extern "C"を付けてやると、無事コンパイル完了。

一応、対処療法でコンパイルはできてるんだけど、なんで、そもそもhost_statisticsのプロトタイプ宣言が有効でないの? でも、g++ =Mで見ても、ちゃんとファイルは読んでる気配があるし、読めない理由がわからない。
とりあえず、家に帰ってからの宿題にしとくか。