title: Linking shared dll library cmake clion project
body: <p>I currently have two C projects on Clion with cmake. One of the projects is named "sharedLibsDemo" and I am trying to create a shared library in this project. In the other project I want to use the library that was created by the "shared" project. </p>
  
  <p>Currently, in the "sharedLibsDemo" project I have the following cmake:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.7)
  project(sharedLibsDemo)
  
  set(CMAKE_C_STANDARD 11)
  INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
  set(SOURCE_FILES shared.c shared.h main.c)
  add_library(shared SHARED ${SOURCE_FILES})
  include(GenerateExportHeader)
  GENERATE_EXPORT_HEADER(shared           # generates the export header shared_EXPORTS.h automatically
          BASE_NAME shared
          EXPORT_MACRO_NAME SHARED_EXPORTS
          EXPORT_FILE_NAME shared_EXPORTS.h
          STATIC_DEFINE SHARED_EXPORTS_BUILT_AS_STATIC) 
  
  set(EXEC_FILES main.c)
  add_executable(myexe ${EXEC_FILES})
  target_link_libraries(myexe shared)
  </code></pre>
  
  <p>However, this cmake only creates the <code>shared_EXPORTS</code>.h, libshared.dll, and libshared.dll.a` files.
  I managed to create the .lib file using Mingw itself and put all of these files including the .h file of the source code into one folder and placed the folder in the root folder of the second project in order to use it.</p>
  
  <p>However, I've looked everywhere to find a way to link the library into the second project's executable. The documentation for cmake itself assumes I have a tonne of knowledge which I don't. Is there any list of commands that I can use to finally link my library. I have already tried the generic answer of "use find_package() or target_link_libraries" to no avail.</p>
  
  <h2>EDIT 1</h2>
  
  <p>The following is the contents of <code>shared.h</code> :</p>
  
  <pre><code>#include "shared_EXPORTS.h"
  #ifndef SHAREDLIBSDEMO_LIBRARY_H
  #define SHAREDLIBSDEMO_LIBRARY_H
  
  void SHARED_EXPORTS sharedHello(void);
  
  #endif
  </code></pre>
  
  <p>As per the suggestion of @Shmuel H. I placed the <code>shared.h</code> <code>shared.c</code> and the cmakelist.txt for the <code>shared</code> project into a folder in the project that I want to include the library in. And I used add_subdirectory() and target_link_libraries().</p>
  
  <p>The following is the <code>CMakeLists.txt</code> for the project:</p>
  
  <pre><code>cmake_minimum_required(VERSION 3.7)
  project(projectFiles)
  
  set(CMAKE_C_STANDARD 11)
  include_directories(src ${maker_INCLUDE_DIR})
  set(SOURCE_FILES src/nv_runner/main.c src/FactParser/FactParser.c src/FactParser/FactParser.h src/Utils/Utils.c src/Utils/Utils.h src/nv_runner/main.h)
  
  list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}")
  find_package(maker REQUIRED)
  
  include_directories(${maker_INCLUDE_DIR})
  
  set(LIBSHARED_LOCATION ${PROJECT_SOURCE_DIR}/libshared)
  add_subdirectory(libshared)
  include_directories(${LIBSHARED_LOCATION})
  
  add_executable(${PROJECT_NAME} ${SOURCE_FILES} src/FactParser/FactParser.c src/FactParser/FactParser.c src/FactParser/FactParser.h src/nv_runner/main.h)
  
  target_link_libraries(${PROJECT_NAME} ${maker_LIBRARY})
  target_link_libraries(${PROJECT_NAME} shared)
  </code></pre>
  
  <p>At first, I had to remove the SHARED_EXPORTS macro from the sharedHello function and the <code>shared_EXPORTS.h</code> include in <code>shared.h</code> because otherwise it would not recognize the function for use in other files. But when I ran the program I got this result:</p>
  
  <pre><code>Process finished with exit code -1073741515 (0xC0000135)
  </code></pre>
  
  <h2>EDIT 2</h2>
  
  <p><a href="https://i.stack.imgur.com/Y5jPw.png" rel="nofollow noreferrer">Project setup image</a></p>
  
  <p>In the screenshot, I've taken all the necessary files from the <code>shared</code> project, placed it in a directory in the current project and marked the directory as a Library. The cmake can be seen in the image. With this setup, whenever I run my program, it just crashes with the error seen in the image.</p>