Q&A
前提
C言語からRustコードを呼び出すプログラムを作成しています。
共有ライブラリはビルドできたのですがその後CMakeでリンクが上手くできていないのかビルドできません。
発生している問題・エラーメッセージ
Bash
1[ 50%] Building CXX object CMakeFiles/Main.dir/main.cc.obj 2[100%] Linking CXX executable Main.exe 3c:/mingw64/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: CMakeFiles\Main.dir/objects.a(main.cc.obj):main.cc:(.text+0xc): undefined reference to `rust_function' 4collect2.exe: error: ld returned 1 exit status 5CMakeFiles\Main.dir\build.make:98: recipe for target 'Main.exe' failed 6mingw32-make.exe[2]: *** [Main.exe] Error 1 7CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/Main.dir/all' failed 8mingw32-make.exe[1]: *** [CMakeFiles/Main.dir/all] Error 2 9Makefile:89: recipe for target 'all' failed 10mingw32-make.exe: *** [all] Error 2
該当のソースコード
toml
1# Cargo.toml 2[package] 3name = "Aeration" 4version = "0.1.0" 5edition = "2021" 6 7[lib] 8crate-type = ["cdylib"] 9 10# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 11 12[dependencies]
Rust
1// lib.rs 2#[no_mangle] 3pub extern "C" fn rust_function() { 4 println!("hello, from rust!"); 5}
C++
1// main.cc 2 3#include <stdio.h> 4#include "my_header.h" 5 6int main(int argc, char **argv) 7{ 8 rust_function(); 9 return 0; 10}
C
1// my_header.h 2 3#include <cstdarg> 4#include <cstdint> 5#include <cstdlib> 6#include <ostream> 7#include <new> 8 9extern "C" { 10 11void rust_function(); 12 13} // extern "C" 14
CMake
1cmake_minimum_required(VERSION 3.16) 2set(CMAKE_CXX_STANDARD 17) 3project(Main) 4add_executable(Main main.cc my_header.h) 5 6target_link_libraries(Main "${CMAKE_SOURCE_DIR}/libAeration.a")
bash
1フォルダー パスの一覧: ボリューム Windows 2ボリューム シリアル番号は CC12-C203 です 3C:. 4 CMakeLists.txt 5 libAeration.a 6 main.cc 7 my_header.h 8 9サブフォルダーは存在しません
試したこと
こちらのサイトを参考にしました
https://tomoyuki-nakabayashi.github.io/book/interoperability/rust-with-c.html
補足情報(FW/ツールのバージョンなど)
OS: Windows 11
Rust:
version: 1.66.0
toolchain: stable-x86_64-pc-windows-gnu
CMake: 3.25.1
あなたの回答
tips
プレビュー