Skip to content

wrong link_name for msvc #578

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

Closed
Dushistov opened this issue Mar 12, 2017 · 6 comments
Closed

wrong link_name for msvc #578

Dushistov opened this issue Mar 12, 2017 · 6 comments

Comments

@Dushistov
Copy link
Contributor

Dushistov commented Mar 12, 2017

Input C Header

extern void test(void);

Bindgen Invocation

    let mut bindings: Builder = bindgen::builder().header(c_file_path.to_str().unwrap());
    bindings = include_dirs.iter().fold(bindings, |acc, x| acc.clang_arg("-I".to_string() + x.as_ref().to_str().unwrap()));

    bindings = bindings
        .no_unstable_rust()
        .raw_line("#![allow(non_upper_case_globals, dead_code, non_camel_case_types, improper_ctypes)]")
        ;
    bindings = c_headers[1..].iter().fold(Ok(bindings), |acc: Result<Builder, String>, header| {
        let c_file_path = search_file_in_directory(include_dirs, header)
                               .map_err(|_| format!("Can not find {}", header))?;
        let c_file_str = c_file_path.to_str()
            .ok_or_else(|| format!("Invalid unicode in path to {}", header))?;
        Ok(acc.unwrap().clang_arg("-include").clang_arg(c_file_str))
    })?;
    
    let generated_bindings = bindings.generate().map_err(|_| "Failed to generate bindings".to_string())?;
    generated_bindings.write_to_file(output_rust).map_err(|err| err.to_string())?;

Actual Results

extern "C" {
    #[link_name = "_test"]
    pub fn test();
}
error LNK2019: unresolved external symbol __test

Expected Results

linking without any errors.

Hint the problem in two underscores instead of one,
looks like rustc (at least 1.16) smart enought to add "_" to names.
So if remove #[link_name = "_test"] all works fine,
but I can not find option to strip link_name from output.

@emilio
Copy link
Contributor

emilio commented Mar 12, 2017

Which clang version are you using?

You can workaround this with --distrust-clang-mangling

@Dushistov
Copy link
Contributor Author

@emilio

Which clang version are you using?

C:\Users\IEUser\projects\link_name_bug\mylib-sys\src>clang --version
clang version 3.9.0 (branches/release_39)
Target: i686-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

You can workaround this with --distrust-clang-mangling

I can not, with/without this option it generates the same:

C:\Users\IEUser\projects\link_name_bug\mylib-sys\src>bindgen --version
bindgen 0.22.1

C:\Users\IEUser\projects\link_name_bug\mylib-sys\src>bindgen test.h
/* automatically generated by rust-bindgen */

extern "C" {
    #[link_name = "_test"]
    pub fn test();
}

C:\Users\IEUser\projects\link_name_bug\mylib-sys\src>bindgen --distrust-clang-mangling test.h
/* automatically generated by rust-bindgen */

extern "C" {
    #[link_name = "_test"]
    pub fn test();
}

@emilio
Copy link
Contributor

emilio commented Mar 12, 2017

Whoopsies, that's my fault. Could you confirm it works with #579?

Also, I'm intrigued by this anyway, given we used to have this remove first underscore of the mangling thing also for windows (see https://github.com/servo/rust-bindgen/blob/master/src/ir/function.rs#L133), but it was removed because it seemed no longer necessary...

Does cd bindgen-integration && cargo test work for you now? and adding windows to that condition?

@Dushistov
Copy link
Contributor Author

Could you confirm it works with #579?

Yes, with --distrust-clang-mangling and #579 it works

Does cd bindgen-integration && cargo test work for you now?

It works on my host machine (with linux) without #579,

and it failed on windows inside virtualbox with #579:

--- stderr
thread 'main' panicked at 'unsupported calling convention: 4', C:\Users\IEUser\p
rojects\rust-bindgen\src\ir\function.rs:109
note: Run with `RUST_BACKTRACE=1` for a backtrace.

@fitzgen
Copy link
Member

fitzgen commented Jul 21, 2017

I think all that is required to repro is this:

// bindgen-flags: -- --target i686-pc-windows-msvc
extern void test(void);

@fitzgen
Copy link
Member

fitzgen commented Jul 21, 2017

Duping this to #541 which is the same root issue but has more discussion.

@fitzgen fitzgen closed this as completed Jul 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants