Skip to content

trouble crosscompiling with threads (and dependencies) #4

@user706

Description

@user706

Hi,

thanks for your great toolchains and also howto-tutorials! It is very much appreciated.

I'm having some trouble crosscompiling a project that uses Boost::program_options and thread

I'm using the sbuild schroot setup (to get boost dependency) and have installed the needed packages:

sudo sbuild-apt rpizero-bullseye-armhf apt-get update
sudo sbuild-apt rpizero-bullseye-armhf apt-get install raspi-copies-and-fills libboost-program-options-dev

Then I try to compile with:

triple=armv8-rpi3-linux-gnueabihf         # Select the main toolchain
variant_triple=armv8-rpi4-linux-gnueabihf # Select a specific variant
# Configure
cmake -D CMAKE_BUILD_TYPE=Debug -D CMAKE_SYSROOT=/var/lib/schroot/chroots/rpizero-bullseye-armhf  -S . -B build-$variant_triple --toolchain ~/opt/x-tools/$triple/$variant_triple.toolchain.cmake
#                                             using the sbuild schroot sysroot - see above!!!
# Build
cmake --build build-$variant_triple -j

but get this error:

-- The CXX compiler identification is GNU 13.1.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /home/albert/opt/x-tools/armv8-rpi3-linux-gnueabihf/bin/armv8-rpi3-linux-gnueabihf-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Check if compiler accepts -pthread
-- Check if compiler accepts -pthread - no
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - not found
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindThreads.cmake:238 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
  CMakeLists.txt:5 (find_package)


-- Configuring incomplete, errors occurred!

As you can see, there is a problem with the threads part.

The CMakeLists.txt contains this

#...
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)
#...

How does one go about, getting this to work? Is some package missing from the schroot? Or something else?

As initial investigation.... what is interesting is that the following

find ~/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/ -name "*pthread*"

shows

/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/lib/libpthread.so.0
/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/lib/libpthread-2.27.so
/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/usr/lib/libpthread_nonshared.a
/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/usr/lib/libpthread.a
/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/usr/lib/libpthread.so
/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/usr/include/pthread.h
/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/usr/include/bits/pthreadtypes.h
/home/superman/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/usr/include/bits/pthreadtypes-arch.h

so the sysroot in your toolchain seems to have the required pthread files.

But I am using the other sysroot in the schroot (/var/lib/schroot/chroots/rpizero-bullseye-armhf), because I have boost dependencies.

Checking the actually used sysroot (sbuild- schroot) for pthread:

sudo find /var/lib/schroot/chroots/rpizero-bullseye-armhf/  -name "*pthread*"

shows

/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/lib/arm-linux-gnueabihf/libpthread.a
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/lib/arm-linux-gnueabihf/libpthread.so.0
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/lib/arm-linux-gnueabihf/libpthread-2.31.so
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/lib/arm-linux-gnueabihf/perl/5.32.1/bits/pthreadtypes-arch.ph
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/lib/arm-linux-gnueabihf/perl/5.32.1/bits/pthreadtypes.ph
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/lib/arm-linux-gnueabihf/libpthread.so
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/share/gdb/auto-load/lib/arm-linux-gnueabihf/libpthread-2.31.so-gdb.py
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/boost/thread/pthread
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/boost/thread/pthread/pthread_helpers.hpp
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/boost/thread/pthread/pthread_mutex_scoped_lock.hpp
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/boost/smart_ptr/detail/lwm_pthreads.hpp
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/boost/interprocess/sync/posix/pthread_helpers.hpp
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/boost/signals2/detail/lwm_pthreads.hpp
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/arm-linux-gnueabihf/bits/pthreadtypes.h
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/arm-linux-gnueabihf/bits/pthreadtypes-arch.h
/var/lib/schroot/chroots/rpizero-bullseye-armhf/usr/include/pthread.h

Seems it also has pthread. Hmmm....

Click here for code, that one can try and cross-compile

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(go VERSION 0.1.0 LANGUAGES CXX)

set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads REQUIRED)

find_package(Boost REQUIRED COMPONENTS program_options)

add_executable(go main.cpp)
target_link_libraries(go PRIVATE Boost::program_options Threads::Threads)

include(GNUInstallDirs)
install(TARGETS go
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})

main.cpp

#include <boost/program_options.hpp>
namespace po = boost::program_options;
#include <iostream>
#include <thread>

int main(int argc, char *argv[]) {
    try {
        // Define and parse the command line arguments
        po::options_description desc("Options");
        desc.add_options() // clang-format off
            ("help", "Print usage information and exit")
            ("sec", po::value<unsigned>(), "The number of seconds to run a thread")
        ; // clang-format on
        po::variables_map vm;
        po::store(po::parse_command_line(argc, argv, desc), vm);
        po::notify(vm);

        // Print a usage message if the --help flag was specified
        if (vm.count("help")) {
            std::cout << "Usage: " << argv[0] << " [options]\n\n"
                      << "Run a thread for a particular number of seconds.\n\n"
                      << desc << "\n";
            return 0;
        }

        // If the --sec option was specified, use that, otherwise use 1 second
        unsigned sec = 1;
        if (vm.count("sec")) {
            sec = vm["sec"].as<unsigned>();
        }
        std::cout << "Running a thread for " << sec << " seconds" << std::endl;

        std::thread th([sec]() {
                         std::this_thread::sleep_for(std::chrono::seconds(sec));
                       });

        th.join();
    } catch (std::exception &e) {
        // Print any errors we may run into (e.g. unrecognized options)
        std::cerr << "error: " << e.what() << std::endl;
        return 1;
    }

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions