Skip to Content
21st Century C, 2nd Edition
book

21st Century C, 2nd Edition

by Ben Klemens
May 2025
Intermediate to advanced
408 pages
5h 55m
Japanese
O'Reilly Media, Inc.
Content preview from 21st Century C, 2nd Edition

第5章. 他人とうまくやる

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

プログラミング言語のカウントは無限大に近づいており、その膨大な数がCインタフェースを備えている。この短い章では、そのプロセスに関する一般化されたメモを提供し、1つの言語Pythonとのインタフェースを詳細に示す。

つまり、C言語とホスト言語でブリッジ・コードを書いた後は、パッケージング・システムにすべてをコンパイル・リンクさせる作業に直面することになる。これは、サブディレクトリを条件付きで処理したり、インストール・フックを追加したりといった、Autotoolsのより高度な機能を紹介する機会を与えてくれる。

ダイナミック・ローディング

他の言語に飛びつく前に、これを可能にしているCの関数(dlopendlsym )を少し理解する価値がある。これらの関数はダイナミック・ライブラリを開き、そのライブラリから静的オブジェクトや関数などのシンボルを取り出す。

これらの関数はPOSIX標準の一部である。Windowsシステムにも同様の設定があるが、関数の名前はLoadLibraryGetProcAddress である。説明を簡単にするため、POSIXの名前にこだわることにする。

このようなファイルには、関数や静的に定義された構造体を含むオブジェクトのリストが含まれており、他のプログラムで使用することが意図されている。

このようなファイルを使うのは、項目のリストを保持するテキスト・ファイルから項目を取り出すのとよく似ている。テキスト・ファイルの場合、まずfopen を呼び出してファイルのハンドルを取得し、次に適切な関数を呼び出してファイルを検索し、発見した項目へのポインタを返す。共有オブジェクトファイルの場合、ファイルを開く関数はdlopen であり、目的のシンボルを検索する関数はdlsym である。マジックは、返されたポインタで何ができるかにある。テキスト項目のリストでは、プレーン・テキストへのポインタを持ち、それを使って通常のテキスト処理を行うことができる。dlsym 、関数へのポインタを取得した場合は、その関数を呼び出すことができる。構造体へのポインタを取得した場合は、その構造体を初期化済みのオブジェクトとしてすぐに使うことができる。

Cプログラムがリンクされたライブラリーにある関数を呼び出すと、このようにしてその関数が取り出され使用される。プラグイン・システムを持つプログラムは、メイン関数が出荷された後、異なる作者によって書かれた関数をロードするためにこのようなことを行っている。Cコードを呼び出したいスクリプト言語は、同じdlopendlsym 関数を呼び出してそれを行う。

dlopen/dlsym で何ができるかを示すために、例5-1はCインタプリタの始まりである:

  1. ユーザにC関数のコードを入力させる。

  2. 関数を共有オブジェクトファイルにコンパイルする。

  3. を介して共有オブジェクトファイルをロードする。dlopen

  4. を介して関数を取得する。dlsym

  5. ユーザが入力した関数を実行する。

以下はそのサンプルである:

 I am about to run a function. But first, you have to write it for me. Enter the function body. Conclude with a '}' alone on ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learn C Programming - Second Edition

Learn C Programming - Second Edition

Jeff Szuhay
Tiny C Projects

Tiny C Projects

Daniel Gookin

Publisher Resources

ISBN: 9798341650848