'(C) How to link dlls within CLion

I've been programming C for a while with Visual Studio, but now switched to CLion. My programming and target system is Windows10.

Within Visual Studio, I was able to include the required DLLs like "vcruntime140d.dll" and "ucrtbased.dll" inside my exe.

I did this by going into the project settings and set configuration settings - C/C++ - code generation - runtime library to "Multithreaded-Debug (/MTd)". Doing this I was able to run the resulting exe without having errors like "vcruntime140d.dll is missing" or "ucrtbased.dll is missing".

But how can I achieve this within CLion?

I've been searching for a while now, and I found a lot of tutorials on how to include .lib files but not for DLLs (I don't have the code for).



Solution 1:[1]

With Clion, you actually are working with CMake. So the question is to be like how to link dlls within CMake.

There are many ways to do. e.g.

If the library could not be found by default, use find_library to search for it.

If these functions seems too strange to you, check this tutorial from the CLion team.

Update

As in the comment you asked, your problem is how to load a dll without lib. To address this, you could dynamicly load the dll, or make a lib from the dll.

For Windows multicopies problem, add following into your CMakeLists.txt

foreach (flag_var
      CMAKE_C_LINK_FLAGS
      CMAKE_C_LINK_FLAGS_DEBUG
      CMAKE_C_LINK_FLAGS_RELEASE
      CMAKE_CXX_LINK_FLAGS
      CMAKE_CXX_LINK_FLAGS_DEBUG
      CMAKE_CXX_LINK_FLAGS_RELEASE
      CMAKE_EXE_LINKER_FLAGS
      CMAKE_EXE_LINKER_FLAGS_DEBUG
      CMAKE_EXE_LINKER_FLAGS_RELEASE
      CMAKE_C_FLAGS
      CMAKE_C_FLAGS_DEBUG
      CMAKE_C_FLAGS_RELEASE
      CMAKE_C_FLAGS_MINSIZEREL
      CMAKE_C_FLAGS_RELWITHDEBINFO
      CMAKE_CXX_FLAGS
      CMAKE_CXX_FLAGS_DEBUG
      CMAKE_CXX_FLAGS_RELEASE
      CMAKE_CXX_FLAGS_MINSIZEREL
      CMAKE_CXX_FLAGS_RELWITHDEBINFO)
  string(REPLACE "/MD" "-MT" ${flag_var} "${${flag_var}}")
endforeach ()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1