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

**URL:** <https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784>\
**Category:** Code\
**Tags:** os:linux\
**Created:** [November 3, 2022, 7:36pm UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784 "2022-11-03T19:36:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jepperaskdk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jepperaskdk/32/2894_2.png) [@jepperaskdk](https://discourse.cmake.org/u/jepperaskdk)\
**Post date:** [November 3, 2022, 7:36pm UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784/1 "2022-11-03T19:36:31Z")

</div>

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

> **[GitHub - kuafuwang/LspCpp: A Language Server Protocol implementation in C++](https://github.com/kuafuwang/LspCpp)**
>
> A Language Server Protocol implementation in C++. Contribute to kuafuwang/LspCpp development by creating an account on GitHub.

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

```auto
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:

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

```

And try to link it:

```auto
target_include_directories(balance lspcpp)
target_link_libraries(balance lspcpp)

```

This produces this error:

```auto
/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?

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [November 4, 2022, 3:48pm UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784/2 "2022-11-04T15:48:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)\
**Post date:** [November 4, 2022, 4:01pm UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784/3 "2022-11-04T16:01:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [November 4, 2022, 4:18pm UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784/4 "2022-11-04T16:18:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jepperaskdk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jepperaskdk/32/2894_2.png) [@jepperaskdk](https://discourse.cmake.org/u/jepperaskdk)\
**Post date:** [November 4, 2022, 7:33pm UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784/5 "2022-11-04T19:33:38Z")

</div>

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](https://stackoverflow.com/questions/74309382/cmake-target-link-libraries-usr-bin-ld-cannot-find-x-no-such-file-or-direct)

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [November 5, 2022, 9:55pm UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784/6 "2022-11-05T21:55:04Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![jepperaskdk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jepperaskdk/32/2894_2.png) [@jepperaskdk](https://discourse.cmake.org/u/jepperaskdk)\
**Post date:** [November 6, 2022, 7:52am UTC](https://discourse.cmake.org/t/usr-bin-ld-cannot-find-x-no-such-file-or-directory/6784/7 "2022-11-06T07:52:29Z")

</div>

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.
