/usr/bin/ld: cannot find X No such file or directory

I’m trying to include this library in my project:

I’m using FetchContent which succesfully populates _deps/lspcpp-build, _deps/lspcpp-src, _deps/lspcpp-subbuild:

FetchContent_Declare(
    lspcpp
    GIT_REPOSITORY https://github.com/kuafuwang/LspCpp.git
)
FetchContent_GetProperties(lspcpp)
if(NOT lspcpp_POPULATED)
    FetchContent_Populate(lspcpp)
endif()
FetchContent_MakeAvailable(lspcpp)

I define my executable:

add_executable(balance
                foo.cpp
                bar.cpp
                ...
)

And try to link it:

target_include_directories(balance lspcpp)
target_link_libraries(balance lspcpp)

This produces this error:

/usr/bin/ld: cannot find -llspcpp: No such file or directory

Can you help me resolve this error? Is it wrong to use FetchContent to include an external library?

You’ve told CMake the name of the library to link (and CMake told the linker), but nowhere have you specified where to look for this. You’ll need a link directory for this to work.

@ben.boeckel To my understanding it’s supposed to be a target brought in by FetchContent

If it is making an -l flag, there is no such target. As for why…it’s beyond my FetchContent knowledge.

I solved it by adding add_subdirectory(${lspcpp_SOURCE_DIR} ${lspcpp_BINARY_DIR}) inside the if.
I posted it on StackOverflow as well and got an answer: c++ - cmake: target_link_libraries - /usr/bin/ld: cannot find X No such file or directory - Stack Overflow

I recommend using FetchContent_MakeAvailable() instead of the old manual population pattern. It is much simpler, more robust and comes with additional features for the developer in more recent CMake releases:

FetchContent_Declare(
    lspcpp
    GIT_REPOSITORY https://github.com/kuafuwang/LspCpp.git
)
FetchContent_MakeAvailable(lspcpp)

Thanks Craig, that’s also what I ended up doing!
I only abandoned this because it went into an infinite loop, but that was due to CMAKE_CXX_COMPILER being changed, and not related to FetchContent.