dlopen() 관련 질문입니다.

superkkt의 이미지

안녕하세요.

A 프로그램이 공유라이브러리 B를 dlopen()으로 동적 로딩했을때, B에서 A에 있는 함수를 호출 할 수 있나요?

#include <stdio.h>
#include <dlfcn.h>
 
int test(void)
{
    printf("XX\n");
}
 
int
main(void)
{
    void (*func)(int) = NULL;
    void *handler = NULL;
    int *ptr = NULL;
 
    handler = dlopen("./libdl.so", RTLD_GLOBAL | RTLD_NOW);
    func = dlsym(handler, "func");
    ptr = dlsym(handler, "value");
    (*func)(*ptr);
 
    return 0;
}

#include <stdio.h>
 
int value = 5;
 
void
func(int num)
{
    printf("%d\n", num);
    test();
}

두번째 코드에서 test()를 빼면 잘 되는데 test()가 들어가니 제대로 작동을 안하는군요. 원래 이게 불가능한가요? 그럼 플러그인으로 개발되는 코드들은 자신을 로딩하는 상위 프로그램에 있는 루틴을 하나도 사용하지 못하고 모든걸 플러그인이 직접 구현해서 처리해야 하나요?

lovian의 이미지

A 프로그램에서 특정 서비스를 하위 라이브러리에 제공하려면 해당 기능에 대한 포인터를 넘기면 됩니다.
포인터를 넘기도록 수정이 필요하겠죠

A program
(*func)( test, *ptr );
 
B library
func( int (*test)(void), int num)
{
(*test);
}

문법은 맞나 모르겠습니다...;;

하여간 이런식으로 포인터를 넘겨 사용할 수 있구요,
제공할 기능이 많다면, 포인터 배열을 생성하여 넘길 수도 있습니다.

-----------------
한글을 사랑합니다.

-----------------
한글을 사랑합니다.

superkkt의 이미지

답변 감사합니다. 직접 호출은 안되나보군요..

======================
BLOG : http://superkkt.com

======================
BLOG : http://superkkt.com

bootmeta의 이미지

동적 라이브러리 개발 시 주의 해야할 점은 위와 같이 상호 참조하는 부분은 피해야 하고, 굳이 상호 참조가 필요하다면 제 3의 모듈에서 두 모듈을 참조하게 구성하는 것을 권장합니다.
그리고 한 라이브러리에서 heap에 메모리를 잡는 경우 해제하는 루틴도 같은 라이브러리에 있어야 합니다.(최소한 윈도우즈에서는 그랬던 기억이)

일반적으로 플러그인을 지원하는 프로그램들은 보통 플러그인등에서 접근 가능한 외부로 노출된 함수들은 최대한 독립적으로, 계층적인 동적 라이브러리로 개발됩니다.

언제나 그랬듯이 권장 사항이지 필수 사항은 아닙니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.