Skip to content

Commit

Permalink
cxx-qt-build: use only link when building
Browse files Browse the repository at this point in the history
  • Loading branch information
ahayzen-kdab committed Sep 9, 2024
1 parent 62757b5 commit 02dff8b
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions crates/cxx-qt-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -752,9 +747,7 @@ impl CxxQtBuilder {
qtbuild: &mut qt_build_utils::QtBuild,
generated_header_dir: impl AsRef<Path>,
header_prefix: &str,
) -> Vec<String> {
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!");
Expand Down Expand Up @@ -893,13 +886,16 @@ 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
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) {
Expand Down Expand Up @@ -1088,18 +1084,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(),
Expand All @@ -1113,6 +1104,12 @@ 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
println!("cargo::rustc-link-arg=-l{}", static_lib_name());

self.cc_builder.compile(&static_lib_name());
}

Expand Down

0 comments on commit 02dff8b

Please sign in to comment.