diff --git a/crates/cxx-qt-build/src/lib.rs b/crates/cxx-qt-build/src/lib.rs index f75386c45..2667224c9 100644 --- a/crates/cxx-qt-build/src/lib.rs +++ b/crates/cxx-qt-build/src/lib.rs @@ -732,11 +732,6 @@ impl CxxQtBuilder { }); } else { println!("cargo::rustc-link-arg={}", obj_file.to_string_lossy()); - // The linker argument order matters! - // We need to link the object file first, then link the static library. - // Otherwise, the linker will be unable to find the symbols in the static library file. - // See also: https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc - println!("cargo::rustc-link-arg=-l{}", static_lib_name()); } } else { panic!( @@ -752,9 +747,7 @@ impl CxxQtBuilder { qtbuild: &mut qt_build_utils::QtBuild, generated_header_dir: impl AsRef, header_prefix: &str, - ) -> Vec { - let mut qml_libraries = vec![]; - + ) { for qml_module in &self.qml_modules { dir::clean(dir::module_target(&qml_module.uri)) .expect("Failed to clean qml module export directory!"); @@ -893,13 +886,18 @@ impl CxxQtBuilder { // Build the QML module as a library if cc_builder.get_files().count() > 0 { let qml_library_name = qml_module_static_lib_name(&qml_module.uri); - cc_builder.compile(&qml_library_name); - qml_libraries.push(qml_library_name); + // The linker argument order matters! + // We need to link the object file first, then link the static library. + // Otherwise, the linker will be unable to find the symbols in the static library file. + // See also: https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc + if !dir::is_exporting() { + println!("cargo::rustc-link-arg=-l{}", &qml_library_name); + } + + cc_builder.compile(&qml_library_name); } } - - qml_libraries } fn setup_qt5_compatibility(&mut self, qtbuild: &qt_build_utils::QtBuild) { @@ -1088,18 +1086,13 @@ impl CxxQtBuilder { // Bridges for QML modules are handled separately because // the metatypes_json generated by moc needs to be passed to qmltyperegistrar - let qml_libraries = self.build_qml_modules( + self.build_qml_modules( &init_builder, &mut qtbuild, &header_root, &self.include_prefix.clone(), ); - // Link all of the qml libraries - for qml_library in &qml_libraries { - println!("cargo:rustc-link-lib={qml_library}"); - } - let mut initializers = self.generate_cpp_from_qrc_files(&mut qtbuild); initializers.extend(dependencies::initializer_paths( self.public_interface.as_ref(), @@ -1113,6 +1106,14 @@ impl CxxQtBuilder { // Only compile if we have added files to the builder // otherwise we end up with no static library but ask cargo to link to it which causes an error if self.cc_builder.get_files().count() > 0 { + // The linker argument order matters! + // We need to link the object file first, then link the static library. + // Otherwise, the linker will be unable to find the symbols in the static library file. + // See also: https://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc + if !dir::is_exporting() { + println!("cargo::rustc-link-arg=-l{}", static_lib_name()); + } + self.cc_builder.compile(&static_lib_name()); }