私が書いたプログラムの一種のプラグイン アーキテクチャを作成しようとしていますが、最初の試みで問題が発生しました。共有オブジェクト内からメインの実行可能ファイルからシンボルにアクセスすることは可能ですか? 以下でいいと思いました。
testlib.cpp:
void foo();
void bar() __attribute__((constructor));
void bar(){ foo(); }
testexe.cpp:
#include <iostream>
#include <dlfcn.h>
using namespace std;
void foo()
{
cout << "dynamic library loaded" << endl;
}
int main()
{
cout << "attempting to load" << endl;
void* ret = dlopen("./testlib.so", RTLD_LAZY);
if(ret == NULL)
cout << "fail: " << dlerror() << endl;
else
cout << "success" << endl;
return 0;
}
以下でコンパイル:
g++ -fPIC -o testexe testexe.cpp -ldl
g++ --shared -fPIC -o testlib.so testlib.cpp
出力:
attempting to load
fail: ./testlib.so: undefined symbol: _Z3foov
だから明らかに、それは大丈夫ではありません。したがって、2 つの質問があると思います: 1) 共有オブジェクトに、ロード元の実行可能ファイル内のシンボルを検索させる方法はありますか?プログラム内で実行するには?