Skip to content

Commit

Permalink
Docs: "Cannot find source file" in common_issues
Browse files Browse the repository at this point in the history
As described in #580, this error may be encountered when using
corrosion_add_cxxbridge, now that the generated headers are added as
`PUBLIC` sources.
  • Loading branch information
LeonMatthesKDAB authored and jschwe committed Dec 18, 2024
1 parent 6d0b915 commit b88ec09
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions doc/src/common_issues.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- [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)
- [Missing `soname` on Linux for `cdylibs`](#missing-soname-on-linux-for-cdylibs)
- [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)
- [CMake Error (target_link_libraries): Cannot find source file](#cmake-error-target_link_libraries-cannot-find-source-file)

## Linking Debug C/C++ libraries into Rust fails on Windows MSVC targets

Expand Down Expand Up @@ -86,3 +87,36 @@ When building binaries using this shared library, you should set the build rpath
your shared library, e.g. by setting `set(CMAKE_BUILD_RPATH ${YOUR_CUSTOM_OUTPUT_DIRECTORY})` before adding
executables.
For a practical example, you may look at [Slint PR 2455](https://github.com/slint-ui/slint/pull/2455).

## CMake Error (target_link_libraries): Cannot find source file

When using `corrosion_add_cxxbridge`, you may encounter an error similar to this in targets that depend on the cxxbridge target:

```diff
- CMake Error at ...../CMakeLists.txt:61 (target_link_libraries):
- Cannot find source file:
-
- ...../corrosion_generated/..../somefile.h
-
- Tried extensions .c .C .c++ .cc .cpp .cxx .cu .mpp .m .M .mm .ixx .cppm
- .ccm .cxxm .c++m .h .hh .h++ .hm .hpp .hxx .in .txx .f .F .for .f77 .f90
- .f95 .f03 .hip .ispc
```

Where `somefile.h` should be generated by CXX via `corrosion_add_cxxbridge`.
In theory, CMake should already know that this is a generated file and just generate it when needed.

However, in older versions of CMake the `GENERATED` property isn't correctly propagated.
See also: [https://gitlab.kitware.com/cmake/cmake/-/issues/18399](https://gitlab.kitware.com/cmake/cmake/-/issues/18399)

This has since been fixed with CMake 3.20: [https://cmake.org/cmake/help/v3.20/policy/CMP0118.html](https://cmake.org/cmake/help/latest/command/cmake_policy.html#version)
However, the CMake policy CMP0118 must be enabled **in any dependent CMakeLists.txt** for the fix to work.

The best fix is to call:
```cmake
cmake_minimium_required(VERSION 3.20 FATAL_ERROR)
# (or any other version above 3.20)
```
As described [here](https://cmake.org/cmake/help/latest/command/cmake_policy.html#version), this implies a call to `cmake_policy` which enables CMP0118.

Unfortunately this must be done in all (transitive) downstream dependencies that link to the bridge target, so cannot be done from within corrosion automatically.

0 comments on commit b88ec09

Please sign in to comment.