mingw - MinGW 无法为导出的函数添加前缀 _impl_
问题描述
MinGW 应编译一个共享 C 库mylib
,其中包含以下函数
declspec(dllexport) int foo();
该库应在 Visual Studio 下的 C++ 应用程序中使用。
构建库(在 CMake 下,带有选项GNUtoMS
)产生三个文件,mylib.dll mylib.dll.a mylinb.lib
. 检查后者,
dumpbin /HEADERS mylib.lib
每个导出的函数打印一个姿态。上述功能的立场foo
包含行
Symbol name : _foo
因此,MinGW 不会生成前缀_imp_
。预计链接依赖应用程序会失败,因为 Visual Studio 找不到_imp_foo
.
如何做到这一点?
解决方案
这对我有用。我编译了这个源代码main.c
__declspec(dllexport) int foo()
{
return 42;
}
用这个CMakeLists.txt
cmake_minimum_required(VERSION 3.15)
project(mylib)
add_library(mylib SHARED "main.c")
使用此脚本build.bat
set "PATH=C:\msys64\mingw64\bin;%PATH%"
cmake ^
-G "MinGW Makefiles" ^
-D CMAKE_GNUtoMS=ON ^
-D CMAKE_GNUtoMS_VCVARS="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Auxiliary/Build/vcvars64.bat" ^
-D CMAKE_C_COMPILER="x86_64-w64-mingw32-gcc.exe" ^
-D CMAKE_CXX_COMPILER="x86_64-w64-mingw32-g++.exe" ^
.
cmake --build .
如您所见,我使用了 MSYS2 中的 MinGW-w64 而不是 MinGW,但这应该没有任何区别。输出是:
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Check for working C compiler: C:/msys64/mingw64/bin/x86_64-w64-mingw32-gcc.exe
-- Check for working C compiler: C:/msys64/mingw64/bin/x86_64-w64-mingw32-gcc.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/msys64/mingw64/bin/x86_64-w64-mingw32-g++.exe
-- Check for working CXX compiler: C:/msys64/mingw64/bin/x86_64-w64-mingw32-g++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/jacob/Documents/prog/stackoverflow/GNUtoMS
C:\Users\jacob\Documents\prog\stackoverflow\GNUtoMS>cmake --build .
Scanning dependencies of target mylib
[ 50%] Building C object CMakeFiles/mylib.dir/main.c.obj
[100%] Linking C shared library libmylib.dll
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.4.3
** Copyright (c) 2019 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64'
Microsoft (R) Library Manager Version 14.24.28315.0
Copyright (C) Microsoft Corporation. All rights reserved.
Creating library libmylib.lib and object libmylib.exp
[100%] Built target mylib
然后我创建了一个程序prog.c
#include <stdio.h>
__declspec(dllimport) int foo();
int main (void)
{
printf("%d\n", foo());
return 0;
}
并使用编译它
cl.exe prog.c libmylib.lib -o prog.exe
链接成功,结果程序打印42。dumpbin.exe显示的符号是
Symbol name : foo
没有任何下划线和小鬼;尽管如此,它还是奏效了。
推荐阅读
- java - @PreAuthorize 注解不适用于 Spring 中的 JpaRepository
- r - Tensorflow:二进制分类结果全零或全一(网络本身没有训练)
- objective-c - 想要更改标记颜色 HighCharts
- laravel - “函数 App\Http\Controllers\Backend\BlogController::edit() 的参数太少,通过了 0,预期正好 1”
- laravel - Laravel忘记密码功能不起作用
- spring - 如何配置 RabbitMQ(在 Spring Boot 2.x 中)以便手动确认有效
- apache-spark - pyspark(2.4版)group by,总和不起作用
- javascript - 如何让 mysql 连接到网站(使用 html 和 css 创建)
- ruby-on-rails - 如果 QUERY_STRING 超过使用 AJAX 和 Rails 的指定长度,则拆分参数
- javascript - 如何获取返回值的选定单选按钮的值