PCRE2Project / pcre2

PCRE2 development is now based here.
Other
922 stars 194 forks source link

build error with Undefined symbols #241

Closed BruceChar closed 1 year ago

BruceChar commented 1 year ago

OS: Macos M2 pcre2: latest as a git submodule I want to use pcre2 in my rust library as FFI binding. I build manually as following:

const FILES: &'static [&'static str] = &[
    "pcre2_auto_possess.c",
    "pcre2_compile.c",
    "pcre2_config.c",
    "pcre2_context.c",
    "pcre2_convert.c",
    "pcre2_dfa_match.c",
    "pcre2_error.c",
    "pcre2_extuni.c",
    "pcre2_find_bracket.c",
    "pcre2_jit_compile.c",
    "pcre2_maketables.c",
    "pcre2_match.c",
    "pcre2_match_data.c",
    "pcre2_newline.c",
    "pcre2_ord2utf.c",
    "pcre2_pattern_info.c",
    "pcre2_script_run.c",
    "pcre2_serialize.c",
    "pcre2_string_utils.c",
    "pcre2_study.c",
    "pcre2_substitute.c",
    "pcre2_substring.c",
    "pcre2_tables.c",
    "pcre2_ucd.c",
    "pcre2_valid_utf.c",
    "pcre2_xclass.c",
];

fn main() {
    println!("cargo:rerun-if-env-changed=PCRE2_SYS_STATIC");

    println!("cargo:rustc-link-search=pcre2-sys/target/");

    let target = env::var("TARGET").unwrap();
    let out = PathBuf::from("./target");

    // make sure our pcre2 submodule has been loaded.
    if has_git() && !Path::new("pcre2/.git").exists() {
        Command::new("git")
            .args(&["submodule", "update", "--init"])
            .status()
            .unwrap();
    }

    let mut builder = cc::Build::new();
    builder
        .out_dir("./target")
        .define("PCRE2_CODE_UNIT_WIDTH", "8")
        .define("HAVE_STDLIB_H", "1")
        .define("HAVE_MEMMOVE", "1")
        .define("HEAP_LIMIT", "20000000")
        .define("LINK_SIZE", "2")
        .define("MATCH_LIMIT", "10000000")
        .define("MATCH_LIMIT_DEPTH", "10000000")
        .define("MAX_NAME_COUNT", "10000")
        .define("MAX_NAME_SIZE", "32")
        .define("NEWLINE_DEFAULT", "2")
        .define("PARENS_NEST_LIMIT", "250")
        .define("PCRE2_STATIC", "1")
        .define("STDC_HEADERS", "1")
        .define("SUPPORT_PCRE2_8", "1")
        .define("SUPPORT_UNICODE", "1")
        .define("PCRE2GREP_BUFSIZE", "20480")
        .define("PCRE2GREP_MAX_BUFSIZE", "1048576");

    if target.contains("windows") {
        builder.define("HAVE_WINDOWS_H", "1");
    }

    let include = out.join("include");
    fs::create_dir_all(&include).unwrap();
    fs::copy("pcre2/src/config.h.generic", include.join("config.h")).unwrap();
    fs::copy("pcre2/src/pcre2.h.generic", include.join("pcre2.h")).unwrap();

    let src = out.join("src");
    fs::create_dir_all(&src).unwrap();
    fs::copy(
        "pcre2/src/pcre2_chartables.c.dist",
        src.join("pcre2_chartables.c"),
    ).unwrap();

    builder
        .include("pcre2/src")
        .include(&include)
        .file(src.join("pcre2_chartables.c"));
    for file in FILES {
        builder.file(Path::new("pcre2/src").join(file));
    }

    if env::var("PCRE2_SYS_DEBUG").unwrap_or(String::new()) == "1" {
        builder.debug(true);
    }
    builder.compile("pcre2");
}

It does generate the libpcre2.a in my target dir, but with following error message:

= note: Undefined symbols for architecture arm64:
            "__pcre2_ckd_smul", referenced from:
                _compile_branch in libpcre2_sys-d9ee7d271cdd3ff9.rlib(pcre2_compile.o)
          ld: symbol(s) not found for architecture arm64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: could not compile `xipin-resolution` due to previous error

Caused by:
  process didn't exit successfully: `CARGO=/Users/bruce/.rustup/toolchains/stable-aarch64-apple-darwin/bin/cargo CARGO_BIN_NAME=xipin-resolution CARGO_CRATE_NAME=xipin_resolution CARGO_MANIFEST_DIR=/Users/bruce/rust/xipin-resolution CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=xipin-resolution CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 DYLD_FALLBACK_LIBRARY_PATH='/Users/bruce/rust/xipin-resolution/target/debug/deps:/Users/bruce/.rustup/toolchains/stable-aarch64-apple-darwin/lib:/Users/bruce/.rustup/toolchains/stable-aarch64-apple-darwin/lib:/Users/bruce/lib:/usr/local/lib:/usr/lib' rustc --crate-name xipin_resolution --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=103 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C split-debuginfo=unpacked -C debuginfo=2 -C metadata=db95aced289ff1f5 -C extra-filename=-db95aced289ff1f5 --out-dir /Users/bruce/rust/xipin-resolution/target/debug/deps -C incremental=/Users/bruce/rust/xipin-resolution/target/debug/incremental -L dependency=/Users/bruce/rust/xipin-resolution/target/debug/deps --extern libc=/Users/bruce/rust/xipin-resolution/target/debug/deps/liblibc-8088543c15d6a2dc.rlib --extern pcre2_sys=/Users/bruce/rust/xipin-resolution/target/debug/deps/libpcre2_sys-d9ee7d271cdd3ff9.rlib -L pcre2-sys/target/ -L native=./target` (exit status: 1)

Did missing something?

PhilipHazel commented 1 year ago

You didn't miss something; I did. I recently noticed that the file pcre2_chkdint.c was missing from the source files listed in NON-AUTOTOOLS-BUILD. I think that is the cause of your problem. (It is a fairly recent addition to the source.)

BruceChar commented 1 year ago

Yup! It does work with pcre2_chkint.c. But strange that I once built successfully with the same version a few days ago. But it fails these days and never be success any more.

PhilipHazel commented 1 year ago

You must be building from the Git repository source, because that file was only introduced recently, and is not in the 10.42 release. So perhaps you previously copied the source before that file was created.