tutorial7.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 <vector>
  8. #include "../b2_workarounds.hpp"
  9. //[callplugcpp_tutorial7
  10. #include <boost/dll/shared_library.hpp>
  11. #include <boost/dll/library_info.hpp>
  12. #include <iostream>
  13. void load_and_execute(const boost::dll::fs::path libraries[], std::size_t libs_count) {
  14. const std::string username = "User";
  15. for (std::size_t i = 0; i < libs_count; ++i) {
  16. // Class `library_info` can extract information from a library
  17. boost::dll::library_info inf(libraries[i]);
  18. // Getting symbols exported from 'Anna' section
  19. std::vector<std::string> exports = inf.symbols("Anna");
  20. // Loading library and importing symbols from it
  21. boost::dll::shared_library lib(libraries[i]);
  22. for (std::size_t j = 0; j < exports.size(); ++j) {
  23. std::cout << "\nFunction '" << exports[j] << "' prints:\n\t";
  24. lib.get_alias<void(const std::string&)>(exports[j]) // importing function
  25. (username); // calling function
  26. }
  27. }
  28. }
  29. //]
  30. int main(int argc, char* argv[]) {
  31. /*<-*/ BOOST_ASSERT(argc >= 3); /*->*/
  32. std::vector<boost::dll::fs::path> libraries;
  33. libraries.reserve(argc - 1);
  34. for (int i = 1; i < argc; ++i) {
  35. if (b2_workarounds::is_shared_library(argv[i])) {
  36. libraries.push_back(argv[i]);
  37. }
  38. }
  39. load_and_execute(&libraries[0], libraries.size());
  40. }