Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unresolved external symbol GetUserProfileDirectoryW #52892

Closed
crusty-dave opened this issue Jul 31, 2018 · 9 comments
Closed

unresolved external symbol GetUserProfileDirectoryW #52892

crusty-dave opened this issue Jul 31, 2018 · 9 comments
Labels
O-windows-msvc Toolchain: MSVC, Operating system: Windows

Comments

@crusty-dave
Copy link

crusty-dave commented Jul 31, 2018

I am hitting the following link error when trying to link a rust library in VS 2017 & VS 2015. I am trying to call an FFI function that is in the library. I don't have any calls to GetUserProfileDirectoryW() in my code.

jims_uri.lib(std-76a5f3e41ba6c505.std2-64a60500296bcccc86151a235f68563b.rs.rcgu.o) : error LNK2019: unresolved external symbol GetUserProfileDirectoryW referenced in function _ZN3std3sys7windows14fill_utf16_buf17hbd7bb37a3f16fbfcE

My suspicion is that I need something in addition to libc? However, if that is the case, it would seem to be a bug? In any case, a work-around would be appreciated. Perhaps an issue with static libraries. Also I am building on Windows 10 / 64-bit.

Cargo.toml

[package]
name = "jims_uri"
version = "0.1.0"
authors = ["me"]

[lib]
crate-type = ["staticlib", "rlib"]

[dependencies]
libc = "0.2.0"
widestring = "0.3.0"
lazy_static = "1.0.2"

url = "1.7.1"

@petrochenkov
Copy link
Contributor

rust-lang/rustup#1468 perhaps?

@crusty-dave
Copy link
Author

Thanks for the link, unfortunately it appears that I am using C:\Program Files (x86)\Windows Kits\8.1\Lib, why won't that work? If I am building my Visual Studio project with the same version, why should Rust care? Am I pulling in something that requires 10\Lib - if so - isn't that a bug?

@retep998
Copy link
Member

GetUserProfileDirectoryW is from userenv.lib which does come with the Windows 8.1 SDK. When linking against a Rust staticlib you must also link against certain libraries that it depends on. Rust used to automatically tell you which libraries you needed, but unfortunately you must now explicitly ask for it by passing --print native-static-libs to Rust.

@crusty-dave
Copy link
Author

Thanks, after a bit of work (google) I figured out how to get the command that I needed to add the option to, however, what do I do with this?

note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms.

note: native-static-libs: advapi32.lib ws2_32.lib userenv.lib shell32.lib msvcrt.lib

Is there a way to specify them in my cargo.toml file?

Thanks again,
-Dave

@retep998
Copy link
Member

You're creating a staticlib in Rust to then use from some other project probably in another language, right? If so, what --print native-static-libs is telling you is what libraries you need to link to when linking to jims_uri.lib from your other project.

@crusty-dave
Copy link
Author

I added userenv.lib to my MSVC project, but I still get the link error:

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol GetUserProfileDirectoryW referenced in function _ZN3std3sys7windows14fill_utf16_buf17h1f8072387770573dE JimsSrxInterface C:\Users\dweaver\git\cic.c\srv\common\JimsSrxInterface\jims_uri.lib(std-a2acd5f5dca269de.std3-89676acb5a26ecabe2ed84f0c51d9c6f.rs.rcgu.o) 1

I will double check it to make sure I didn't screw it up somehow.

@crusty-dave
Copy link
Author

After adding the following, it links, thanks so much for all the help! :)

#pragma comment(lib, "userenv.lib")
#pragma comment(lib, "ws2_32.lib")

This was way too painful...

@rokit
Copy link

rokit commented Mar 22, 2024

For future visitors, here is an easy cargo command to see the libs:

cargo rustc -q -- --print=native-static-libs

-q, --quiet = Do not print cargo log messages

@neon-sunset
Copy link

neon-sunset commented Apr 27, 2024

Figuring this out required way more ceremony that is acceptable...

For future visitors, here is an easy cargo command to see the libs:

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-windows-msvc Toolchain: MSVC, Operating system: Windows
Projects
None yet
Development

No branches or pull requests

5 participants