RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1274442
Accepted
user_1234567890
user_1234567890
Asked:2022-04-25 04:27:21 +0000 UTC2022-04-25 04:27:21 +0000 UTC 2022-04-25 04:27:21 +0000 UTC

在 CLion (+ CMake) 中构建动态库

  • 772

如果我理解正确,请告诉我要在 CLion 中获取 DLL 库,只需在 CMake 文件中输入以下内容即可:

add_library(project_name SHARED src/library.cpp src/library.h)

事实是我在 CMakeLists.txt 中准确输入了这个,但是在组装后我得到了一个无法动态连接到另一个项目的库(对于测试,我尝试动态连接 libmariadb.dll 库 - 一切正常,所以我连接它正确)。

我也附上代码。

1.库.h

#pragma once

#ifdef MY_LIB
#define MY_LIB __declspec(dllexport)
#else
#define MY_LIB __declspec(dllimport)
#endif

extern "C" MY_LIB void testa(int a);
extern "C" MY_LIB bool testb();

2.库.cpp

#include "library.h"
#include <iostream>

void testa(int a) {
    std::cout << "This is 'A' function" << std::endl;
}

bool testb() {
    std::cout << "This is 'B' function" << std::endl;
    return true;
}

3.CMakeLists.txt

cmake_minimum_required(VERSION 3.5)

project (MY_LIB)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++1y -Wall")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/pdb)

#-------------------- Create a library -----------------------------------------
#Generate the shared library from the library sources
#-------------------------------------------------------------------------------

include_directories("${PROJECT_SOURCE_DIR}/include")

add_library(${PROJECT_NAME}
        SHARED
        src/library.cpp
        )


include(GenerateExportHeader)
generate_export_header(${PROJECT_NAME})

4.程序的代码,我检查库的工作。我启动它并在控制台中指定库的名称(包括 .dll)。库本身 (name.dll) 位于该程序所在的文件夹中(连同 .exe 文件)。如果我将任何其他 dll 库放入同一个文件夹并在控制台中输入其名称,则它会成功加载(我没有收到消息“库...未找到”)。但是,如果我指定我的 dll 的名称,我会收到一个错误,即库未加载(...未找到)。

#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <iostream>

using namespace std;

typedef int (*function)(int a);

std::wstring s2ws(const std::string& s) {
    int len;
    int slength = (int)s.length() + 1;
    len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
    wchar_t* buf = new wchar_t[len];
    MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
    std::wstring r(buf);
    delete[] buf;
    return r;
}

int main() {
    string name;
    cout << "Enter the name of library: ";
    cin >> name;

    wstring stemp = s2ws(name);
    LPCWSTR result = stemp.c_str();

    HMODULE hm = LoadLibrary(result);

    if (hm == NULL) {
        cout << "Library " << name << " is not found!" << endl;
        system("pause");
    }
    else {
        cout << "Library " << name << " is loaded" << endl;
        function testa= (function)GetProcAddress(hm, "testa");

        testa(321);
    }

    FreeLibrary(hm);

    _getch();
    return 0;
}

谢谢!

cmake
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    arrowd
    2022-05-01T20:51:19Z2022-05-01T20:51:19Z

    预处理器定义是必要的,因为 当你编译一个使用 DLL 的应用程序时,它的函数必须用 注释__declspec(dllimport),而当你编译 DLL 本身时,它的函数必须用__declspec(dllexport). 如果你在那里和那里都写了一个导出,那么库将正常编译,但应用程序会出现问题。

    • 1

相关问题

  • 如何在 find_package 之后从 CMake 获取包含目录的路径?

  • 是否可以在 add_library cmake 中指定放置库二进制文件的文件?

  • find_package() 命令在 cmake 中的工作原理

  • 为什么 foreach() 中的 find_library() 不起作用?

  • 为什么 find_library() 不在 CMAKE_LIBRARY_PATH 中寻找路径?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5