Rust linker seeks a LIB, rather than a DLL - windows

I'm experimenting with Rust on Windows. My code declares and calls a function in an external library.
The declaration is like this:
#[link(name = "Rvea0326nc-64")]
extern "C" {
fn WeibullSpeedProbability(wa: &f32, wk: &f32, qu: &f32, prob: &f32, theerr: &i32) -> ();
}
(It's all ByRef because the DLL is Fortran. It's built with the Intel compiler.)
Note that the file name has no extension. The DLL is in the \target\debug\deps folder of the Rust project.
According to the documentation here
https://doc.rust-lang.org/std/keyword.extern.html, this should import a DLL on Windows, but I get an error, thus:
error: linking with `link.exe` failed: exit code: 1181
<SNIP>
= note: LINK : fatal error LNK1181: cannot open input file 'Rvea0326nc-64.lib'
Sure enough, if I find and copy in the *.lib file from which the DLL was generated, everything works fine. The DLL is apparently irrelevant.
I have tried explicitly adding ".dll" to the link name, but Rust just complains that it cannot find Rvea0326nc-64.dll.lib.
Is the documentation wrong? Have I missed something? Is there a way to get Rust to work with the DLL?
Update: I found that when running the Rust-compiled executable directly, the DLL is required and the LIB is not.

Without having too much experience with FFI in Rust, I can imagine to compile your program, you will need the .lib installed on your machine, so that rustc can correctly check that the FFI function is correct. Then when the produced binary is run, it loads the .dll at runtime and uses it.
Try to see if after producing a binary with the .lib installed, that you can run that binary without the .lib installed.

Related

Cannot compile Rust code that calls C code which calls vsnprintf on Windows

I'm trying to compile Rust code on Windows that calls vsnprintf. My C code looks like this:
#include <stdarg.h>
#include <stdio.h>
// A static buffer for storing any formatted messages.
static char buffer[4096];
void rust_logger(const char *fmt, ...) {
// Reconstruct the variable arguments as a va_list. This is necessary so we
// can chain together a call to vsnprintf.
va_list varargs;
va_start(varargs, fmt);
// Write the formatted string to our target (static) buffer.
vsnprintf(buffer, sizeof(buffer)-1, fmt, varargs);
// Call Rust back with final string
...
// Clean up processing of variable arguments
va_end(varargs);
}
This works fine on macOS (and I would guess under Linux, although I have not tried it). But on Windows, I'm having problems. The error I get is:
"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin\\amd64\\link.exe" "/LIBPATH:C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\lib\\amd64" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\10\\lib\\10.0.10240.0\\ucrt\\x64" "/LIBPATH:C:\\Program Files (x86)\\Windows Kits\\8.1\\lib\\winv6.3\\um\\x64" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\deps\\rust_fmu-dd167e2c1e3583f1.0.o" "/OUT:C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\deps\\rust_fmu-dd167e2c1e3583f1.exe" "/OPT:REF,NOICF" "/DEBUG" "/LIBPATH:C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\deps" "/LIBPATH:C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\build\\rust-fmu-e434516f4288772d\\out" "/LIBPATH:C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "logger.lib" "C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\deps\\liblibloading-c41a2f71457b39f3.rlib" "C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\deps\\liblibc-5dc7b85e748840b4.rlib" "C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\deps\\libkernel32-835ed4d4f4dc2d3e.rlib" "C:\\Users\\mtiller\\Source\\rust-fmu\\target\\debug\\deps\\libwinapi-a5898d7aceb63fac.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-90fbcc8c07b4a644.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libpanic_unwind-d2e7baf2c0a36eaf.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libunwind-112baa0117a60076.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liblibc-ad15457034b2bf37.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\librand-fa1852079e0fefd1.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcollections-27e4c8cc19e6faac.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc-588bb0bd8c9dd8ca.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\liballoc_system-dbfe715efb71d408.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd_unicode-a2e15800b52a7a60.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcore-b2880fdfb9b2b596.rlib" "C:\\Users\\mtiller\\.rustup\\toolchains\\stable-x86_64-pc-windows-msvc\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libcompiler_builtins-9d27746f5ba8488f.rlib" "kernel32.lib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "shell32.lib" "msvcrt.lib"
note: logger.lib(logger.o) : error LNK2019: unresolved external symbol __ms_vsnprintf referenced in function vsnprintf
C:\Users\mtiller\Source\rust-fmu\target\debug\deps\rust_fmu-dd167e2c1e3583f1.exe : fatal error LNK1120: 1 unresolved externals
From my research, it seems like this has something to do with some mismatch between MinGW and MinGW64, but when I look at the output of the Rust build, it seems to be using Visual Studio 14.0.
I'm totally confused. I don't quite know how Rust determines which toolchain to use. I used the rustup-init.exe installer which I assumed would "do the right thing", but it seems confused here (or is it just me).
Any suggestions on how to get my toolchain aligned? One other constraint is that I'm using the libloading crate because my program is loading DLLs. I want that part to work as well, but haven't gotten to test that under Windows yet because of build issues.
P.S. - I need the v version of vsnprintf because I'm getting passed varargs and I need a way to actually process them.
My machine had MSVC, MinGW64 and MinGW installed on it. Given the error messages, I fell into the trap of thinking that it was related to rustc invoking the wrong compiler/linker because of issues in my path or its own assumptions about the tools on my machine.
It turns out the build script I was using was calling gcc and ar directly, which invoked the MinGW commands. I thought Rust had "wrappers" for these commands so they behaved the same on all platforms (hence my lack of concern about these).
When I read the documentation on build scripts more closely, I see that they repeat the same (non-portable) approach, but then talk about the lack of cross-platform support for that approach and then show a much simpler and more portable way to do this.
Bottom line, use:
[build-dependencies]
gcc = "0.3"
...and read the build script documentation carefully. Now everything compiles fine on both macOS and Windows.

boost library 1.47.1 build 'lib' prefix causing LNK1104 error

I'm having difficulties generating the correct boost .lib file to compile with a VS project I've been given. It appears that after performing the complete build installation using 'b2.exe' from VS2010 command prompt I'm only able to generate the boost library files that contain the 'lib' prefix.
When I come to compile my project I'm getting the following error message:
"error LNK1104: cannot open file 'boost_signals-vc90-mt-1_47.lib'"
After going through the lib folder I can see that my boost build has only generated 'libboost_signals-vc90-mt-1_47.lib'
The boost documentation gives the following information about the lib prefix:
lib
Prefix: except on Microsoft Windows, every Boost library name begins with this string. On Windows, only ordinary static libraries use the lib prefix; import libraries and DLLs do not.
So far I've attempted the following build options for the msvc-9.0 toolset:
'build-type=complete'
'link=static,shared'
Any advice on how I may be able to generate the required .lib file would be greatly appreciated.
Many Thanks.
link=static should be used whenever you're linking to static version of boost library.
link=shared - should be used whenever you're linking dynamically to boost. It will add extra dependencies on boost dll's.
You can also use link=static,shared to build both versions - static and dynamic.
Define 'BOOST_ALL_DYN_LINK' in project controls how you link to boost.
If it's defined - it's dynamic linking, if not defined - it's static linking.

Error in CodeBlocks C++ program and how to set default main class

I have included the boost library in a Codeblocks c++ project.
Now, in the file
boost/function.hpp
there is an include statement
#include <boost/preprocessor/iterate.hpp>
However I get this error in Codeblocks when I try and compile:
/home/arvind/Documents/Workspace/Browser/boost/function.hpp|15|fatal error:
boost/preprocessor/iterate.hpp: No such file or directory|
What am I doing wrong here? I have simply included the Boost library as it is.
Also, I cannot find the screen/option to set the main class (which will actually execute).
How do I do this?(I am new to CodeBlocks hence this question).
Your boost includes seem to be in a non-standard/system directory : /home/arvind/Documents/Workspace/Browser, you must tell the compiler to look there (gcc -I command-line switch).
Go to Project->Build Options->Search Directories->Compiler and add the directory where boost includes are. I don't have a codeblocks install right here so this was from here.
If you can, I would recommand installing boost on your system once and for all instead of just copying files in your codeblocks workspace.

Problems compiling with libraries

I am trying to compile some C++ code which uses the CGAL library on OS X Lion. I downloaded and installed on some directory the CGAL library. Then, when I try to compile the code, using "make";
triangulation.h:18:64: error: CGAL/Exact_predicates_inexact_constructions_kernel.h: No such file or directory
which means it does not find the CGAL lib. I look at the Makefile, and I see that it compiles using the flag
-lCGAL
Wondering how to solve this, I guess I could pass the information about the placement of my compiled library to this variable, but I do not how. I tried with export and so on but it does not recognize it, any hints?
The error message doesn't mean the library isn't found; it means a header file isn't found. The -lCGAL switch does indeed refer to the library. You're going to need a -IXXXXX switch added on to CFLAGS, where XXXXX is the path to the directory containing the CGAL directory which in turn contains Exact_predicates_inexact_constructions_kernel.h .

Why isn't cl.exe producing a valid Windows module?

I have a simple C DLL that exposes functions from a static library. The DLL compiles without errors and I can run DUMPBIN on it to see the exports. However, when I attempt to load it with DllImport in C#, it says this:
System.DllNotFoundException: Unable to load DLL 'ei.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E).
It's in the correct directory, for sure. So, I read that it might be a good idea to try Dependency Walker, in case I need to include something else. Unfortunately, when I try to open my DLL in DW, I get this:
Error: At least one file was not a 32-bit or 64-bit Windows module.
Here's my cl command:
set ERL_INTERFACE_DIR=C:\Progra~1\erl5.7.2\lib\erl_interface-3.6.2\
call vcvars32.bat
cl /I%ERL_INTERFACE_DIR%include /LD ei.c ei.lib Ws2_32.lib /link /LIBPATH:%ERL_INTERFACE_DIR%lib
What could be causing this?
I was linking with a LIB file whose name is the same as the LIB file that the compiler emits, so it was linking with itself. I just changed the name of my source file to ErlInterface.c. I would think the linker would throw up a warning or something when this happens, but it doesn't.
Anyway, I can open the DLL in Dependency Walker now, but I still can't use it with DllImport. That's for another question, though.

Resources