example_exe.cxx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <kwsys/DynamicLoader.hxx>
  2. #include <example.h>
  3. #include <example_exe.h>
  4. #include <iostream>
  5. #include <string>
  6. #include <stdio.h>
  7. // Implement the ABI used by plugins.
  8. extern "C" int example_exe_function()
  9. {
  10. std::cout << "hello" << std::endl;
  11. return 123;
  12. }
  13. #ifdef CMAKE_INTDIR
  14. #define CONFIG_DIR "/" CMAKE_INTDIR
  15. #else
  16. #define CONFIG_DIR ""
  17. #endif
  18. int main()
  19. {
  20. std::string libName = EXAMPLE_EXE_PLUGIN_DIR CONFIG_DIR "/";
  21. libName += kwsys::DynamicLoader::LibPrefix();
  22. libName += "example_mod_1";
  23. libName += kwsys::DynamicLoader::LibExtension();
  24. kwsys::DynamicLoader::LibraryHandle handle =
  25. kwsys::DynamicLoader::OpenLibrary(libName.c_str());
  26. if (!handle) {
  27. // Leave the .c_str() on this one. It is needed on OpenWatcom.
  28. std::cerr << "Could not open plugin \"" << libName.c_str() << "\"!"
  29. << std::endl;
  30. return 1;
  31. }
  32. kwsys::DynamicLoader::SymbolPointer sym =
  33. kwsys::DynamicLoader::GetSymbolAddress(handle, "example_mod_1_function");
  34. if (!sym) {
  35. std::cerr << "Could not get plugin symbol \"example_mod_1_function\"!"
  36. << std::endl;
  37. return 1;
  38. }
  39. #ifdef __WATCOMC__
  40. int(__cdecl * f)(int) = (int(__cdecl*)(int))(sym);
  41. #else
  42. int (*f)(int) = reinterpret_cast<int (*)(int)>(sym);
  43. #endif
  44. if (f(456) != (123 + 456)) {
  45. std::cerr << "Incorrect return value from plugin!" << std::endl;
  46. return 1;
  47. }
  48. kwsys::DynamicLoader::CloseLibrary(handle);
  49. return 0;
  50. }