0

Consider the following minimal example: CMakeLists.txt contains:

cmake_minimum_required(VERSION 3.22)
project(Minexample)

find_package(ZLIB)
if(ZLIB_FOUND)
  add_definitions(-DHAVE_LIBZ)
  include_directories(${ZLIB_INCLUDE_DIR})
endif()

# tell cmake to search boost in the directory /home/hiisi/workspace/boost-1_75_0
set(Boost_NO_SYSTEM_PATHS ON)
set(BOOST_ROOT "/home/hiisi/workspace/boost-1_75_0")
set(Boost_INCLUDE_DIR "/home/hiisi/workspace/boost-1_75_0")
#set(BOOST_LIBRARYDIR "/home/hiisi/workspace/boost-1_75_0/lib")
set(Boost_USE_STATIC_LIBS ON)
set(Boost_DEBUG ON)
#set(Boost_COMPILER -emscripten-3.1.24)
#set(Boost_toolset emscripten-3.1.24)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_DETAILED_FAILURE_MSG ON)
Set(Boost_LIB_PREFIX "lib")
find_package(Boost COMPONENTS program_options iostreams system)
#find_package(Boost)
  if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS} ${Boost_INCLUDE_DIR})
 endif()

This is how I feed it to make: emcmake cmake . -DZLIB_INCLUDE_DIR=/home/hiisi/workspace/zlib -DZLIB_LIBRARY=/home/hiisi/workspace/zlib/libz.a

Here's the output of the above command:

configure: cmake . -DZLIB_INCLUDE_DIR=/home/hiisi/workspace/zlib -DZLIB_LIBRARY=/home/hiisi/workspace/zlib/libz.a -DCMAKE_TOOLCHAIN_FILE=/home/hiisi/workspace/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR=/home/hiisi/workspace/emsdk/node/14.18.2_64bit/bin/node;--experimental-wasm-threads
-- Found ZLIB: /home/hiisi/workspace/zlib/libz.a (found version "1.2.13") 
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1688 ] _boost_TEST_VERSIONS = "1.78.0;1.78;1.77.0;1.77;1.76.0;1.76;1.75.0;1.75;1.74.0;1.74;1.73.0;1.73;1.72.0;1.72;1.71.0;1.71;1.70.0;1.70;1.69.0;1.69;1.68.0;1.68;1.67.0;1.67;1.66.0;1.66;1.65.1;1.65.0;1.65;1.64.0;1.64;1.63.0;1.63;1.62.0;1.62;1.61.0;1.61;1.60.0;1.60;1.59.0;1.59;1.58.0;1.58;1.57.0;1.57;1.56.0;1.56;1.55.0;1.55;1.54.0;1.54;1.53.0;1.53;1.52.0;1.52;1.51.0;1.51;1.50.0;1.50;1.49.0;1.49;1.48.0;1.48;1.47.0;1.47;1.46.1;1.46.0;1.46;1.45.0;1.45;1.44.0;1.44;1.43.0;1.43;1.42.0;1.42;1.41.0;1.41;1.40.0;1.40;1.39.0;1.39;1.38.0;1.38;1.37.0;1.37;1.36.1;1.36.0;1.36;1.35.1;1.35.0;1.35;1.34.1;1.34.0;1.34;1.33.1;1.33.0;1.33"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1689 ] Boost_USE_MULTITHREADED = "ON"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1690 ] Boost_USE_STATIC_LIBS = "ON"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1691 ] Boost_USE_STATIC_RUNTIME = "ON"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1692 ] Boost_ADDITIONAL_VERSIONS = <unset>
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1693 ] Boost_NO_SYSTEM_PATHS = "ON"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1725 ] BOOST_ROOT = "/home/hiisi/workspace/boost-1_75_0"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1726 ] ENV{BOOST_ROOT} = <unset>
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1727 ] BOOST_INCLUDEDIR = <unset>
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1728 ] ENV{BOOST_INCLUDEDIR} = <unset>
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1729 ] BOOST_LIBRARYDIR = <unset>
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1730 ] ENV{BOOST_LIBRARYDIR} = <unset>
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1818 ] location of version.hpp: /home/hiisi/workspace/boost-1_75_0/boost/version.hpp
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1858 ] Boost_VERSION = "1.75.0"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1859 ] Boost_VERSION_STRING = "1.75.0"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1860 ] Boost_VERSION_MACRO = "107500"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1861 ] Boost_VERSION_MAJOR = "1"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1862 ] Boost_VERSION_MINOR = "75"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1863 ] Boost_VERSION_PATCH = "0"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1864 ] Boost_VERSION_COUNT = "3"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1888 ] Boost_LIB_PREFIX = "lib"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1889 ] Boost_NAMESPACE = "boost"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:953 ] _boost_COMPILER = "-clang16" (guessed)
-- [ /usr/share/cmake/Modules/FindBoost.cmake:1925 ] _boost_MULTITHREADED = "-mt"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2003 ] _boost_ARCHITECTURE_TAG = "" (detected)
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2007 ] _boost_RELEASE_ABI_TAG = "-s"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2008 ] _boost_DEBUG_ABI_TAG = "-sd"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2068 ] _boost_LIBRARY_SEARCH_DIRS_RELEASE = "/home/hiisi/workspace/boost-1_75_0/lib;/home/hiisi/workspace/boost-1_75_0/stage/lib;/home/hiisi/workspace/boost-1_75_0/lib;/home/hiisi/workspace/boost-1_75_0/../lib;/home/hiisi/workspace/boost-1_75_0/stage/lib;NO_CMAKE_SYSTEM_PATH;NO_SYSTEM_ENVIRONMENT_PATH"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2069 ] _boost_LIBRARY_SEARCH_DIRS_DEBUG = "/home/hiisi/workspace/boost-1_75_0/lib;/home/hiisi/workspace/boost-1_75_0/stage/lib;/home/hiisi/workspace/boost-1_75_0/lib;/home/hiisi/workspace/boost-1_75_0/../lib;/home/hiisi/workspace/boost-1_75_0/stage/lib;NO_CMAKE_SYSTEM_PATH;NO_SYSTEM_ENVIRONMENT_PATH"
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2254 ] Searching for PROGRAM_OPTIONS_LIBRARY_RELEASE: libboost_program_options-clang16-mt-s-1_75;libboost_program_options-clang16-mt-s;libboost_program_options-clang16-mt-s;libboost_program_options-mt-s-1_75;libboost_program_options-mt-s;libboost_program_options-mt-s;libboost_program_options-mt;libboost_program_options
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2309 ] Searching for PROGRAM_OPTIONS_LIBRARY_DEBUG: libboost_program_options-clang16-mt-sd-1_75;libboost_program_options-clang16-mt-sd;libboost_program_options-clang16-mt-sd;libboost_program_options-mt-sd-1_75;libboost_program_options-mt-sd;libboost_program_options-mt-sd;libboost_program_options-mt;libboost_program_options
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2254 ] Searching for IOSTREAMS_LIBRARY_RELEASE: libboost_iostreams-clang16-mt-s-1_75;libboost_iostreams-clang16-mt-s;libboost_iostreams-clang16-mt-s;libboost_iostreams-mt-s-1_75;libboost_iostreams-mt-s;libboost_iostreams-mt-s;libboost_iostreams-mt;libboost_iostreams
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2309 ] Searching for IOSTREAMS_LIBRARY_DEBUG: libboost_iostreams-clang16-mt-sd-1_75;libboost_iostreams-clang16-mt-sd;libboost_iostreams-clang16-mt-sd;libboost_iostreams-mt-sd-1_75;libboost_iostreams-mt-sd;libboost_iostreams-mt-sd;libboost_iostreams-mt;libboost_iostreams
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2254 ] Searching for SYSTEM_LIBRARY_RELEASE: libboost_system-clang16-mt-s-1_75;libboost_system-clang16-mt-s;libboost_system-clang16-mt-s;libboost_system-mt-s-1_75;libboost_system-mt-s;libboost_system-mt-s;libboost_system-mt;libboost_system
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2309 ] Searching for SYSTEM_LIBRARY_DEBUG: libboost_system-clang16-mt-sd-1_75;libboost_system-clang16-mt-sd;libboost_system-clang16-mt-sd;libboost_system-mt-sd-1_75;libboost_system-mt-sd;libboost_system-mt-sd;libboost_system-mt;libboost_system
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2254 ] Searching for REGEX_LIBRARY_RELEASE: libboost_regex-clang16-mt-s-1_75;libboost_regex-clang16-mt-s;libboost_regex-clang16-mt-s;libboost_regex-mt-s-1_75;libboost_regex-mt-s;libboost_regex-mt-s;libboost_regex-mt;libboost_regex
-- [ /usr/share/cmake/Modules/FindBoost.cmake:2309 ] Searching for REGEX_LIBRARY_DEBUG: libboost_regex-clang16-mt-sd-1_75;libboost_regex-clang16-mt-sd;libboost_regex-clang16-mt-sd;libboost_regex-mt-sd-1_75;libboost_regex-mt-sd;libboost_regex-mt-sd;libboost_regex-mt;libboost_regex
-- Could NOT find Boost (missing: program_options iostreams system) (found version "1.75.0")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hiisi/workspace/minexmpl

Here's the directory listing of /home/hiisi/workspace/boost-1_75_0/lib:

cmake  libboost_filesystem.a  libboost_filesystem.bc  libboost_iostreams.a  libboost_iostreams.bc  libboost_program_options.a  libboost_program_options.bc

Those .a files were created by running emar q lib.a lib.bc command. Why is it that my zlib is being recognized but not boost? This is how I compiled boost: emconfigure ./b2 toolset=emscripten link=static runtime-link=static threading=multi --prefix=/home/hiisi/workspace/boost-1_75_0 --with-program_options --with-iostreams --with-filesystem

And:

/b2 install --prefix=/home/hiisi/workspace/boost-1_75_0

Larry Kosher
  • 323
  • 1
  • 11
  • 1
    "Why is it that my zlib is being recognized" - You set variable `ZLIB_LIBRARY` in the command line, so CMake assumes it to be corresponding to the library file without even checking it. However, according to the output from `Boost_DEBUG`, Boost libraries should be found as well... I am not familiar with emscripten and its environment, but the only things I could think are some specific prefixes or suffixes for the library files or some sort of sysroot setting. – Tsyvarev Nov 17 '22 at 08:51
  • 1
    Instead of setting of `ZLIB_INCLUDE_DIR` and `ZLIB_LIBRARY` variables you could try to set `ZLIB_ROOT` variable to `/home/hiisi/workspace/zlib` and check whether CMake will succesfully find zlib. (Before that experiment make sure to erase CMake cache by removing `CMakeCache.txt` from the build directory.) – Tsyvarev Nov 17 '22 at 08:54
  • @Tsyvarev Great debugging advice, thank you! You are right. I did the following experiment. Just setting `ZLIB_ROOT` and/or `ZLIB_INCLUDE_DIR` is not enough. Only after setting `libz.a` location via `ZLIB_LIBRARY` cmake was able to `find_package(ZLIB)`. Now, the challenge is to reproduce the same with boost components. – Larry Kosher Nov 17 '22 at 13:20
  • 1
    Reproducing the problem with zlib has the advantage, since the Find script for that library is much simpler than one for Boost. You could pass additional option `--debug-find` for `cmake` and inspect the output from searching zlib. Here CMake should print actual paths where it searches for the library. Using these paths and other debugging information you could try to find the reason of non-found library. (While `Boost_DEBUG` prints some paths about searching the Boost, these paths are actually a content of some Boost-specific variables. But `find_package` is affected by other variables too.) – Tsyvarev Nov 17 '22 at 13:36
  • @Tsyvarev That was it! Using `--debug-find!` I was able to locate the problem. It turned out emscripten looks for actual libraries inside of `/home/hiisi/workspace/emsdk/upstream/emscripten/cache/sysroot/home/hiisi/workspace/lib/(lib)boost_system.a`, not just `/home/hiisi/workspace/lib/(lib)boost_system.a`. Creating symlinks solved the issue. Respond with the answer please (i.e. not a comment), I will select it . – Larry Kosher Nov 17 '22 at 16:42
  • 1
    Oh, so compiling for emscripten is actually a **cross-compiling** with `CMAKE_FIND_ROOT_PATH` variable being set. This is the reason why directories intended to hint `find_path`/`find_library` are prepended with some "root" directory. See [that question](https://stackoverflow.com/questions/35791251/possible-causes-for-boost-not-being-found-by-cmake-in-certain-situations) and its answers for more info. – Tsyvarev Nov 17 '22 at 16:56

1 Answers1

0

you can try this:

find_package(Boost 1.75 REQUIRED COMPONENTS
program_options
iostreams
system)
if (NOT Boost_FOUND)
    message("Not found Boost!")
elseif (Boost_FOUND)
    message("Found Boost!""  Version:" ${Boost_VERSION})
    include_directories(${Boost_INCLUDE_DIR})
    target_link_libraries(${PROJECT_NAME} ${Boost_LIBRARIES})
endif() 

And this is a link about CMake FindBoost.

Tom
  • 177
  • 7
  • ```CMake Error at /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message): Could NOT find Boost (missing: program_options iostreams system) (found suitable version "1.75.0", minimum required is "1.75") Call Stack (most recent call first): /usr/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE) /usr/share/cmake/Modules/FindBoost.cmake:2376 (find_package_handle_standard_args) CMakeLists.txt:19 (find_package)``` – Larry Kosher Nov 17 '22 at 03:24
  • Thanks for the answer, but that's another way of doing the same. Maybe I should recompile boost somehow specifically for cmake to be able to find it? – Larry Kosher Nov 17 '22 at 03:26
  • From the CMake Error message, maybe the CMake has found the boost(version: 1.75.0), but has not found the components that you need? I think your boost library has not fully installed, you may try to reinstall the boost again. – Tom Nov 17 '22 at 06:37