Poco::SharedLibrary を紹介します。
Poco::SharedLibrary クラスは、Shared Library を実行時に動的にロードします。
SharedLibrary.cpp
・Windows 環境の場合、hello() を export。
・これを libSharedLibrary という名前の SharedLibrary としてビルド。
#include <string> #if defined(_WIN32) #define LIBRARY_API __declspec(dllexport) #else #define LIBRARY_API #endif extern "C" void LIBRARY_API hello(std::string& str); void hello(std::string& str) { str = "Hello SharedLibrary world!"; } |
SharedLibraryTest.cpp
・libSharedLibrary という名前の SharedLibrary をロードして、hello という関数をコールし、戻された
文字列を表示。
#include <Poco/Format.h> #include <Poco/SharedLibrary.h> #include <string> #include "ScopedLogMessage.h" #include "PrepareConsoleLogger.h" typedef void (*HelloFunc)(std::string& str); // function pointer type const std::string kSharedLibraryName("libSharedLibrary"); const std::string kFunctionName("hello"); int main(int /*argc*/, char** /*argv*/) { PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION); ScopedLogMessage msg("SharedLibraryTest ", "start", "end"); std::string path(kSharedLibraryName); path.append(Poco::SharedLibrary::suffix()); Poco::SharedLibrary library; try { library.load(path); if(library.isLoaded()) { msg.Message(Poco::format(" \"%s\" is loaded", path)); std::string str; try { HelloFunc func = reinterpret_cast<HelloFunc>(library.getSymbol(kFunctionName)); func(str); library.unload(); msg.Message(Poco::format(" %s", str)); } catch(Poco::NotFoundException& exc) { msg.Message(Poco::format(" %s \"%s\"", std::string(exc.name()), kFunctionName)); } } } catch(Poco::LibraryLoadException& exc) { msg.Message(Poco::format(" %s \"%s\"", std::string(exc.name()), path)); } return 0; } |
Results of execution
・Linux/Mac 等では、libSharedLibrary をインストールするために make install してから SharedLibraryTest
を実行する。
– On Linux
[0] SharedLibraryTest start [0] "libSharedLibrary.so" is loaded [0] Hello SharedLibrary world! [0] SharedLibraryTest end |
– On Macintosh OSX 10.6.4
[0] SharedLibraryTest start [0] "libSharedLibrary.dylib" is loaded [0] Hello SharedLibrary world! [0] SharedLibraryTest end |
– On Windows XP sp3
[0] SharedLibraryTest start [0] "libSharedLibrary.dll" is loaded [0] Hello SharedLibrary world! [0] SharedLibraryTest end |
Downloads
・ここをクリックすると、makefile や VC++ プロジェクトなど一式がダウンロードできます。
(2013.05.31 updated)
・2010年7月25日からのダウンロード数:1054
Subversion
・フリーの Subversion ホスティングサービス Assemblaで、ソースコードを管理しています。
Reference
・http://pocoproject.org にある Shared Libraries のプレセンテーション。(PDF)
![]() |
Copyright © 2010 Round Square Inc. All rights reserved. |
---|
0 Comments.