# target\_link\_libraries doesn't work correctly with C++/CLI compiled dll

**URL:** https://discourse.cmake.org/t/target-link-libraries-doesnt-work-correctly-with-c-cli-compiled-dll/6406
**Category:** Usage
**Tags:** os:windows, lang:csharp
**Created:** [September 4, 2022, 5:32pm UTC](https://discourse.cmake.org/t/target-link-libraries-doesnt-work-correctly-with-c-cli-compiled-dll/6406 "2022-09-04T17:32:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![lo1ol](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lo1ol/32/2735_2.png) [@lo1ol](https://discourse.cmake.org/u/lo1ol)
#### Post date: [September 4, 2022, 5:32pm UTC](https://discourse.cmake.org/t/target-link-libraries-doesnt-work-correctly-with-c-cli-compiled-dll/6406/1 "2022-09-04T17:32:17Z")

</div>

Hi! I noticed that cmake doesn’t link C++/CLI dll correctly. The problem is that cmake tries to link static version of library too, but it doesn’t exist.

I write small example to reproduce a problem:

```cmake
cmake_minimum_required(VERSION 3.5)

project(BugExample LANGUAGES CSharp CXX)

include(CSharpUtilities)

if(CMAKE_CXX_FLAGS_DEBUG MATCHES "/RTC1")
   string(REPLACE "/RTC1" " " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}")
endif()

if(CMAKE_CXX_FLAGS MATCHES "/EHsc")
   string(REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
endif()

add_library(Class1 SHARED Class1.cpp)
set_property(TARGET Class1 PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.7.2")
set_property(TARGET Class1 PROPERTY COMMON_LANGUAGE_RUNTIME "")
set_target_properties(Class1 PROPERTIES DEBUG_POSTFIX "d")
set_target_properties(Class1 PROPERTIES VS_GLOBAL_ManagedAssembly "true")
set_target_properties(Class1 PROPERTIES VS_GLOBAL_KEYWORD "ManagedCProj")
set_property(TARGET Class1 PROPERTY VS_DOTNET_REFERENCES
         "Microsoft.CSharp"
         "System"
         "System.Core"
         "System.Windows"
         "System.Data"
         "WindowsBase"
         "mscorlib")

add_library(Class2 SHARED Class2.cpp)
set_property(TARGET Class2 PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "v4.7.2")
set_property(TARGET Class2 PROPERTY COMMON_LANGUAGE_RUNTIME "")
set_target_properties(Class2 PROPERTIES DEBUG_POSTFIX "d")
set_target_properties(Class2 PROPERTIES VS_GLOBAL_ManagedAssembly "true")
set_target_properties(Class2 PROPERTIES VS_GLOBAL_KEYWORD "ManagedCProj")
set_property(TARGET Class2 PROPERTY VS_DOTNET_REFERENCES
         "Microsoft.CSharp"
         "System"
         "System.Core"
         "System.Windows"
         "System.Data"
         "WindowsBase"
         "mscorlib")

#target_compile_options(Class2 PRIVATE "/FU$<TARGET_FILE:Class1>")

target_link_libraries(Class2 Class1)

```

```nohighlight
namespace Kek {
        public ref class Class1 {};
}

```

```nohighlight
namespace Kek {
        public ref class Class2 {
                void lol() {
                        Class1^ x = gcnew Class1();
                }
        };
}

```

So, at compilation I get:

```nohighlight
LINK : fatal error LNK1104: cannot open file 'Debug\Class1d.lib'

```

To workaround this problem you can pass /FU flag manually, but it’s not cool:)

---

<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: [September 4, 2022, 10:00pm UTC](https://discourse.cmake.org/t/target-link-libraries-doesnt-work-correctly-with-c-cli-compiled-dll/6406/2 "2022-09-04T22:00:49Z")

</div>

If `Class1` doesn’t export any symbols, I don’t think the linker produces an import library (.lib) for your DLL. You would need to ensure `Class1` does export symbols to address that. I am not familiar with what the `/FU` option does, but presumably one of its effects is to define at least one symbol, which would force a .lib to be created.

---

<div class="post-metadata">

### Author: ![lo1ol](https://discourse.cmake.org/user_avatar/discourse.cmake.org/lo1ol/32/2735_2.png) [@lo1ol](https://discourse.cmake.org/u/lo1ol)
#### Post date: [September 5, 2022, 6:09am UTC](https://discourse.cmake.org/t/target-link-libraries-doesnt-work-correctly-with-c-cli-compiled-dll/6406/3 "2022-09-05T06:09:54Z")

</div>

`Class1` contains at least constructor symbol. Nevertheless, I have the same problem with libraries which contains symbols explicitly.

But maybe I misunderstood you. Can you please provide an example?

About [/FU](https://docs.microsoft.com/ru-ru/cpp/build/reference/fu-name-forced-hash-using-file?view=msvc-170) flag

---

<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: [September 5, 2022, 11:15am UTC](https://discourse.cmake.org/t/target-link-libraries-doesnt-work-correctly-with-c-cli-compiled-dll/6406/4 "2022-09-05T11:15:03Z")

</div>

You have defined `Class1`, but I don’t see anything that is causing that symbol to be exported. Note that defining and exporting are two different things. With the Visual Studio toolchain, no symbols are exported by default, you have to actively specify what to export. Other compilers export all symbols by default, so you can get away without doing anything.

It might be overkill for what you need, but the earlier parts of my 2019 CppCon talk covers this topic: [CppCon 2019: Deep CMake For Library Authors - Crascit](https://crascit.com/2019/10/16/cppcon-2019-deep-cmake-for-library-authors/)

I haven’t use the `/FU` flag personally, so I am not familiar with its effects.

---

<div class="post-metadata">

### Author: ![emmenlau](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/e/8c91f0/32.png) [@emmenlau](https://discourse.cmake.org/u/emmenlau)
#### Post date: [August 6, 2025, 1:41pm UTC](https://discourse.cmake.org/t/target-link-libraries-doesnt-work-correctly-with-c-cli-compiled-dll/6406/5 "2025-08-06T13:41:55Z")

</div>

I am also affected by this. Basically, `target_link_libraries` is not the right way to link a “C++/CLI” library, or at least not out-of-the-box. The symbols just show as unresolved.

I think there must be some other way to reference a “C++/CLI” library, for example something like [https://cmake.org/cmake/help/latest/prop\_tgt/VS\_DOTNET\_REFERENCES.html](https://cmake.org/cmake/help/latest/prop_tgt/VS_DOTNET_REFERENCES.html), but I’m not knowledgeable with this at all.

Any help would be appreciated!
