平台:Mac
项目管理方式:CMake cargo
项目github:CXX-QT
这个项目主要是一个Qt /Qml 和 rust互操作的一个实现。
在编译项目时,链接阶段报错:
QtCore not found.
日志文件:
[main] Building folder: cxx-qt
[build] Starting build
[proc] Executing command: /opt/homebrew/bin/cmake --build /Users/allen/Projects/RustProjects/cxx-qt/build --config Debug --target _cargo-build_qml-features –
[build] [1/2 0% :: 0.002] cd /Users/allen/Projects/RustProjects/cxx-qt/examples/qml_features/rust && /Applications/CMake.app/Contents/bin/cmake -E env CXXQT_EXPORT_DIR=/Users/allen/Projects/RustProjects/cxx-qt/build/examples/qml_features/cxxqt DYLD_FRAMEWORK_PATH=/opt/homebrew/Cellar/qt@5/5.15.12_1/lib QMAKE=/opt/homebrew/Cellar/qt@5/5.15.12_1/bin/qmake CARGO_TARGET_AARCH64_APPLE_DARWIN_LINKER=/usr/bin/clang CC_aarch64-apple-darwin=/usr/bin/clang CXX_aarch64-apple-darwin=/usr/bin/clang++ SDKROOT=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.0.sdk CORROSION_BUILD_DIR=/Users/allen/Projects/RustProjects/cxx-qt/build/examples/qml_features CARGO_BUILD_RUSTC=/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc /Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/bin/cargo rustc --lib --target=aarch64-apple-darwin --package qml-features --manifest-path /Users/allen/Projects/RustProjects/cxx-qt/examples/qml_features/rust/Cargo.toml --target-dir /Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build – -Cdefault-linker-libraries=yes
[build] Compiling cxx-qt-lib v0.6.0 (/Users/allen/Projects/RustProjects/cxx-qt/crates/cxx-qt-lib)
[build] Compiling qml-features v0.1.0 (/Users/allen/Projects/RustProjects/cxx-qt/examples/qml_features/rust)
[build] error: linking with/usr/bin/clang
failed: exit status: 1
[build] |
[build] = note: env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL=“C” PATH=“/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Users/allen/.cargo/bin:/Users/allen/Library/Application Support/JetBrains/Toolbox/scripts:/Users/allen/Library/Python/3.9/bin” VSLANG=“1033” ZERO_AR_DATE=“1” “/usr/bin/clang” “-arch” “arm64” “/var/folders/hw/lc023pr55psdqktjg68xvnkw0000gn/T/rustc7cGQY5/symbols.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.10fl17pzfgknotu.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.119okv1xmy7z81f6.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.137xat5v19szbkn0.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.15oph1vhrzhh5e5j.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.19opgb164thgeyxk.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.1vlp0hyqz47obvqu.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.1yhg74qrm5jbn57.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.26tes9igtz8fy7rn.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.2mfoke3mrp14psgs.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.2xsado67lvou67qs.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.2zb7b71tju74uh5j.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.37xis5uwom24c582.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.3qh0u66rpovuphck.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.4186phjz8kddzw0h.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.42i2ewundsav9ojs.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.45d9edr6wy3brpeh.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.4aqs871owq9ftsf1.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.4zbmzfjwshprgoiz.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.4zm0s84o8zrdyxpo.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.55605lsdx0j8mjk3.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.55mmkitcjyl348i7.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.5663x8hp4g2zm7jf.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.59c517g8ydpmusil.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.xhcy1h0fk613fhe.rcgu.o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7.4qr33kompxeqtcjw.rcgu.o” “-L” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/deps” “-L” “/opt/homebrew/Cellar/qt@5/5.15.12_1/lib” “-L” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-cd4fe937a4be2c39/out” “-L” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-201f66f53e1e6176/out” “-L” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/link-cplusplus-71603c78c2dde3e1/out” “-L” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcxx_qt_lib_headers-d9912cca892c1c19.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcxx_qt_build-6ab1336d900c684d.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libversion_check-9f51aa7577df62b5.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcxx_qt-d2c539d1a4753f86.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libstatic_assertions-21ebe0eb3cea606d.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcxx-0dae514a5f69b4e8.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/liblink_cplusplus-29d675dabc421268.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libqt_build_utils-906dd61ff2dbfcbc.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libversions-8466793749c2eea6.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libnom-718756c2487eced8.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libmemchr-e6b5ebc697c65687.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libitertools-e621136652a36976.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libeither-f70f4bfdd6077160.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcc-4f7e5addbf99ef3e.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libjobserver-dd4b614b2cf16aea.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/liblibc-dd2450870a8e46be.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcxx_gen-5eee1925fa27fac5.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcxx_qt_gen-8d441a361f4b6960.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libclang_format-089b44882994e1ab.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libthiserror-dd35499ccfe2017d.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libconvert_case-35c30cdcabc87c2a.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libunicode_segmentation-7d3ed2ca008d237f.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libsyn-ac3b92559e3bce53.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libquote-f7d359d29bfc9b4a.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libcodespan_reporting-16803727eb35ee92.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libunicode_width-16a1984432142341.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libtermcolor-3d23a97142594491.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libproc_macro2-950e5ceb51725b27.rlib” “/Users/allen/Projects/RustProjects/cxx-qt/build/cargo/build/debug/deps/libunicode_ident-2260bb06186abcf3.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libproc_macro-c7150b0669ad7a0f.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd-4f192af08beaa759.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libpanic_unwind-401344856f0fc3c5.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libobject-9e012890a70780c2.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libmemchr-60338fe528589910.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libaddr2line-a2e883cf5bcf6347.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libgimli-df8440ef641907f8.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_demangle-fa2288bf7728f91c.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libstd_detect-f39c850853dd7dfe.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libhashbrown-c5b1930e10569ddd.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_alloc-96bad2fc8f58f610.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libminiz_oxide-06eec439eca0e252.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libadler-023007819562a373.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libunwind-7254491d238c3b3e.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcfg_if-9f474e4309122405.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liblibc-0bfd536a501ea558.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/liballoc-d56621fef414e9f8.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/librustc_std_workspace_core-60d6843fd08e9533.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcore-62a9b4d83244cb2b.rlib” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib/libcompiler_builtins-4010f8e9d1f7f9aa.rlib” “-framework” “QtCore” “-framework” “DiskArbitration” “-framework” “IOKit” “-lc++” “-liconv” “-lSystem” “-lc” “-lm” “-L” “/Users/allen/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/aarch64-apple-darwin/lib” “-o” “/Users/allen/Projects/RustProjects/cxx-qt/build/./cargo/build/debug/build/cxx-qt-lib-6020208f1a041cc7/build_script_build-6020208f1a041cc7” “-Wl,-dead_strip” “-nodefaultlibs”
[build] = note: ld: framework ‘QtCore’ not found
[build] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[build]
[build]
[build] error: linking with/usr/bin/clang
failed: exit status: 1
[build] |
首先查看日志数据,发现
“-L” “/opt/homebrew/Cellar/qt@5/5.15.12_1/lib”
说明CMake已经找到了qt的lib目录,查看对应目录的内容,
tree -L 1 /opt/homebrew/Cellar/qt@5/5.15.12_1/lib | grep QtCore
output:
QtCore.framework
发现有对应的库,但是库是framework结构,而不是.a 或.lib等结构。
再看编译命令:
“-framework” “QtCore” “-framework” “DiskArbitration” “-framework” “IOKit”
发现已经添加了-framework标记来链接这个库,按理说应该可以找到这个库。
写一个最简单的my_demo.cpp
#include
#include
int main(int argc,char** argv)
{
QObject* obj = new QObject();
obj->setObjectName("obj");
std::cout<<"Hello world !"<
my_demo.cpp
从上面的clang命令中提取关键编译信息,
执行如下命令进行编译这个项目:
/usr/bin/clang -arch arm64 -o my_demo my_demo.cpp
-L/opt/homebrew/Cellar/qt@5/5.15.12_1/lib
-framework QtCore -framework DiskArbitration -framework IOKit
-lc++
output:
// fatal error: ‘QtCore/qobject.h’ file not found
发现确实有这个问题,但是明明指定了链接的目录-L 也指定了链接的库 -framework
那么为什么不行呢?
通过QtCreator创建一个CMkae管理的Qt项目,然后进行编译,查看build目录下的compile_commands.json
查看具体执行的编译命令。
最后发现,可以编译通过的Qt项目,
-iframework /Users/allen/Soft/Qt/6.5.3/macos/lib
他们指定目录时用的是-iframework 参数,而不是-L。
查看clang手册,
-iframework
Add directory to SYSTEM framework search path
发现-iframework和-L确实不一样,-iframework是mac平台下专门为了查找framework指定目录的一个参数,需要用-iframework指定framework的所在目录,然后使用-framework才能链接到这个库。
继续翻,发现
-F
Add directory to framework include search path
就是说俩有点区别,但都是用来指定framework目录的,用my_demo 发现都可以编译通过。
已经找到了错误问题,开始着手处理问题。
回头查看日志,发现报错是在
[build] Compiling qml-features v0.1.0 (/Users/allen/Projects/RustProjects/cxx-qt/examples/qml_features/rust)
进入对应的CMakeLists.txt文件,找一下对应的地方:
set(CRATE qml-features)
corrosion_import_crate(MANIFEST_PATH rust/Cargo.toml CRATES ${CRATE})
set(CXXQT_EXPORT_DIR "${CMAKE_CURRENT_BINARY_DIR}/cxxqt")
corrosion_set_env_vars(${CRATE}
"CXXQT_EXPORT_DIR=${CXXQT_EXPORT_DIR}"
"QMAKE=${QMAKE}"
$<$:RUSTC_WRAPPER=${CMAKE_RUSTC_WRAPPER}>
)
发现这里通过使用CMake的函数corrosion_import_crate 将rust的Cargo.toml导入进去,应该是这里处理的rust项目。
进入corrosion库目录,查看对应的函数、文档,发现了很多接口,如下是我试验的一些过程:
结果是不生效。
对于实际执行的clang命令没有任何改变!
rustflags = ["-C", "link-arg=-F/opt/homebrew/Cellar/qt@5/5.15.12_1/lib"]
在根目录增加.cargo/config文件,添加上述内容。
结果是不生效。
对于实际执行的clang命令没有任何改变!
在这里qml-feature/rust/build.rs
增加rust的编译内容。
结果是不生效。
对于实际执行的clang命令没有任何改变!
当天搞到了晚上一点多,虽然找到了问题,但是却怎么弄都不行,睡了一觉第二天回想一下这个问题,发现自己有这么几点没搞明白:
1 为什么cmake不会改变clang的执行命令?
2 cmake是如何调用了rust的编译器,又是如何调用到最后的clang命令的?
通过查看corrosion这个库的函数,发现CMake最后通过run_custom_command的方式,调用了cargo , 然后cargo调用rustc 然后调用了clang。我去,给我整郁闷了。
打开对应的corrosion库,在里面的多个地方增加对应的函数,尝试编译。
发现都没有效果😭
为什么就是不能用呢?
去cargo 官方搜索文档,
google
cargo link framework
发现buildscript中有这么一个参数:
cargo:rustc-link-search=[KIND=]PATH
The rustc-link-search instruction tells Cargo to pass the -L flag to the compiler to add a directory to the library search path.The optional KIND may be one of dependency, crate, native, framework,
or all. See the rustc book for more detail.These paths are also added to the dynamic library search path
environment variable if they are within the OUT_DIR. Depending on this
behavior is discouraged since this makes it difficult to use the
resulting binary. In general, it is best to avoid creating dynamic
libraries in a build script (using existing system libraries is fine).
Ok,找到这个参数了,剩下的就是怎么给他放进去了。
其中的过程不赘述了,
在crates/cxx-qt的build.rs中
加入
println!("cargo:rustc-link-search=framework=/opt/homebrew/Cellar/qt@5/5.15.12_1/lib");
- 1
最后编译即可正常。但是这个不是最好的办法,最好应该是让他自动解决这个符号。
后续再说吧,搞了四天才搞定这个麻烦问题。
没事少碰还不是很完善的东西。
我指的是QML和rust互操作。Rust还是很好用的。