tutorial3.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2014 Renato Tegon Forti, Antony Polukhin.
  2. // Copyright 2015-2019 Antony Polukhin.
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #include "../b2_workarounds.hpp"
  8. //[callplugcpp_tutorial3
  9. #include <boost/dll/import.hpp> // for import_alias
  10. #include <boost/make_shared.hpp>
  11. #include <boost/function.hpp>
  12. #include <iostream>
  13. #include "../tutorial_common/my_plugin_api.hpp"
  14. namespace dll = boost::dll;
  15. std::size_t search_for_symbols(const std::vector<boost::dll::fs::path>& plugins) {
  16. std::size_t plugins_found = 0;
  17. for (std::size_t i = 0; i < plugins.size(); ++i) {
  18. std::cout << "Loading plugin: " << plugins[i] << '\n';
  19. dll::shared_library lib(plugins[i], dll::load_mode::append_decorations);
  20. if (!lib.has("create_plugin")) {
  21. // no such symbol
  22. continue;
  23. }
  24. // library has symbol, importing...
  25. typedef boost::shared_ptr<my_plugin_api> (pluginapi_create_t)();
  26. boost::function<pluginapi_create_t> creator
  27. = dll::import_alias<pluginapi_create_t>(boost::move(lib), "create_plugin");
  28. std::cout << "Matching plugin name: " << creator()->name() << std::endl;
  29. ++ plugins_found;
  30. }
  31. return plugins_found;
  32. }
  33. //]
  34. int main(int argc, char* argv[]) {
  35. BOOST_ASSERT(argc >= 3);
  36. std::vector<boost::dll::fs::path> plugins;
  37. plugins.reserve(argc - 1);
  38. for (int i = 1; i < argc; ++i) {
  39. if (b2_workarounds::is_shared_library(argv[i])) {
  40. plugins.push_back(argv[i]);
  41. }
  42. }
  43. const std::size_t res = search_for_symbols(plugins);
  44. BOOST_ASSERT(res == 1);
  45. (void)res;
  46. }