Linking LLVM fails with target_link_libraries()

find_package(LLVM REQUIRED CONFIG)

llvm_map_components_to_libnames(llvm_libs
  core
  support
  target
  irreader
)

target_include_directories(catguy PRIVATE ${LLVM_INCLUDE_DIRS})
target_compile_definitions(catguy PRIVATE ${LLVM_DEFINITIONS})
target_link_libraries(catguy PRIVATE ${llvm_libs})

The above fails with:

[ 50%] Linking CXX executable catguy
/usr/bin/ld: cannot find -lLLVMCore: No such file or directory
/usr/bin/ld: cannot find -lLLVMTarget: No such file or directory
/usr/bin/ld: cannot find -lLLVMIRReader: No such file or directory
collect2: error: ld returned 1 exit status

But if I change the target_link_libraries to just link_libraries it builds successfully. Can somebody please explain why this happens? and how to fix it?
The above code is mostly from the llvm docs: Building LLVM with CMake - LLVM

Not sure if the LLVM build instructions are up-to-date. Most of the code predates modern CMake. None of the libraries mentioned in your error message exist on my system, while something like libLLVMSupport.a exists. I’m on Arch Linux (if that even matters).

But, it looks like they have imported targets these days (at least for LLVM 22) for certain libraries.

So, what works for me is:

find_package(LLVM REQUIRED CONFIG)

add_executable(foo foo.cpp)
target_link_libraries(foo PRIVATE LLVM)

LLVM is defined as add_library(LLVM SHARED IMPORTED) (in LLVMExports.cmake) and therefore a shared library, which is fine for me, but not sure if you need the static libraries.

Maybe https://discourse.llvm.org/ is a better place to ask for help in your specific situation.