0

I recently changed to Linux Mint Debian Edition and cannot include boost to Clion using cmake. Yesterday I tried every option I could find, including on stackoverflow, but nothing has worked yet.

Boost has been installed using: sudo apt -y install libboost-filesystem-dev

This is a version I found in this thread: How to include external library (boost) into CLion C++ project with CMake? which supposedly worked, but it didn't.

cmake_minimum_required(VERSION 3.22)
project(boost_project)

set(CMAKE_CXX_STANDARD 14)

find_package(Boost COMPONENTS system filesystem REQUIRED)

if(Boost_FOUND)

    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")

    include_directories(${Boost_INCLUDE_DIRS})

endif()

add_executable(boost_project main.cpp)


if(Boost_FOUND)

    target_link_libraries(boost_project ${Boost_LIBRARIES})

endif()

The Cmake output message is: CMake Error at /app/extra/clion/bin/cmake/linux/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message): Could NOT find Boost (missing: Boost_INCLUDE_DIR system filesystem)

I've gone through dozens of threads but so far nothing has worked. Has anyone suggestions of what's wrong?


EDIT: So I've de-installed Clion and re-installed it with snap, since I read in another thread that it was an issue with installing Clion through the software center. Now I can include boost with no problem, but building it reveals the compiler still doesn't like using it.

#include <iostream>
#include "boost/asio.hpp"
boost::asio::io_service io_s;
int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

: && /usr/bin/c++ -g CMakeFiles/BoostTest.dir/main.cpp.o -o BoostTest && : /usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_event::posix_event()': /usr/include/boost/asio/detail/impl/posix_event.ipp:42: undefined reference to pthread_condattr_setclock' /usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::~posix_thread()': /usr/include/boost/asio/detail/impl/posix_thread.ipp:35: undefined reference to pthread_detach' /usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::join()': /usr/include/boost/asio/detail/impl/posix_thread.ipp:42: undefined reference to pthread_join' /usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_thread::start_thread(boost::asio::detail::posix_thread::func_base*)': /usr/include/boost/asio/detail/impl/posix_thread.ipp:59: undefined reference to pthread_create' /usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_signal_blocker::posix_signal_blocker()': /usr/include/boost/asio/detail/posix_signal_blocker.hpp:43: undefined reference to pthread_sigmask' /usr/bin/ld: CMakeFiles/BoostTest.dir/main.cpp.o: in function boost::asio::detail::posix_signal_blocker::~posix_signal_blocker()': /usr/include/boost/asio/detail/posix_signal_blocker.hpp:50: undefined reference to pthread_sigmask' collect2: error: ld returned 1 exit status ninja: build stopped: subcommand failed.

Lucia
  • 11
  • 3
  • You can try to set the variable BOOST_ROOT to your installation directory of boost: set(BOOST_ROOT "path/to/boost"). Also please use target_include_directories() and not include_directories() – CodingWithMagga May 03 '22 at 12:37
  • I aded set(BOOST_ROOT "/usr/include/boost") before "find_package" and exchanged include_directories with include_directories. Nothing changed sadly. – Lucia May 03 '22 at 14:31
  • I'm uncertain if this is the correct directory for BOOST_ROOT. On my Ubuntu 20.04 system, I get "/usr/include" for Boost_INCLUDE_DIRS and "/usr/lib/x86_64-linux-gnu" for Boost_LIBRARY_DIRS after finding Boost. You could try to set the variables BOOST_INCLUDEDIR and BOOST_LIBRARYDIR to these paths (or similar paths on your system) and see if that helps. But it would be strange if that is the case, since CMake should search these paths itself. I assume that you have checked that the boost files are there on your system? – CodingWithMagga May 03 '22 at 15:34
  • Also, as Sidenote: Exchanging include_directories with target_include_directories has nothing to do with the find_package command, it is just a more modern (and better) cmake command. – CodingWithMagga May 03 '22 at 15:36
  • thanks for the help so far. The files are in the exact paths that you wrote. set(BOOST_INCLUDEDIR "/usr/include") set(BOOST_LIBRARYDIR "/usr/lib/x86_64-linux-gnu") indeed didn't help. It's still saying Could NOT find Boost (missing: Boost_INCLUDE_DIR) – Lucia May 03 '22 at 17:07
  • CMake just prints (missing: Boost_INCLUDE_DIR) now instead of (missing: Boost_INCLUDE_DIR system filesystem)? If thats the case we are one step further. Then just the include path seems still to be wrong. I have to check a few things on my system tomorrow. – CodingWithMagga May 03 '22 at 20:10
  • It looks like that Boost looks for the files boost/version.hpp and boost/config.hpp in the include directory, which is in our case "/usr/include". Are both of these files on your system? – CodingWithMagga May 04 '22 at 08:19
  • yes, both files are in "/usr/include". – Lucia May 04 '22 at 16:02

1 Answers1

1

The solution to the first problem, cmake not finding boost, had nothing to do with cmake but Clion. Installing Clion with flatpak can apparently cause this error. To fix it I just needed to de-install Clion and re-install it with snap.

The second issue was cmake not finding several thread related includes. For that cmake needed

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

to be added. I found this solution in these threads: linking errors for boost for visual studio linux project Undefined reference to pthread_create in Linux

The complete and working cmake looks like this now:

cmake_minimum_required(VERSION 3.22)
project(boost_project)

message(STATUS "start running cmake...")

find_package(Boost COMPONENTS system)

if(Boost_FOUND)

    message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
    message(STATUS "Boost_VERSION: ${Boost_VERSION}")

    set(CMAKE_CXX_STANDARD 14)
    add_executable(boost_project main.cpp)

    include_directories(${Boost_INCLUDE_DIR})
    link_directories(${Boost_LIBRARY_DIR})

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")

endif()

target_link_libraries(boost_project ${Boost_LIBRARIES})
Lucia
  • 11
  • 3