CMake 3.21 rc-1 | Threads::Threads not found

Since switching to CMake 3.21 we are getting a host of errors relating to “Threads::Threads”

This is happening on Windows/MSVC.

I get multiple error message like this after configuration is done

-- Configuring done
CMake Error at CMakeLists.txt:33 (add_library):
  Target "foobar" links to target "Threads::Threads" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

And when I check the VS solution I found this in it.

image

It’s trying to link a library called “Threads::Threads.lib”

I’ve managed to create a SSCCE to reproduce the issue.

I’m on Windows, and my generator is VS2019. This worked just fine in 3.20 but not 3.21.

threads_threads.zip (1.9 KB)

Here is the layout of the project:
image

CMakeLists.txt

cmake_minimum_required(VERSION 3.21)
project(FOOBAR LANGUAGES "CXX")
add_executable(baz main.cpp)
add_subdirectory(temp)
target_link_libraries(baz PRIVATE foo)

temp/CMakeLists.txt

add_library(foo STATIC)
add_subdirectory(src)

temp\src\CMakeLists.txt

find_package(Threads REQUIRED)
target_link_libraries(foo PRIVATE Threads::Threads)
target_sources(foo PRIVATE foo.cpp)
target_include_directories(foo PUBLIC .)

EDIT:

You will get this error message

CMake Error at CMakeLists.txt:5 (add_executable):
  Target "baz" links to target "Threads::Threads" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

Thanks. I can reproduce that on Linux too. I’ll take a look.

Not sure if this helps but, if I change the project slightly it works now.

temp/CMakeLists.txt

add_library(foo STATIC)
add_subdirectory(src)

# Move these 2 calls out of the 'src' dir CMakeLists.txt
find_package(Threads REQUIRED)
target_link_libraries(foo PRIVATE Threads::Threads)

temp\src\CMakeLists.txt

target_sources(foo PRIVATE foo.cpp)
target_include_directories(foo PUBLIC .)

Perhaps it’s a scoping issue that effects imported targets?

I’ve opened CMake Issue 22363 to track this regression. I posted an even simpler minimal example there. Please follow the issue for further updates.

1 Like

@buildSystemPerson thanks for trying out the release candidate!

1 Like