man ひきたい

微妙にman引けない場合.@Ubuntu

% man 3 printf
No manual entry for printf in section 3
See 'man 7 undocumented' for help when manual pages are not available.

インストール.

% aptitude search manpages
...
p   manpages-ja                                  - 日本語版マニュアルページ (ユーザ用)
p   manpages-ja-dev                              - Japanese version of the manual pages (for developers)
...
% sudo aptitude install manpages-ja-dev

で,ひけるようになった.

glib インストール / コンパイル

インストール@Ubuntu

% sudo aptitude install libglib2.0-dev

パスの通った場所に glib.h ははいらない

バージョンを明示的に指定するためなのかな?

-I とか -l とかつけないと,エラーいろいろでる.

hoge.cpp:10:18: error: glib.h: No such file or directory
In file included from /usr/include/glib-2.0/glib/galloca.h:34,
                 from /usr/include/glib-2.0/glib.h:32,
                 from hoge.cpp:10:
/usr/include/glib-2.0/glib/gtypes.h:34:24: error: glibconfig.h: No such file or directory

とか.

コンパイラオプションつける

パスは通ってないのでコンパイラオプションにつける.

% g++ -g -Wall -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lglib -o hoge hoge.cpp

ソースには

#include <glib.h>

みたいな.

undefined reference to `g_timer_continue' / glib の timers 使い方

glibの timers を使おうとして,g_timer_continue を呼ぶと,「うまく言語化できない」などと言って,コンパイルできなかった.
というか, g_timer_new/start/stop などは使えるのに,なぜ g_timer_continue が使えない... と思ったのだけど,結論からいうと,さっきのエントリ (http://d.hatena.ne.jp/strkpy/20090918/1253206116) でのコンパイラオプションが間違っていた.

インクルードパスなどは,glib-2.0 のほうを指定してたのに,-lglib としていたことで,ライブラリ本体は libglib1.2 のほうにリンクしようとしていた.で, libglib1.2 のヘッダファイルを見たら, timers に g_timer_continue なんてないってさ!

ということで,コンパイラオプションは,

g++ -g -Wall -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -lglib-2.0 -o hoge hoge.cpp

のように -l もバージョン指定してあげましょう,ってことでした.

ヘッダファイルと本体のほうで違うバージョン読んでたとかもうなんていうか,はあ,そうかあ.もっと早く気づけなかったもんかなあ.

で,使い方

簡単.でもこの変数持ち回らなきゃいけない感じはラッパーかきたくなるなあ.w

#include <iostream>                                                                                                                                                                                                                         

#include <glib.h>
#include <unistd.h>

using namespace std;

int main(int argc, char *argv[])
{
  cout << "start:" << endl;
  GTimer *Timer;
  gulong time;
  gdouble diff;

  Timer = g_timer_new();

  cout << "sleeping 3 sec." << endl; sleep(3);

  diff = g_timer_elapsed(Timer, &time);
  cout << diff << endl;

  g_timer_stop(Timer);

  cout << "sleeping 3 sec." << endl; sleep(3);

  diff = g_timer_elapsed(Timer, &time);
  cout << diff << endl;

  g_timer_continue(Timer);

  cout << "sleeping 3 sec." << endl; sleep(3);

  diff = g_timer_elapsed(Timer, &time);
  cout << diff << endl;


  g_timer_stop(Timer);
  g_timer_destroy(Timer);

  cout << "end:" << endl;
  return 0;
}
start:
sleeping 3 sec.
3.00014
sleeping 3 sec.
3.00027
sleeping 3 sec.
6.00051
end:

これで簡単な時間計測ならこれでいける感じになった.かな.

Glib/Timers のラッパー

てことで,書いた.(ちょうてきとう
これで少し楽になった.はっは.

#include <iostream>

#include <glib.h>
#include <unistd.h>

using namespace std;

class GTimerP
{
private:
  GTimer *Timer;
  gulong microseconds;
  gdouble time;
public:
  GTimerP() { Timer = g_timer_new(); }
  ~GTimerP() { g_timer_destroy(Timer); }
  void start() { g_timer_start(Timer); }
  void reset() { start(); }
  void stop() { g_timer_stop(Timer); }
  gdouble elapsed() {
    time = g_timer_elapsed(Timer, &microseconds);
    return time;
  }
  void cont() { g_timer_continue(Timer); }
};

int main(int argc, char *argv[])
{
  cout << "start:" << endl;
  GTimerP Timer;

  cout << "sleeping 3 sec." << endl; sleep(3);

  cout << Timer.elapsed() << endl;

  Timer.stop();

  cout << "sleeping 3 sec." << endl; sleep(3);

  cout << Timer.elapsed() << endl;

  Timer.cont();

  cout << "sleeping 3 sec." << endl; sleep(3);

  cout << Timer.elapsed() << endl;

  cout << "end:" << endl;
  return 0;
}

結果は同じ

start:
sleeping 3 sec.
3.00018
sleeping 3 sec.
3.00045
sleeping 3 sec.
6.00067
end:

autotools で生成した実行ファイルはただのスクリプトファイルだから

gdb とかするには libtool とかでほげってしないと,

% gdb ./hoge
...
hoge: not in executable format: File format not recognized

ってエラーでてだめ.

% libtool --mode=execute gdb ./hoge

とかやる.