mystor / rust-cpp

Embed C++ directly inside your rust code!
Apache License 2.0
807 stars 44 forks source link

Link error on MacOS arm64 #107

Open lylythechosenone opened 1 year ago

lylythechosenone commented 1 year ago

Using this crate with qmetaobject on MacOS arm64, I get this strange error:

= note: ld: warning: ignoring file /Users/lysan/edit/target/debug/build/edit-ed5f1e2f905ca358/out/librust_cpp_generated.a, building for macOS-arm64 but attempting to link with file built for unknown-unsupported file format ( 0x21 0x3C 0x61 0x72 0x63 0x68 0x3E 0x0A 0x2F 0x20 0x20 0x20 0x20 0x20 0x20 0x20 )
          Undefined symbols for architecture arm64:
            "___cpp_closure_17188711627517009258", referenced from:
                edit::main::h358ea517e810126f in edit-5673f38a0ffa045a.3yv251ocyfeukzqw.rcgu.o
          ld: symbol(s) not found for architecture arm64

Here's my build.rs (mostly copied from qmetaobject):

/* Copyright (C) 2018 Olivier Goffart <ogoffart@woboq.com>
   Copyright (C) 2023 Lysander Mealy <lysander.mealy@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

use semver::Version;

fn main() {
    let qt_include_path = std::env::var("DEP_QT_INCLUDE_PATH").unwrap();
    let qt_version = std::env::var("DEP_QT_VERSION")
        .unwrap()
        .parse::<Version>()
        .expect("Parsing Qt version failed");

    let mut config = cpp_build::Config::new();
    for f in std::env::var("DEP_QT_COMPILE_FLAGS")
        .unwrap()
        .split_terminator(';')
    {
        config.flag(f);
    }
    config.include(&qt_include_path).build("src/main.rs");

    for minor in 7..=15 {
        if qt_version >= Version::new(5, minor, 0) {
            println!("cargo:rustc-cfg=qt_{}_{}", 5, minor);
        }
    }
    let mut minor = 0;
    while qt_version >= Version::new(6, minor, 0) {
        println!("cargo:rustc-cfg=qt_{}_{}", 6, minor);
        minor += 1;
    }
}

and my actual code:

let mut component = QmlComponent::new(&engine);
component.load_url(
    QUrl::from(QString::from("ui/themes/Dark.qml")),
    CompilationMode::PreferSynchronous,
);
let object = component.create();
let variant = cpp!(unsafe [object as "QObject*"] -> QVariant as "QVariant" {
    return QVariant::fromValue(object);
});
engine.set_property("theme".into(), variant);
ogoffart commented 1 year ago

I cannot explain this bug looking at this code. The ___cpp_closure_17188711627517009258 is probably the symbol created for your cpp! macro. It should be present in the generated .cpp file, could you check that?

lylythechosenone commented 1 year ago

I think the important part here is not the "Undefined symbols..." but the "ignoring file..."

The symbols are present in the file, the file is just ignored.

ogoffart commented 1 year ago

I've never seen this message before. If I search for this message I found https://github.com/rust-lang/cc-rs/issues/680

Let me know if you find the solution and if something should be changed in the crate.