tttapa / docker-arm-cross-toolchain

Modern GCC cross-compilation toolchains for Raspberry Pi
https://tttapa.github.io/Pages/Raspberry-Pi
MIT License
82 stars 15 forks source link

trouble crosscompiling with threads (and dependencies) #4

Open user706 opened 1 year ago

user706 commented 1 year ago

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 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`** ```cpp #include namespace po = boost::program_options; #include #include 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(), "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(); } 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; } ```
user706 commented 1 year ago

By the way, if I go ahead and use ubuntu's own crosscompiler /usr/bin/arm-linux-gnueabihf-g++, I then the thread-stuff works and I get:
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success

Edit: not - I was not using the sbuild-schroot sysroot!

-- The CXX compiler identification is GNU 11.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/arm-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 - Success
-- Found Threads: TRUE  
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Boost (missing: Boost_INCLUDE_DIR program_options)
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)
  CMakeLists.txt:7 (find_package)

-- Configuring incomplete, errors occurred!
user706 commented 1 year ago

Also: If I use your toolchain, but not the sbuild schroot sysroot, calling like this:

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/rpi-bullseye-armhf`  -S . -B 
#                                    commenting out the schroot above!!!!!!!!!!!!!!
build-$variant_triple --toolchain ~/opt/x-tools/$triple/$variant_triple.toolchain.cmake
# Build
cmake --build build-$variant_triple -j13

then I get:

-- 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
-- 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 - found
-- Found Threads: TRUE  
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find Boost (missing: Boost_INCLUDE_DIR program_options)
Call Stack (most recent call first):
  /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:594 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.22/Modules/FindBoost.cmake:2360 (find_package_handle_standard_args)
  CMakeLists.txt:7 (find_package)

-- Configuring incomplete, errors occurred!

So then threads if found (yay!!), but then I have a problem with boost program options not being found.

Edit: do I somehow need to merge the sysroots?

I've kindof tried copying your toolchain's sysroot over to the sbuild schroot...

sudo rsync -av /home/albert/opt/x-tools/armv8-rpi3-linux-gnueabihf/armv8-rpi3-linux-gnueabihf/sysroot/ /var/lib/schroot/chroots/rpizero-bullseye-armhf
sudo symlinks -rc /var/lib/schroot/chroots/rpizero-bullseye-armhf

...but the result did not work. (Or maybe there was an error copying?) ??

tttapa commented 1 year ago

See https://github.com/tttapa/RPi-Cross-Cpp-Development/issues/2#issuecomment-1431703892 for an explanation and a fix.

tttapa commented 1 year ago

By the way, if I go ahead and use ubuntu's own crosscompiler /usr/bin/arm-linux-gnueabihf-g++, I then the thread-stuff works and I get: -- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success

This is surprising to me. Which pthread library does it select? Does it link correctly? Which toolchain file are you using in this case?

user706 commented 1 year ago

See tttapa/RPi-Cross-Cpp-Development#2 (comment) for an explanation and a fix.

Thanks!

(Regarding ubuntu's own crosscompiler: Ah sorry, I was not clear: with ubuntu's own crosscompiler, I was not using the sbuild schroot sysroot. So it found it's own pthread, but the boost depencency failed.)

tttapa commented 1 year ago

(Regarding ubuntu's own crosscompiler: Ah sorry, I was not clear: with ubuntu's own crosscompiler, I was not using the sbuild schroot sysroot. So it found it's own pthread, but the boost depencency failed.)

Ah okay, then it makes sense :)