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