Giter Club home page Giter Club logo

Comments (8)

jschwe avatar jschwe commented on July 20, 2024

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.

jschwe avatar jschwe commented on July 20, 2024

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.

ClayCore avatar ClayCore commented on July 20, 2024

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.

jschwe avatar jschwe commented on July 20, 2024

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.

ClayCore avatar ClayCore commented on July 20, 2024

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);

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.

jschwe avatar jschwe commented on July 20, 2024

You should have a look at the Cmake output directory variables, which corrosion will respect and copy the rust artifacts to.

from corrosion.

ClayCore avatar ClayCore commented on July 20, 2024

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.

jschwe avatar jschwe commented on July 20, 2024

Great to hear, closing the issue.

from corrosion.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.