首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用MSVC在Windows上针对"cdylib“锈菌库编写简单的compile+link

如何使用MSVC在Windows上针对"cdylib“锈菌库编写简单的compile+link
EN

Stack Overflow用户
提问于 2020-06-22 15:17:28
回答 1查看 667关注 0票数 1

我使用Rust创建了一个动态库(crate type = "cdylib")。生锈/货物产生了两个文件:text_loading_lib.dlltext_loading_lib.dll.lib。我想在Windows上构建一个非常简单的项目(hello_world.c),它只使用MSVC ( Microsoft C++工具集)和JetBrains CLion/CMake来使用这个库中的一个函数。

main.c

代码语言:javascript
复制
#include <stdio.h>

typedef unsigned long long usize_t; // 64 bit / usize on 64 bit machine
extern void show_loading_animation_ffi(usize_t, usize_t, int, usize_t (*prog_fn)());

// function that tells the lib how many percent progress we made so far
usize_t progress_reporter() { return (usize_t) 20 }

int main(void) {
    show_loading_animation_ffi(0, 100, TARGET_STDERR, progress_reporter);
    return 0;
}

我熟悉UNIX上的这个过程,但我不知道如何在Windows上完成这个过程。在UNIX上,我会在编译期间将共享对象与main.c链接起来,并在运行时使用LD_LIBRARY_PATH提供它的位置。在Windows上类似吗?

我还使用JetBrains CLion对CMAKE项目进行了尝试,但仍然没有成功。当我试图从main()运行main.c中的CLion时,总是会出现无法创建目标的错误。使用Rust创建的库文件(text_loading_lib.dlltext_loading_lib.dll.lib)与CMakeLists.txtmain.c位于同一个目录中。

CMakeLists.txt

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.16)
project(main C)

set(CMAKE_C_STANDARD 11)

add_executable(main main.c)

# the path is correct
target_link_libraries(main text_loading_animation)
#also tried: target_link_libraries(main text_loading_animation.dll)
#also tried:  target_link_libraries(main text_loading_animation.dll.lib)

最高产出是:

代码语言:javascript
复制
[ 50%] Linking C executable main.exe
LINK Pass 1: command "C:\PROGRA~2\MICROS~2\2019\COMMUN~1\VC\Tools\MSVC\1426~1.288\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\main.dir\objects1.rsp /out:main.exe /implib:main.lib /pdb:C:\dev\lib-text-loading-animation-rust\calling-from-c-examples\windows\cmake-build-debug\main.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console text_loading_animation.lib kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\main.dir/intermediate.manifest CMakeFiles\main.dir/manifest.res" failed (exit code 1104) with the following output:
LINK : fatal error LNK1104: File "text_loading_animation.lib" can't be opened.
NMAKE : fatal error U1077: ""C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe"": Return-Code "0xffffffff"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\nmake.exe"": Rückgabe-Code "0x2"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\nmake.exe"": Rückgabe-Code "0x2"
Stop.
NMAKE : fatal error U1077: ""C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.26.28801\bin\HostX64\x64\nmake.exe"": Rückgabe-Code "0x2"
Stop.
EN

回答 1

Stack Overflow用户

发布于 2020-06-22 18:45:13

我让它和CMake以及命令行一起工作。

Cmake: CMakeLists.txt

代码语言:javascript
复制
cmake_minimum_required(VERSION 3.16)
project(main C)

set(CMAKE_C_STANDARD 11)

add_executable(main main.c)
# "*.dll.lib" - important! not just *.dll
target_link_libraries(main ${CMAKE_CURRENT_SOURCE_DIR}/../../target/release/text_loading_animation.dll.lib)

# copy DLL file into target dir
add_custom_command(TARGET main POST_BUILD              # Adds a post-build event to "main"
    COMMAND ${CMAKE_COMMAND} -E copy_if_different      # which executes "cmake - E copy_if_different..."
    "${PROJECT_SOURCE_DIR}/text_loading_animation.dll" # <--this is in-file
    $<TARGET_FILE_DIR:main>)                           # <--this is out-file path

现在您可以在Jetbrains CLion中“运行main”了。

命令行(windows批处理脚本)

重要的部分是:

  1. cl main.c /link text_loading_animation.dll.lib
  2. main.exe

完整批处理脚本:

代码语言:javascript
复制
@echo off

rem: clean old data
del main.exe main.obj *.dll *.dll.lib

rem: copy rust lib into current dir
xcopy /y ..\..\target\release\text_loading_animation.dll .
xcopy /y ..\..\target\release\text_loading_animation.dll.lib .


rem: sets up Visual Studio Compilertoolsuite environment
call "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat"

rem: *.dll.lib contains header information during compile; *.dll is the runtime file that must be in the same directory/path

rem: "cl" is windows compiler: produces main.obj and main.exe; main.obj is not needed in the end;
rem:   it is only for the stage between compiling and linking
rem: /W4 is highest warnings flag
cl /W4 main.c /link text_loading_animation.dll.lib
del main.obj

rem: now execute
main.exe
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62517760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档