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

Linking C library in Windows #658

Closed
makarr opened this issue Aug 27, 2023 · 4 comments
Closed

Linking C library in Windows #658

makarr opened this issue Aug 27, 2023 · 4 comments

Comments

@makarr
Copy link

makarr commented Aug 27, 2023

I've been using the C API on Mac and Linux without issues but when I try to link on Windows I get "unresolved symbol" errors that seem to refer to Windows system libraries. With rustup==1.72.0 I get 32 errors, while with rustup==1.69.0 I get 29 errors, so maybe the problem lies on their end. I am making sure to set the RUSTFLAGS env variable following a previous issue.

Meanwhile I can build and run resvg.exe on Windows without any problems. Perplexing!

I setup a simple test repo here: https://github.com/makarr/resvg_lib_test

Even if the fault does not lie with resvg I would be thrilled if someone could explain how to get the C API to work on Windows.

@RazrFalcon
Copy link
Owner

RazrFalcon commented Aug 28, 2023

Here are the Win libraries tools/viewsvg links:

-lWs2_32 -lAdvapi32 -lBcrypt -lUserenv -lNtdll

I do plan to write build instructions eventually: #564 But I'm not using Windows, so I'm not sure what happens in your case. I would assume you have to link Win libraries (for some reason) required by Rust std, because resvg by itself doesn't need any.

@makarr
Copy link
Author

makarr commented Aug 28, 2023

Steps to build resvg C library:

  1. Install Rust on Windows 10. (Rust version 1.72.0)
  2. git clone https://github.com/RazrFalcon/resvg.git
  3. cd resvg/c-api
  4. $ENV:RUSTFLAGS = "-lWs2_32 -lAdvapi32 -lBcrypt -lUserenv -lNtdll -C target-feature=+crt-static"
  5. cargo build --release

Steps to reproduce issue:

  1. Install CMake (>= 3.22) on Windows 10
  2. git clone https://github.com/makarr/resvg_lib_test.git
  3. Copy resvg\target\release\resvg.lib to resvg_lib_test\resvg
  4. cd resvg_lib_test
  5. cmake -S . -B build
  6. cmake --build build

@RazrFalcon
Copy link
Owner

RazrFalcon commented Aug 28, 2023

You have to link those libs to your executable, not to Rust/resvg. For some reason, afaiu, Rust doesn't propagate those library dependencies to the final binary. Maybe because of Windows, maybe because you use static linking. I don't know. I don't have Windows to test it.

You might want to ask about it in Rust/Cargo repos. This is not a resvg issue, but rather the way Rust builds C libraries on Windows. I don't think there is anything I can do on my side. At least I'm not aware of it.

PS: Passing libs to $ENV:RUSTFLAGS has no effect anyway.

@makarr
Copy link
Author

makarr commented Aug 28, 2023

You're 100% right. Fixed it by linking the Windows system libraries in CMake. Closing the issue.

@makarr makarr closed this as completed Aug 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants