immunant / c2rust

Migrate C code to Rust
https://c2rust.com/
Other
4.01k stars 243 forks source link

tmux example from the manual fails to link after building #341

Open maemre opened 3 years ago

maemre commented 3 years ago

After going through the steps in the manual:

cd examples/tmux/repo
git submodule update --init .
./autogen.sh && ./configure
intercept-build make check
## make sure that there are no `-On` flags in compile command json file
cd ..
./translate.py
cd repo/rust

Then, building the Rust version of tmux with cargo build, I get the following linking error where linking with cc fails. It seems like some of the symbols defined in the Rust crate are not exported/public.

$ cargo build --verbose
       Fresh cc v1.0.12
   Compiling tmux-rs v0.1.0 (/home/emre/lab/c2rust/examples/tmux/repo/rust)
     Running `/home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/build/tmux-rs-50a78917a0aa67a1/build-script-build`
     Running `rustc --crate-name tmux_rs src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=db6a4a05024c60fa -C extra-filename=-db6a4a05024c60fa --out-dir /home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/deps -C incremental=/home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/incremental -L dependency=/home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/deps -L native=/home/emre/lab/c2rust/examples/tmux/repo/rust/lib -L native=/home/emre/lab/c2rust/examples/tmux/repo/rust/lib -l static=variadic -l dylib=resolv -l ncursesw -l event`
warning: unused import: `self::event_struct_h::event_base`
    --> src/main.rs:1336:5
     |
1336 | use self::event_struct_h::event_base;
     |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     |
     = note: `#[warn(unused_imports)]` on by default

error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "...other linker arguments I removed"
  = note: /home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/deps/tmux_rs-db6a4a05024c60fa.2vdqh606d9yve3vl.rcgu.o: In function `tmux_rs::usage':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1599: undefined reference to `getprogname'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/deps/tmux_rs-db6a4a05024c60fa.2vdqh606d9yve3vl.rcgu.o: In function `areshell':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1636: undefined reference to `getprogname'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/deps/tmux_rs-db6a4a05024c60fa.2vdqh606d9yve3vl.rcgu.o: In function `tmux_rs::make_label':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1685: undefined reference to `strlcpy'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/target/debug/deps/tmux_rs-db6a4a05024c60fa.2vdqh606d9yve3vl.rcgu.o: In function `tmux_rs::main_0':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1796: undefined reference to `BSDgetopt'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:(.text._ZN7tmux_rs6main_017h3f926d28fe41256aE+0x339): undefined reference to `BSDoptind'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:(.text._ZN7tmux_rs6main_017h3f926d28fe41256aE+0x384): undefined reference to `BSDoptarg'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1810: undefined reference to `getprogname'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:(.text._ZN7tmux_rs6main_017h3f926d28fe41256aE+0x411): undefined reference to `BSDoptarg'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1814: undefined reference to `set_cfg_file'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:(.text._ZN7tmux_rs6main_017h3f926d28fe41256aE+0x44a): undefined reference to `BSDoptarg'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1818: undefined reference to `xstrdup'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:(.text._ZN7tmux_rs6main_017h3f926d28fe41256aE+0x485): undefined reference to `BSDoptarg'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1823: undefined reference to `xstrdup'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1826: undefined reference to `log_add_level'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:(.text._ZN7tmux_rs6main_017h3f926d28fe41256aE+0x4d5): undefined reference to `BSDoptind'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1833: undefined reference to `getptmfd'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1871: undefined reference to `hooks_create'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1872: undefined reference to `environ_create'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1875: undefined reference to `environ_put'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1891: undefined reference to `options_create'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1892: undefined reference to `options_create'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1893: undefined reference to `options_create'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:(.text._ZN7tmux_rs6main_017h3f926d28fe41256aE+0xa65): undefined reference to `options_table'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1898: undefined reference to `options_default'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1902: undefined reference to `options_default'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1906: undefined reference to `options_default'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1933: undefined reference to `options_set_number'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1936: undefined reference to `options_set_number'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1949: undefined reference to `xstrdup'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1967: undefined reference to `osdep_event_init'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/src/main.rs:1967: undefined reference to `client_main'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/lib/libvariadic.a(variadic.o): In function `environ_set':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:252: undefined reference to `environ_find'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:254: undefined reference to `xvasprintf'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:256: undefined reference to `xmalloc'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:257: undefined reference to `xstrdup'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:258: undefined reference to `xvasprintf'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:259: undefined reference to `environ_RB_INSERT'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/lib/libvariadic.a(variadic.o): In function `fatalx':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:397: undefined reference to `log_vwrite'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/lib/libvariadic.a(variadic.o): In function `options_set_string':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:415: undefined reference to `xvasprintf'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:418: undefined reference to `options_get_only'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:425: undefined reference to `options_add'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:427: undefined reference to `options_parent_table_entry'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:427: undefined reference to `options_default'
          /home/emre/lab/c2rust/examples/tmux/repo/rust/lib/libvariadic.a(variadic.o): In function `xasprintf':
          /home/emre/lab/c2rust/examples/tmux/repo/rust/../variadic.c:733: undefined reference to `xvasprintf'
          collect2: error: ld returned 1 exit status

@rinon mentioned on Discord that this may be related to module merging.

maemre commented 3 years ago

From some manual inspection, it seems like the crate (main.rs) does not declare the translated modules. I added pub mod <module name> for compat and all Rust files in src/ except variadic, and now it links and runs.