16021
|
1 |
# Commonly encountered (Non-Corrosion) Issues
|
|
2 |
|
|
3 |
## Table of Contents
|
|
4 |
|
|
5 |
- [Linking Debug C/C++ libraries into Rust fails on Windows MSVC targets](#linking-debug-cc-libraries-into-rust-fails-on-windows-msvc-targets)
|
|
6 |
- [Linking Rust static libraries into Debug C/C++ binaries fails on Windows MSVC targets](#linking-rust-static-libraries-into-debug-cc-binaries-fails-on-windows-msvc-targets)
|
|
7 |
- [Missing `soname` on Linux for `cdylibs`](#missing-soname-on-linux-for-cdylibs)
|
|
8 |
- [Missing `install_name` on MacOS for `ccdylibs` / Hardcoded references to the build-directory](#missing-installname-on-macos-for-ccdylibs--hardcoded-references-to-the-build-directory)
|
|
9 |
|
|
10 |
## Linking Debug C/C++ libraries into Rust fails on Windows MSVC targets
|
|
11 |
|
|
12 |
`rustc` always links against the non-debug Windows runtime on `*-msvc` targets.
|
|
13 |
This is tracked [in this issue](https://github.com/rust-lang/rust/issues/39016)
|
|
14 |
and could be fixed upstream.
|
|
15 |
|
|
16 |
A typical error message for this issue is:
|
|
17 |
|
|
18 |
```
|
|
19 |
Compiling rust_bin v0.1.0 (D:\a\corrosion\corrosion\test\cxxbridge\cxxbridge_cpp2rust\rust)
|
|
20 |
error: linking with `link.exe` failed: exit code: 1319
|
|
21 |
[ redacted ]
|
|
22 |
= note: cxxbridge-cpp.lib(lib.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in libcxx-bafec361a1a30317.rlib(cxx.o)
|
|
23 |
|
|
24 |
cxxbridge-cpp.lib(lib.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MD_DynamicRelease' in libcxx-bafec361a1a30317.rlib(cxx.o)
|
|
25 |
|
|
26 |
cpp_lib.lib(cpplib.cpp.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in libcxx-bafec361a1a30317.rlib(cxx.o)
|
|
27 |
|
|
28 |
cpp_lib.lib(cpplib.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MD_DynamicRelease' in libcxx-bafec361a1a30317.rlib(cxx.o)
|
|
29 |
|
|
30 |
msvcrt.lib(initializers.obj) : warning LNK4098: defaultlib 'msvcrtd.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
|
|
31 |
```
|
|
32 |
|
|
33 |
### Solutions
|
|
34 |
|
|
35 |
One solution is to also use the non-debug version when building the C/C++ libraries.
|
|
36 |
You can set the [MSVC_RUNTIME_LIBRARY] target properties of your C/C++ libraries to the non-debug variants.
|
|
37 |
By default you will probably want to select the `MultiThreadedDLL` variant, unless you specified
|
|
38 |
[`-Ctarget-feature=+crt-static`](https://rust-lang.github.io/rfcs/1721-crt-static.html) in your
|
|
39 |
`RUSTFLAGS`.
|
|
40 |
|
|
41 |
|
|
42 |
[MSVC_RUNTIME_LIBRARY]: https://cmake.org/cmake/help/latest/prop_tgt/MSVC_RUNTIME_LIBRARY.html#prop_tgt:MSVC_RUNTIME_LIBRARY
|
|
43 |
|
|
44 |
## Linking Rust static libraries into Debug C/C++ binaries fails on Windows MSVC targets
|
|
45 |
|
|
46 |
This issue is quite similar to the previous one, except that this time it's a Rust library being linked
|
|
47 |
into a C/C++ target. If it's 100% only Rust code you likely won't even have any issues.
|
|
48 |
However, if somewhere in the dependency graph C/C++ code is built and linked into your Rust library,
|
|
49 |
you will likely encounter this issue. Please note, that using [cxx] counts as using C++ code and will
|
|
50 |
lead to this issue.
|
|
51 |
|
|
52 |
The previous solution should also work for this case, but additionally you [may also
|
|
53 |
have success](https://github.com/rust-lang/rust/issues/39016#issuecomment-853964918) by using
|
|
54 |
`corrosion_set_env_vars(your_rust_lib "CFLAGS=-MDd" "CXXFLAGS=-MDd")` (or `-MTd` for a statically linked
|
|
55 |
runtime).
|
|
56 |
For debug builds, this is likely to be the preferable solution. It assumes that downstream C/C++ code
|
|
57 |
is built by the `cc` crate, which respects the `CFLAGS` and `CXXFLAGS` environment variables.
|
|
58 |
|
|
59 |
[cxx]: https://github.com/dtolnay/cxx
|
|
60 |
|
|
61 |
|
|
62 |
## Missing `soname` on Linux for `cdylibs`
|
|
63 |
|
|
64 |
Cargo doesn't support setting the `soname` field for cdylib, which may cause issues.
|
|
65 |
You can set the soname manually by passing a linker-flag such as `-Clink-arg=-Wl,-soname,libyour_crate.so`
|
|
66 |
to the linker via `corrosion_add_target_local_rustflags()` and additionally seting the `IMPORTED_SONAME`
|
|
67 |
property on the import CMake target:
|
|
68 |
```
|
|
69 |
set_target_properties(your_crate-shared PROPERTIES IMPORTED_SONAME libyour_crate.so)
|
|
70 |
```
|
|
71 |
Replace `your_crate` with the name of your shared library as defined in the `[lib]` section of your Cargo.toml
|
|
72 |
Manifest file.
|
|
73 |
|
|
74 |
Attention: The Linux section may not be entirely correct, maybe `$ORIGIN` needs to be added to the linker arguments.
|
|
75 |
Feel free to open a pull-request with corrections.
|
|
76 |
|
|
77 |
## Missing `install_name` on MacOS for `ccdylibs` / Hardcoded references to the build-directory
|
|
78 |
|
|
79 |
The solution here is essentially the same as in the previous section.
|
|
80 |
```
|
|
81 |
corrosion_add_target_local_rustflags(your_crate -Clink-arg=-Wl,-install_name,@rpath/libyour_crate.dylib,-current_version,${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR},-compatibility_version,${PROJECT_VERSION_MAJOR}.0)
|
|
82 |
set_target_properties(your_crate-shared PROPERTIES IMPORTED_NO_SONAME 0)
|
|
83 |
set_target_properties(your_crate-shared PROPERTIES IMPORTED_SONAME libyour_crate.dylib)
|
|
84 |
```
|
|
85 |
When building binaries using this shared library, you should set the build rpath to the output directory of
|
|
86 |
your shared library, e.g. by setting `set(CMAKE_BUILD_RPATH ${YOUR_CUSTOM_OUTPUT_DIRECTORY})` before adding
|
|
87 |
executables.
|
|
88 |
For a practical example, you may look at [Slint PR 2455](https://github.com/slint-ui/slint/pull/2455).
|