解决 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 并调用真实函数。

相关推荐
bu_shuo16 分钟前
删除Windows资源管理器左侧【此电脑】的Linux企鹅图标
linux·windows
孙鹏宇.22 分钟前
C++类型转换
c++
超级架构师34 分钟前
让 COBOL 长出 AI 大脑:原生 Agent 核心框架 praxo-core 深度剖析
c语言·开发语言·c++·人工智能·python·架构·ai编程
·present·38 分钟前
CSAPP(lecture 10)
c语言·c++
YLCHUP43 分钟前
【模拟&STL】题解:AT_abc203_e [ABC203E] White Pawn_基础算法_数据结构_STL模板库_AtcoderABC_C++算法竞赛
数据结构·c++·算法·leetcode·动态规划·哈希算法·近邻算法
xiaolang_8616_wjl1 小时前
c++游戏_宝可梦大世界(源代码)
开发语言·c++·游戏
清水白石0081 小时前
Python 安装与环境配置:Windows、macOS、Linux 完整指南(第2集)
windows·python·macos
木卫二号Coding1 小时前
关闭-Windows10文件资源管理器-预览窗格-有效
windows
余额瞒着我当琳1 小时前
C++异常,异常的概念,异常抛出与捕获,RAII 和智能指针的设计思路,C++标准库一些指针的使用
java·开发语言·c++·算法
東隅已逝,桑榆非晚1 小时前
map和set的使用
数据结构·c++·笔记·学习