0

I'm trying to link my C++ program with static Boost::program_options and dynamic Boost::log libraries. Here is my CMakeList.txt file:

project(test)
add_executable(testprog "main.cpp")
add_definitions(-DBOOST_ALL_NO_LIB)

set(Boost_USE_STATIC_LIBS ON)
find_package(Boost COMPONENTS program_options)
target_link_libraries(testprog PRIVATE Boost::program_options)

set(Boost_USE_STATIC_LIBS OFF)
add_definitions(-DBOOST_LOG_DYN_LINK)
find_package(Boost COMPONENTS log)
target_link_libraries(testprog PRIVATE Boost::log)

and my main.cpp file:

#define BOOST_ALL_NO_LIB

#include <iostream>
#include <string>
#include <boost/program_options.hpp>
#include <boost/log/trivial.hpp>

int main(int argc, char* argv[])
{
  namespace po = boost::program_options;
  po::options_description desc("allowed arguments");
  desc.add_options()("log", po::value<std::string>(), "log level");
  po::variables_map options;
  po::store(po::command_line_parser(argc, argv).options(desc).run(), options);
  po::notify(options);
  if (options.count("log")) {
    std::cout << "log level: " << options["log"].as<std::string>() << std::endl;
  }
  BOOST_LOG_TRIVIAL(info) << "test message";
  return 0;
}

I'm getting boost_filesystem.dll is missing from your computer error. If I get rid of set(Boost_USE_STATIC_LIBS OFF) line, I'm getting unresolved external symbols boost::log.... Looks like Boost_USE_STATIC_LIBS can be set only once during the configuration time, but I just cannot find the way to tell cmake to split static/dynamic libraries...

Thanks.

  • 1
    Mixing static and dynamic Boost libraries in one project doesn't look as a good idea. And I see no purpose for that mix: Want minimal size of your project? - Use **all** dynamic linking. Want minimum runtime dependencies? Use **all** static linking. It is possible that `find_package` simply doesn't work with such mix. Note, that there is some dependency between Boost libraries. If Boost library `libraryA` depends from `libraryB`, then that dependency is integrated into shared version of `libraryA`. So, if you link with `libraryA` dynamically, then `libraryB` should also be linked dynamically. – Tsyvarev Dec 30 '20 at 09:25
  • I would like to use static linking for all boost libraries, but I can't because of Boost::log specifics. So I was thinking about keeping everything static while only Boost::log dynamic.I just realised that Boost::log can depend on Boost::filesystem, this can be the reason why I'm getting boost_filesystem.dll is missing error. Can someone confirm this? – Alexander Maltsev Dec 30 '20 at 12:41
  • You can check that by yourself by inspecting `.dll` for Boost lod library. See that question: https://stackoverflow.com/questions/7378959/how-to-check-for-dll-dependency. – Tsyvarev Dec 30 '20 at 13:21
  • Thanks, it really depends on boost::thread, boost::filesystem and boost::date_time. As I understand now, there is nothing wrong with my cmake file, if I want dynamic boost::log I must have the above dynamic libraries as well. And there is no much sense in keeping a few remaining boost libraries static. – Alexander Maltsev Dec 30 '20 at 13:40

0 Answers0