Comments (8)
How does your CMakeLists.txt look like? Did you do anything like the following:
target_link_libraries(my_cpp_bin PUBLIC my_rust_lib)
from corrosion.
Also please note that no mangle is not sufficient. You need to specify that your Rust function should use the C-ABI, otherwise behavior when calling from C or C++ is undefined.
from corrosion.
Also please note that no mangle is not sufficient. You need to specify that your Rust function should use the C-ABI, otherwise behavior when calling from C or C++ is undefined.
I've added extern "C"
to the function definitions, but I'm getting the same errors. Wasn't aware I needed this though
How does your CMakeLists.txt look like? Did you do anything like the following:
target_link_libraries(my_cpp_bin PUBLIC my_rust_lib)
The CMakeLists.txt
relies on the template, but the one used to build the executable is this:
add_executable(
cli
main.cpp
)
add_executable(dev::cli ALIAS cli)
corrosion_import_crate(
MANIFEST_PATH
${CMAKE_SOURCE_DIR}/rlib/Cargo.toml
)
if(NOT TARGET rlib)
message(FATAL_ERROR " !! rust library not built/imported")
endif()
target_link_libraries(cli PRIVATE dev_options dev_warnings)
target_link_libraries(cli PUBLIC rlib)
target_include_directories(
cli
${WARNING_GUARD}
PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/cli>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/cli>
)
target_compile_features(cli PUBLIC cxx_std_20)
set_target_properties(
cli
PROPERTIES
VERSION ${PROJECT_VERSION}
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
)
if(BUILD_TESTING)
message(TRACE "Building tests...")
add_subdirectory(test)
endif()
add_custom_command(
TARGET
cli
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy $<TARGET_FILE:cli> ${PROJECT_SOURCE_DIR}/target
)
from corrosion.
I've added extern "C" to the function definitions, but I'm getting the same errors. Wasn't aware I needed this though
It's not related to the linking issue. By default Rust functions use the Rust-ABI, which is neither stable nor guaranteed in any way to be compatible with C/C++ ABIs. If your function is Rust-ABI, but you call it from C/C++ the behavior is undefined. For such a simple function as in your example the ABIs will probably be the same, but you can't know that.
I believe the same also applies for C++ code - you should declare the Rust function as having C-ABI. I'm not really thatfamiliar with C++, so perhaps a simple extern
is equivalent with extern "C"
, but I would still recommend writing it out to be explicit.
extern "C" void greet();
extern "C" size_t add(size_t, size_t);
That being said, I'm not sure whats going wrong for you - We do test windows -msvc
with clang-cl and the Ninja Generator in CI.
Could you try removing the custom command that moves the cli target to another location post-build?
add_custom_command(
TARGET
cli
POST_BUILD
COMMAND
${CMAKE_COMMAND} -E copy $<TARGET_FILE:cli> ${PROJECT_SOURCE_DIR}/target
)
from corrosion.
I've added extern "C" to the function definitions, but I'm getting the same errors. Wasn't aware I needed this though
It's not related to the linking issue. By default Rust functions use the Rust-ABI, which is neither stable nor guaranteed in any way to be compatible with C/C++ ABIs. If your function is Rust-ABI, but you call it from C/C++ the behavior is undefined. For such a simple function as in your example the ABIs will probably be the same, but you can't know that.
I believe the same also applies for C++ code - you should declare the Rust function as having C-ABI. I'm not really thatfamiliar with C++, so perhaps a simple
extern
is equivalent withextern "C"
, but I would still recommend writing it out to be explicit.extern "C" void greet(); extern "C" size_t add(size_t, size_t);
Adding extern "C"
to function definitions in C++ source fixed it. I wasn't aware that it needs to be added. Oops.
That being said, I'm not sure whats going wrong for you - We do test windows
-msvc
with clang-cl and the Ninja Generator in CI. Could you try removing the custom command that moves the cli target to another location post-build?add_custom_command( TARGET cli POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:cli> ${PROJECT_SOURCE_DIR}/target )
To be precise, I'm using clang
LLVM binaries, not clang-cl
. Removing the post-build command didn't help on its own. I had to do the above of adding extern "C"
to C++ source.
It does appear I'll have to add a custom command to copy the compiled rust shared library to the same directory that I'm copying the compiled executable to, corrosion doesn't seem to do it on its own
from corrosion.
You should have a look at the Cmake output directory variables, which corrosion will respect and copy the rust artifacts to.
from corrosion.
You should have a look at the Cmake output directory variables, which corrosion will respect and copy the rust artifacts to.
Yeah, I should probably stop using custom commands everywhere. Thank you!
I believe this solves my issue
from corrosion.
Great to hear, closing the issue.
from corrosion.
Related Issues (20)
- Support `$<CONFIG>` in `OUTPUT_DIRECTORY` HOT 6
- [Bug]: Some crate requires AR_<triple> environment to cross-compile HOT 1
- [Bug]: Regression caused by passing AR? commit b72c8ceb52f5d0235ce0fc268cfeff20ce56e8d8? HOT 1
- Upstream Rust language support
- [Bug]:cross compile failed HOT 1
- [Bug]: cross compilation from linux to windows with build tools fails. HOT 4
- [Enhancement]: Vcpkg support HOT 5
- [Bug]: "multiple rules generate" with Ninja Multiconfig in CROSS_CONFIG mode HOT 10
- [Bug]: Macos build with -F rather than -L HOT 4
- unused crates in a workspace not included in package, but still expected HOT 3
- corrosion_import_crate: issue with PROFILE argument in combination with CRATES
- [Bug]: corrosion_link_libraries does not include transitive dependencies HOT 2
- Breaking change to target names containing dashes with Rust 1.79 HOT 3
- corrosion_experimental_cbindgen always triggers rebuilds of files that include the generated header HOT 3
- corrosion_link_libraries behavior vs. expectation HOT 1
- corrosion_set_features did not work HOT 8
- [Bug]: On error, FindRust puts `Rust_FOUND` in wrong scope HOT 1
- Detect and issue a warning if a different corrosion version is already included.
- [Bug]: strip=true and missing pdb files on Windows HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from corrosion.