Search code examples
c++cmaketoolchain

FindThreads only works if either C or CXX language is enabled


I'm trying to build a portable gcc in order to allow others to compile using different systems.

I used crosstool-ng to cross compile gcc-8.2 for x86_64, I built a static toolchain.

I created the following toolchain file:

#set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR "x86_64")
set(QAMF_ROOT "/opt/hyperserver-dev/x86_64-qamf")

set(COMPILER_ROOT "${QAMF_ROOT}")
set(COMPILER_PREFIX "x86_64-qamf-linux-gnu-")
set(C_COMPILER "gcc")
set(CXX_COMPILER "g++")

# specify the cross compile
set(CMAKE_C_COMPILER    ${COMPILER_ROOT}/bin/${COMPILER_PREFIX}${C_COMPILER} )
set(CMAKE_CXX_COMPILER  ${COMPILER_ROOT}/bin/${COMPILER_PREFIX}${CXX_COMPILER} )

# HyperServer sysroot path
set(HYPERSERVER_SYSROOT "${COMPILER_ROOT}/x86_64-qamf-linux-gnu/sysroot") 

# where is the target environment 
set(CMAKE_FIND_ROOT_PATH ${HYPERSERVER_SYSROOT} )

# search for programs in the build host directories
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

# for libraries and headers in the target directories
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

On my host machine it works great and I can compile without any issue but when I moved the toolchain file on another machine I have problems. If I try to include Threads on my cmake I've the error below:

CMake Error at /usr/local/share/cmake-3.13/Modules/FindThreads.cmake:54 (message):
  FindThreads only works if either C or CXX language is enabled
Call Stack (most recent call first):
  CMakeLists.txt:2 (find_package)


-- Configuring incomplete, errors occurred!
make: *** No targets specified and no makefile found.  Stop.

This is my CMakeFiles/txt

cmake_minimum_required(VERSION 3.9)
find_package(Threads REQUIRED)
project(HelloWorld C CXX)
add_executable(${PROJECT_NAME} "main.cpp")
target_link_libraries(${PROJECT_NAME} PRIVATE Threads::Threads)

Solution

  • Place

    find_package(Threads REQUIRED)
    

    after

    project(HelloWorld C CXX)
    

    This is what the error message tells: The project() call enables languages, so finding the Threads would succeed.


    Normally, all find_package calls should come after the project() one.