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

error LNK2019: unresolved external symbol printf #2860

Closed
Tastaturtaste opened this issue Aug 3, 2022 · 0 comments · Fixed by #2861
Closed

error LNK2019: unresolved external symbol printf #2860

Tastaturtaste opened this issue Aug 3, 2022 · 0 comments · Fixed by #2861
Labels
C-bug Category: bug

Comments

@Tastaturtaste
Copy link
Contributor

Description

Building a program using printf does not work with the linker error LNK2019: unresolved external symbol printf referenced in function ...

A Minimum Working Example without any dependencies (except libc) that shows the issue

use libc::{c_char, printf};
fn main() {
    unsafe {
        printf(b"Hello World!\n\0" as *const u8 as *const c_char);
    }
}

The target triple

x86_64-pc-windows-msvc

Instructions to reproduce

uploads to github gists, etc.)

  1. Create a new project with cargo new reproduce
  2. cd ./reproduce
  3. cargo add libc
  4. copy the minimum example above in the main.rs file
  5. cargo run

Further information

This issue apparently runs into the same problem.

From this microsoft documentation it seems that the bug depends on the Windows SDK version used:

If you are linking with Windows SDK 8.1 or earlier, you might encounter these unresolved external symbol errors. In that case, you should resolve the error by adding legacy_stdio_definitions.lib to the linker input as described previously.

I successfully tried the fix outlined in this comment by separating out the affected print- and scan- family functions (currently there are only the two functions printf and fprintf) into their own extern "C" block and annotating that with #[cfg_attr(all(windows, target_env = "msvc"), link(name = "legacy_stdio_definitions", kind = "dylib"))].

According to this documentation from microsoft the "legacy_stdio_definitions.lib" is only supplied with Visual Studio 15 and later. I don't know if there is a way to detect at compile time the Visual Studio version used in the toolchain and only conditionally apply the fix I outlined above.

@Tastaturtaste Tastaturtaste added the C-bug Category: bug label Aug 3, 2022
bors added a commit that referenced this issue Aug 4, 2022
Fix for #2860

This PR is intended to fix the issue #2860.
As indicated in the issue, this fix requires linking against "legacy_stdio_definitions.lib", [which is only provided with Visual Studio >=15](https://docs.microsoft.com/en-us/cpp/porting/overview-of-potential-upgrade-issues-visual-cpp?view=msvc-170#libraries). I don't know how this could be checked conditionally at compile time though.
bors added a commit that referenced this issue Aug 4, 2022
Fix for #2860

This PR is intended to fix the issue #2860.
As indicated in the issue, this fix requires linking against "legacy_stdio_definitions.lib", [which is only provided with Visual Studio >=15](https://docs.microsoft.com/en-us/cpp/porting/overview-of-potential-upgrade-issues-visual-cpp?view=msvc-170#libraries). I don't know how this could be checked conditionally at compile time though.
@bors bors closed this as completed in af34f07 Aug 8, 2022
tatref pushed a commit to tatref/libc that referenced this issue Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant