Sorry about being unclear. I have a bad habit of only asking questions when I'm tired. At this point I have found a solution, kinda.
I was trying to link to a .lib output from the rust compiler. Compiling with cargo build --release, crate-type set to ["staticlib"] withing cargo.toml.
I tried reducing my problem more and using both mingw compiler as well as msvs. So I reduced my Rust code to:
#[no_mangle]
pub extern "win64" fn foo() {
let x = 5;
// print!("hi Rust");
}
To more closely follow the example here: A little Rust with your C - The Embedded Rust Book . Commented out print because that wasn't working either.
A.lib file results from the compilation. I try to compile the .cpp program:
#include <stdio.h>
void foo();
int main(){
printf("hello C");
foo();
return 0;
}
with MSVS command "cl /EHsc cppfoo.cpp /link export_rust.lib" and get the error:
cppfoo.obj : error LNK2019: unresolved external symbol "void __cdecl foo(void)" (?foo@@YAXXZ) referenced in function main
cppfoo.exe : fatal error LNK1120: 1 unresolved externals
with mingw command "g++ cppfoo.cpp export_rust.lib" I get:
undefined reference to `foo()'
collect2.exe: error: ld returned 1 exit status
I have tried changing pub extern "win64"
to pub extern "C", "system", "stdcall", and "sysv64"
. All result in the same error messages. Funnily, the only thing I got to work was changing the program from .cpp to .c. This compiled and ran when the print!
macro was commented out, but returned a slew of errors about undefined references. Regardless, changing .cpp file to .c is not a long term solution.
I don't know if it makes a difference, but when I run rustup default it lists my default toolchain as "stable-x86_64-pc-windows-msvc". I have not tried other toolchains.