差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
shlib [2010/06/26 15:36] – 作成 Takuya Nishimotoshlib [Unknown date] (現在) – 外部編集 (Unknown date) 127.0.0.1
行 1: 行 1:
 ====== 動的ライブラリ ====== ====== 動的ライブラリ ======
  
-[[c_language|C言語]](gcc)で動的ライブラリを実装し、C言語および[[Python]]言語から呼び出す例を示す。+[[c_language|C言語]](gcc)で動的ライブラリ(shlib)を実装し、C言語および[[Python]]言語から呼び出す例を示す。
  
-<code makefile+資料  
-#! shlibtest+  * http://www.linux.or.jp/JF/JFdocs/Program-Library-HOWTO/index.html 
 +  * http://www.python.jp/doc/2.5/lib/ctypes-ctypes-tutorial.html 
 + 
 +[[cygwin]] gcc 4.3.4 および python 2.5.2 で動作を確認した。 
 + 
 +<code> 
 +Makefile
  
 target: libmystuff main target: libmystuff main
行 16: 行 22:
 main: main.c main: main.c
  gcc main.c -o main -ldl  gcc main.c -o main -ldl
-<code>+</code> 
 + 
 +<code c> 
 +/* a.c */ 
 +int a(int x, int y) 
 +
 +    return x + y; 
 +
 +</code> 
 + 
 +<code c> 
 +/* b.c */ 
 +int b(int x, int y) 
 +
 +    return x - y; 
 +
 +</code> 
 + 
 +<code c> 
 +/* str.c */ 
 +#include <string.h> 
 + 
 +char *cat(char *a, char *b) 
 +
 +    return strcat(a, b); 
 +
 +</code> 
 + 
 + 
 +<code c> 
 +/* main.c */ 
 +#include <stdlib.h> 
 +#include <stdio.h> 
 +#include <dlfcn.h> 
 +#include <string.h> 
 + 
 +int main(void) 
 +
 +    void *handle; 
 +    int (*func_a)(int, int); 
 +    int (*func_b)(int, int); 
 +    char *(*func_cat)(char *, char *); 
 +    char *error; 
 +    char buf1[10], buf2[10]; 
 + 
 +    handle = dlopen("libmystuff.so.1.0.1", RTLD_LAZY); 
 +    if (!handle) { 
 +        fputs (dlerror(), stderr); 
 +        exit(1); 
 +    } 
 + 
 +    func_a = dlsym(handle, "a"); 
 +    if ((error = dlerror()) != NULL)  { 
 +        fputs(error, stderr); 
 +        exit(1); 
 +    } 
 + 
 +    func_b = dlsym(handle, "b"); 
 +    if ((error = dlerror()) != NULL)  { 
 +        fputs(error, stderr); 
 +        exit(1); 
 +    } 
 + 
 +    func_cat = dlsym(handle, "cat"); 
 +    if ((error = dlerror()) != NULL)  { 
 +        fputs(error, stderr); 
 +        exit(1); 
 +    } 
 + 
 +    printf("a %d\n", (*func_a)(3,2)); 
 +    printf("b %d\n", (*func_b)(3,2)); 
 +     
 +    strcpy(buf1, "a"); 
 +    strcpy(buf2, "b"); 
 +    printf("cat %s\n", (*func_cat)(buf1, buf2)); 
 +    dlclose(handle); 
 +    return 0; 
 +
 +</code> 
  
 +<code python>
 +# main.py
 +from ctypes import *
 +h = cdll.LoadLibrary("libmystuff.so.1.0.1")
 +print h.a(3,2)
 +print h.b(3,2)
 +s1 = create_string_buffer('a', 10)
 +print c_char_p(h.cat(s1,'b')).value
 +</code>
  
shlib.1277534178.txt.gz · 最終更新: 2010/06/26 06:36 (外部編集)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0