我使用Rust创建了一个动态库(crate type = "cdylib")。生锈/货物产生了两个文件:text_loading_lib.dll和text_loading_lib.dll.lib。我想在Windows上构建一个非常简单的项目(hello_world.c),它只使用MSVC ( Microsoft C++工具集)和JetBrains CLion/CMake来使用这个库中的一个函数。
main.c
#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.dll和text_loading_lib.dll.lib)与CMakeLists.txt和main.c位于同一个目录中。
CMakeLists.txt
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)最高产出是:
[ 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.发布于 2020-06-22 18:45:13
我让它和CMake以及命令行一起工作。
Cmake: CMakeLists.txt
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批处理脚本)
重要的部分是:
cl main.c /link text_loading_animation.dll.libmain.exe完整批处理脚本:
@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.exehttps://stackoverflow.com/questions/62517760
复制相似问题