# Cannot "link" to interface library

**URL:** https://discourse.cmake.org/t/cannot-link-to-interface-library/10624
**Category:** Code
**Created:** [April 10, 2024, 11:16am UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624 "2024-04-10T11:16:26Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [April 10, 2024, 11:16am UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624/1 "2024-04-10T11:16:26Z")

</div>

Hello,

I am working on a library (you can see the files structure [here](https://discourse.cmake.org/t/i-cant-understand-an-error-about-interface-sources-and-prefix/10105/3)).  
I have packaged and zipped it, I get it from a remote server with `FetchContent` and everything is (almost) fine.

In the root CMakeLists.txt, I added an INTERFACE library, and use it as target in a call to `target_compile_options()` (by using also INTERFACE).  
In the src/ directory, I define a static library, and I link it to the interface library. No problem either.  
In its directory, I export a configuration so that this static library may be built and delivered as a ZIP file.

When I want to use the interface library in an other CMake project (after having used FethContent), I get an error message telling the .lib cannot be found (of course : this interface library only define compilation switches).

What should I do ?

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 10, 2024, 11:50am UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624/2 "2024-04-10T11:50:58Z")

</div>

You may find a good example of a generated [cmake-init-header-only](https://github.com/friendlyanon/cmake-init-header-only/tree/master) library.

To help you, I would need to see your CMake files!

---

<div class="post-metadata">

### Author: ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)
#### Post date: [April 10, 2024, 12:09pm UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624/3 "2024-04-10T12:09:05Z")

</div>

> [@ClausKlein](#):
>
> You may find a good example of a generated [cmake-init-header-only](https://github.com/friendlyanon/cmake-init-header-only/tree/master) library.

Nice project !

> [@ClausKlein](#):
>
> To help you, I would need to see your CMake files!

They’re mainly inspired by what is done here :

> **[Creating a C++ library with CMake | Declaration of VAR](https://decovar.dev/blog/2021/03/08/cmake-cpp-library/)**
>
> Creating a C++ library with CMake

I added a level of hierarchy to add tests.

I also added, in the root CMakeLists.txt, this piece of code :

> add\_library(CommonConfig INTERFACE)  
> target\_compile\_features(CommonConfig INTERFACE cxx\_std\_17)  
> target\_compile\_options(CommonConfig INTERFACE  
> $\<$\<CXX\_COMPILER\_ID:MSVC\>:/MP\>  
> $\<$\<CXX\_COMPILER\_ID:MSVC\>:/W4\>  
> $\<$\<CXX\_COMPILER\_ID:MSVC\>:/ZI\>VisualStudio  
> $\<$\<CXX\_COMPILER\_ID:MSVC\>:/Zc:\_\_cplusplus\>  
> )  
> target\_link\_options(CommonConfig INTERFACE  
> $\<$\<CXX\_COMPILER\_ID:MSVC\>:/INCREMENTAL\>debugging / VisualStudio  
> )

I retrieve CommonConfig in the src directory by :

> target\_link\_libraries(${PROJECT\_NAME} PUBLIC CommonConfig)

But I am not sure how to retrieve it from the consumer.  
After `FetchContent_Declare()`, `FetchContent_MakeAvailable()`, `FetchContent_GetProperties()` and `find_package()`, I can link to the binary library, but I am not sure how I am supposed to retrieve CommonConfig interface library from the library client.

If you need all the CMakeLists.txt, let me know.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 10, 2024, 12:22pm UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624/4 "2024-04-10T12:22:55Z")

</div>

> [@Oodini](#):
>
> target\_link\_libraries(${PROJECT\_NAME} PUBLIC CommonConfig)

if you link it this way, you have to export the config packages too!

Or you should use it like this:

```cmake
target_link_libraries(${PROJECT_NAME} PRIVATE $<BUILD_INTERFACE:CommonConfig>)

```

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 10, 2024, 12:32pm UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624/5 "2024-04-10T12:32:13Z")

</div>

> [@Oodini](#):
>
> [Creating a C++ library with CMake | Declaration of VAR](https://decovar.dev/blog/2021/03/08/cmake-cpp-library/)

Sorry to say it, this is not quite up to date:

- FILE\_SETS are not used!
- header only libs are not handled!
- cmake workflow preset are missing?
- GitHub workflow (CI) is missing?

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [April 10, 2024, 1:20pm UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624/6 "2024-04-10T13:20:04Z")

</div>

> not quite up to date  
> FILE\_SETS, header only libs, cmake workflow preset

From what I can see 3 years later since that blog post was published, none of the essentials became “not up to date” in there. But I can certainly agree that being a blog post it does not replace the entire “Professional CMake” book 🙂

> GitHub workflow (CI)

Did not know that this is required for everyone nowadays. So far we’ve been hosting our Git repositories with Gitea on internal network. I’ll ask around, could be that we are missing something.

---

<div class="post-metadata">

### Author: ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)
#### Post date: [April 10, 2024, 1:36pm UTC](https://discourse.cmake.org/t/cannot-link-to-interface-library/10624/7 "2024-04-10T13:36:23Z")

</div>

> [@retif](#):
>
> I can certainly agree that being a blog post it does not replace the entire “Professional CMake” boo

You are right. A lot of new features has been added to CMake. 😀
