- blogs:
- cles::blog
2009/09/08
[ by hsur at 18:12]
Cでmain()が呼ばれる直前に関数を呼ぶ
研究用のCプログラムでmain()が呼ばれる直前をフックして別の処理を埋め込みたいという相談を後輩からうけたので、検索してみたところGCCであれば、__attribute__((constructor))を使えばすぐ出来るという話を見つけたので試して見たところ意外とすんなり実装できてびっくり。普段はほとんどC言語を使う機会もないので、こういう機能をじっくり調べる機会もあまりないんですよね。
GCC では main() の前に呼ばれる関数を __attribute__((constructor)) という拡張機能を使って定義できます。たとえば、次のプログラムでは main() の前に foo() が呼び出されます。
gccのinfoを検索してみると、ちゃんと解説もありました。
info gcc --index-search="Declaring Attributes of Functions"
5.24 Declaring Attributes of Functions
======================================
(中略)
`constructor'
`destructor'
The `constructor' attribute causes the function to be called
automatically before execution enters `main ()'. Similarly, the
`destructor' attribute causes the function to be called
automatically after `main ()' has completed or `exit ()' has been
called. Functions with these attributes are useful for
initializing data that will be used implicitly during the
execution of the program.
These attributes are not currently implemented for Objective-C.
======================================
(中略)
`constructor'
`destructor'
The `constructor' attribute causes the function to be called
automatically before execution enters `main ()'. Similarly, the
`destructor' attribute causes the function to be called
automatically after `main ()' has completed or `exit ()' has been
called. Functions with these attributes are useful for
initializing data that will be used implicitly during the
execution of the program.
These attributes are not currently implemented for Objective-C.
ほかにもattributeはいろいろあるみたいなので、このあたりは一読しておくといいのかも。
このエントリは役に立ちましたか?
トラックバックについて
Trackback URL:
お気軽にどうぞ。トラックバック前にポリシーをお読みください。[policy]
このエントリへのTrackbackにはこのURLが必要です→http://blog.cles.jp/item/3193
Trackbacks
このエントリにトラックバックはありません
Comments
愛のあるツッコミをお気軽にどうぞ。[policy]
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
古いエントリについてはコメント制御しているため、即時に反映されないことがあります。
コメントはありません
Comments Form
コメントは承認後の表示となります。
OpenIDでログインすると、即時に公開されます。
OpenID を使ってログインすることができます。
Copyright © 2004-2010 by CLES All Rights Reserved.
sp-20100319152446644595555@cles.net
sp-20100319152446644595555@cles.net
サイト内検索
おしらせ
検索ワード
- フック c 関数
- Yahoo! - 10/03/12 16:39:52
Tags [All Tags]
google | php | ruby | windows | firefox | linux | SoftwareEngineering | java | spam | ranking | curry | camera | seo | FireEmblem | thunderbird | server | marketing | mobile | Doctoral | docomo
へぇが多いエントリ [Top 100]
- おめでとうございます (4)
- 知恵の輪 サターン編 (3)
- SourceForge.JPのSubversion... (3)
- サーバセットアップ (3)
- 和食 小錦 (3)
- .inはインドのccTLDなのか (3)
- 散髪しました (3)
- やっと髪をきりました (3)
- Waterfallプロセスに返れ? (3)
- 大雪でした (3)
閲覧数が多いエントリ [Top 100]
1 . ドラゴンクエストVIの影響力 [8950x]
2 . やっぱりあった!パクれる読書感想文! [7596x]
3 . Echofon for Firefox [6372x]
4 . OpenIDで自分のサイトのURLを使う [5737x]
5 . 急性胃腸炎 [5724x]
2 . やっぱりあった!パクれる読書感想文! [7596x]
3 . Echofon for Firefox [6372x]
4 . OpenIDで自分のサイトのURLを使う [5737x]
5 . 急性胃腸炎 [5724x]
最新のエントリ [archives]
最新のコメント [Latest 100]
- CD-ROM起動で、HDDを完全消去
- NP_Moblog v1.16
- pinzoro 01/15
- hsur 12/29
- and more...
- 耳がおかしいと思ったら突発..
- baca 01/13
- hsur 01/13
- and more...
カテゴリ別エントリ
cles::blogについて
Syndicate
Calendar
Referrers
Blog People
Admin
★はてな認証APIをつかってログインすることができます。
Powered by NP_Paint




