Unresolved external symbol {memcpy, memset, memmove, memcmp, strlen, __CxxFrameHandler, _CxxThrowException}

Error when building dylib

"libcore-44bd5dbe4e29964c.rlib(core-44bd5dbe4e29964c.core.7rmb18uk-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memcpy
libminhook_sys-1326816ef2290471.rlib(hook.o) : error LNK2001: unresolved external symbol memcpy
libminhook_sys-1326816ef2290471.rlib(trampoline.o) : error LNK2001: unresolved external symbol memcpy
libstd-e5130808ccb074ce.rlib(std-e5130808ccb074ce.std.cergmp4c-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memcpy
liballoc-3c0db88c6c3c2080.rlib(alloc-3c0db88c6c3c2080.alloc.3pxpxezw-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memcpy
libstd-e5130808ccb074ce.rlib(std-e5130808ccb074ce.std.cergmp4c-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol __CxxFrameHandler3
libpanic_unwind-580326a9fdffe4b1.rlib(panic_unwind-580326a9fdffe4b1.panic_unwind.c9nntgss-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol __CxxFrameHandler3
liballoc-3c0db88c6c3c2080.rlib(alloc-3c0db88c6c3c2080.alloc.3pxpxezw-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol __CxxFrameHandler3
libstd-e5130808ccb074ce.rlib(std-e5130808ccb074ce.std.cergmp4c-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol memset referenced in function _ZN4core3ops8function6FnOnce40call_once$u7b$$u7b$vtable.shim$u7d$$u7d$17h79aa7d690a3d5e88E
librustc_demangle-a76d77b5d9ed4759.rlib(rustc_demangle-a76d77b5d9ed4759.rustc_demangle.bwbqjk7y-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memset
libcore-44bd5dbe4e29964c.rlib(core-44bd5dbe4e29964c.core.7rmb18uk-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memset
libstd-e5130808ccb074ce.rlib(std-e5130808ccb074ce.std.cergmp4c-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol memmove referenced in function _ZN4core3ptr90drop_in_place$LT$std..io..buffered..bufwriter..BufWriter$LT$W$GT$..flush_buf..BufGuard$GT$17hedcaf5acf9e62584E
liballoc-3c0db88c6c3c2080.rlib(alloc-3c0db88c6c3c2080.alloc.3pxpxezw-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memmove
libstd-e5130808ccb074ce.rlib(std-e5130808ccb074ce.std.cergmp4c-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol memcmp referenced in function _ZN5alloc11collections5btree3map21BTreeMap$LT$K$C$V$GT$6insert17hc358a9b81795c956E
librustc_demangle-a76d77b5d9ed4759.rlib(rustc_demangle-a76d77b5d9ed4759.rustc_demangle.bwbqjk7y-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memcmp
libcore-44bd5dbe4e29964c.rlib(core-44bd5dbe4e29964c.core.7rmb18uk-cgu.0.rcgu.o) : error LNK2001: unresolved external symbol memcmp
libstd-e5130808ccb074ce.rlib(std-e5130808ccb074ce.std.cergmp4c-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol strlen referenced in function _ZN3std3ffi5c_str7CString8from_raw17h67ae0c016f9720bbE
libpanic_unwind-580326a9fdffe4b1.rlib(panic_unwind-580326a9fdffe4b1.panic_unwind.c9nntgss-cgu.0.rcgu.o) : error LNK2019: unresolved external symbol _CxxThrowException referenced in function _ZN12panic_unwind8real_imp5panic17hd7ecf9c6caff3a53E
msvcrt.lib(delete_scalar.obj) : error LNK2019: unresolved external symbol free referenced in function "void __cdecl operator delete(void *)" (??3@YAXPEAX@Z)"

Cargo.toml

[package]
name = "dandra"
version = "0.1.0"
edition = "2018"

[lib]
crate-type = ["cdylib"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
minhook-sys = "0.1.1"
winapi = "0.3.9"
ntapi = "0.3.6"

lib.rs

extern crate alloc;

use winapi::shared::minwindef::*;
use winapi::um::winnt::DLL_PROCESS_ATTACH;

mod dandra;

#[no_mangle]
pub extern "C" fn _DllMainCRTStartup(_h_inst_dll: HINSTANCE, fdw_reason: DWORD, _lpv_reserved: LPVOID) -> BOOL {
    if fdw_reason == DLL_PROCESS_ATTACH {
        dandra::initialize();
    }

    return TRUE;
}

dandra.rs

extern crate alloc;

use minhook_sys::*;

pub fn initialize(){
    unsafe {
        MH_Initialize();
    }
}

Things to try: enabling panic="abort" and lto=true.

I found this issue page which should help you:
https://github.com/rust-lang/rust/issues/54137#issuecomment-521922176

That solved most of the linker errors, but there are still others that are being thrown.

New output:
dandra_but_rust.minhook_sys-6663c81d952708f5.minhook_sys.8jmt0q6j-cgu.0.rcgu.o.rcgu.o : error LNK2019: unresolved external symbol memcpy referenced in function _ZN5alloc3fmt6format17h05374c298f68a70bE
libminhook_sys-6663c81d952708f5.rlib(hook.o) : error LNK2001: unresolved external symbol memcpy
libminhook_sys-6663c81d952708f5.rlib(trampoline.o) : error LNK2001: unresolved external symbol memcpy
dandra_but_rust.minhook_sys-6663c81d952708f5.minhook_sys.8jmt0q6j-cgu.0.rcgu.o.rcgu.o : error LNK2019: unresolved external symbol memset referenced in function ZN4core3fmt3num3imp52$LT$impl$u20$core..fmt..Display$u20$for$u20$u64$GT$3fmt17h6481d312afbe3d1dE
dandra_but_rust.minhook_sys-6663c81d952708f5.minhook_sys.8jmt0q6j-cgu.0.rcgu.o.rcgu.o : error LNK2019: unresolved external symbol memcmp referenced in function _ZN4core3str7pattern11StrSearcher3new17h7ea9ff6487124d59E

You need to link in C library for those symbols. Either msvcrt or libcmt.

Btw, is there a reason you're using _DllMainCRTStartup instead of DllMain?

Yes, for some reason the linker doesn't recognize DllMain as the entry point but it does recognizes _DllMainCRTStartup.

msvcrt is statically linked but it still doesn't work

statically linking libcmt doesn't work either

Try adding ALL of the following libraries: ucrt, vcruntime and msvcrt.

1 Like

Per @chrisd, adding these in lib.rs allowed me to compile under WIndows 10:

#[link(name = "vcruntime")]
extern {}

#[link(name = "ucrt")]
extern {}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.