解决 Windows C++ DLL 导出类不可见的编译错误

在把C++类/函数编译成动态链接库过程中,Windows 默认所有函数、类都不导出。虽然程序确实被编译成DLL了,但是由于类/函数不写进符号表,因此,外部程序找不到它。

我们可以写一个小程序测试一下:

my_print.h:

cpp 复制代码
#pragma once

class MyPrint {
public:
  MyPrint();
  void PrintHello();
};

my_print.cpp:

cpp 复制代码
#include "my_print.h"
#include <iostream>

MyPrint::MyPrint() {

}

void MyPrint::PrintHello() {
  std::cout << "hello" << std::endl;
}

主函数,main.cpp

cpp 复制代码
#include "my_print.h"

int main()
{
  MyPrint my_print;
  my_print.PrintHello();

  return 0;
}

我们希望把类MyPrint编译为dll,同时main.cpp编译成可执行程序,调用这个动态链接库。我们在CMakeLists.txt中写:

cpp 复制代码
cmake_minimum_required (VERSION 3.10)

project ("test")

# 强制 MSVC 使用 UTF-8 编码
if (MSVC)
    add_compile_options("/utf-8")
endif()

# 编译动态库
add_library(my_print_lib SHARED "my_print.cpp" "my_print.h")
# 编译可执行文件
add_executable(test "main.cpp")
# 链接动态库
target_link_libraries(test PRIVATE my_print_lib)

VS中使用MSVC编译器编译成功,但运行后报错:

编译目录下面是存在DLL文件my_print_lib.dll,但为什么会报这个错误呢?这是因为Linux/macOS中默认所有类/函数都公开,外部随便调用,但Windows中默认所有函数都隐藏,你不主动声明导出,外部根本找不到函数。我们需要改造一下my_print.h,加入导出宏:

cpp 复制代码
#pragma once

// 必须加导出/导入宏,Windows 动态库才能正常使用
#ifdef _WIN32        // 判断当前编译平台是否为 Windows 系统
#  ifdef MYLIB       // 判断是否正在编译生成动态库(MYLIB 为自定义宏)
#    define DLL_TYPE __declspec(dllexport)  // 定义为导出符号,用于生成 DLL
#  else              // 否则为使用动态库的场景
#    define DLL_TYPE __declspec(dllimport)  // 定义为导入符号,用于调用 DLL
#  endif             // 结束 MYLIB 宏判断
#else                // 非 Windows 平台(Linux/macOS 等)
#  define DLL_TYPE   // 类 Unix 系统无需导入导出,宏定义为空
#endif

class DLL_TYPE MyPrint {
public:
  MyPrint();
  void PrintHello();
};

同时我们还需要修改CMakeLists.txt文件:

cpp 复制代码
cmake_minimum_required (VERSION 3.10)

project ("test")

# 强制 MSVC 使用 UTF-8 编码
if (MSVC)
    add_compile_options("/utf-8")
endif()

# 编译动态库
add_library(my_print_lib SHARED "my_print.cpp" "my_print.h")
target_compile_definitions(my_print_lib PRIVATE MYLIB)
# 编译可执行文件
add_executable(test "main.cpp")
# 链接动态库
target_link_libraries(test PRIVATE my_print_lib)

最重要的是在编译my_print_lib这个库的时候,定义一个叫MYLIB的宏,只给库内部使用。

清理编译目录后再次编译运行,程序成功运行,输出hello。我们在编译的目录下面除了my_print_lib.dll外,还可以找到一个my_print_lib.lib文件,这个文件用于在编译期帮助链接器完成符号解析,运行期引导程序加载 DLL 并调用真实函数

相关推荐
君义_noip3 小时前
信息学奥赛一本通 4150:【GESP2509七级】⾦币收集 | 洛谷 P14078 [GESP202509 七级] 金币收集
c++·算法·gesp·信息学奥赛·csp-s
Ricky_Theseus3 小时前
静态链接与动态链接
c++
IDC02-阿杰4 小时前
Windows WSL2安装Ubuntu24.04全攻略
linux·windows
澈2074 小时前
双指针,数组去重
c++·算法
小辉同志4 小时前
207. 课程表
c++·算法·力扣·图论
feng_you_ying_li4 小时前
C++11,{}的初始化情况与左右值及其引用
开发语言·数据结构·c++
小樱花的樱花5 小时前
打造高效记事本:UI设计到功能实现
开发语言·c++·qt·ui
零二年的冬6 小时前
epoll详解
java·linux·开发语言·c++·链表
坚持编程的菜鸟6 小时前
The Blocks Problem
数据结构·c++·算法