clion 引用dll,链接共享的dll库cmake clion项目

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.

Currently, in the "sharedLibsDemo" project I have the following cmake:

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)

However, this cmake only creates the shared_EXPORTS.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.

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.

EDIT 1

The following is the contents of shared.h :

#include "shared_EXPORTS.h"

#ifndef SHAREDLIBSDEMO_LIBRARY_H

#define SHAREDLIBSDEMO_LIBRARY_H

void SHARED_EXPORTS sharedHello(void);

#endif

As per the suggestion of @Shmuel H. I placed the shared.h shared.c and the cmakelist.txt for the shared project into a folder in the project that I want to include the library in. And I used add_subdirectory() and target_link_libraries().

The following is the CMakeLists.txt for the project:

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)

At first, I had to remove the SHARED_EXPORTS macro from the sharedHello function and the shared_EXPORTS.h include in shared.h because otherwise it would not recognize the function for use in other files. But when I ran the program I got this result:

Process finished with exit code -1073741515 (0xC0000135)

EDIT 2

In the screenshot, I've taken all the necessary files from the shared 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.

解决方案

I found out what the issue was. I did not know that the dll had to be in the same folder as the exe, that's why the program was not executing. Once I placed the .dll file inside the exe folder, it worked.

Credit goes to this question for helping me identify the issue: C++ running file with included library failes without compiling error (CMake / CLion)

And I found out that .lib files are only necessary for MSVC compiler. MinGW uses the .dll.a file; replacing .lib. This post lead me to finding this out: Compatibility of *.dll *.a *.lib *.def between VisualStudio and gcc

Please correct me if I'm wrong however. Here is an image of my current project and cmake setup

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值