Code Monkey home page Code Monkey logo

Comments (2)

Minoru avatar Minoru commented on June 27, 2024

Build scripts (like build.rs you linked to) can print out text commands that Cargo understands. In particular, there are:

  • rustc-link-lib=static=libdouble.a, which tells Cargo to look for libdouble.a; and
  • rustc-link-search=native=SOME_DIR, which tells Cargo to look for the library in "SOME_DIR".

Both of these commands are printed when you call compile(). You can learn more about that in cc's documentation here, and also in The Book.

You can run cargo build in verbose mode to see what the build script prints out on your machine:

# Cleaning everything to force the rebuild
$ cargo clean
$ cargo build -vv

In my case, the output was:

It's rather long, click to expand
   Compiling cc v1.0.25
   Compiling libc v0.2.45                                                                                                                                                                                          
     Running `rustc --crate-name cc /home/minoru/.cargo/registry/src/github.com-1ecc6299db9ec823/cc-1.0.25/src/lib.rs --color always --crate-type lib --emit=dep-info,link -C debuginfo=2 -C metadata=8dc6d0eb3b3011ee -C extra-filename=-8dc6d0eb3b3011ee --out-dir /dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps -L dependency=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps --cap-lints warn`
     Running `rustc --crate-name build_script_build /home/minoru/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.45/build.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="use_std"' -C metadata=59c42b4afe560b27 -C extra-filename=-59c42b4afe560b27 --out-dir /dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/libc-59c42b4afe560b27 -L dependency=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps --cap-lints warn`
     Running `/dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/libc-59c42b4afe560b27/build-script-build`                                                                                                    
cargo:rustc-cfg=core_cvoid                                                                                                                                                                                         
     Running `rustc --crate-name libc /home/minoru/.cargo/registry/src/github.com-1ecc6299db9ec823/libc-0.2.45/src/lib.rs --color always --crate-type lib --emit=dep-info,link -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="use_std"' -C metadata=0446e5f9b8b82390 -C extra-filename=-0446e5f9b8b82390 --out-dir /dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps -L dependency=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps --cap-lints warn --cfg core_cvoid`
   Compiling rust-to-c v0.1.0 (/dev/shm/rust-ffi-examples/rust-to-c)                                                                                                                                               
     Running `rustc --crate-name build_script_build build.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=307344becb5b0d7b -C extra-filename=-307344becb5b0d7b --out-dir /dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/rust-to-c-307344becb5b0d7b -C incremental=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/incremental -L dependency=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps --extern cc=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps/libcc-8dc6d0eb3b3011ee.rlib`
     Running `/dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/rust-to-c-307344becb5b0d7b/build-script-build`                                                                                               
TARGET = Some("x86_64-unknown-linux-gnu")                                                                                                                                                                          
OPT_LEVEL = Some("0")                                                                                                                                                                                              
HOST = Some("x86_64-unknown-linux-gnu")                                                                                                                                                                            
CC_x86_64-unknown-linux-gnu = None                                                                                                                                                                                 
CC_x86_64_unknown_linux_gnu = None                                                                                                                                                                                 
HOST_CC = None                                                                                                                                                                                                     
CC = None                                                                                                                                                                                                          
CFLAGS_x86_64-unknown-linux-gnu = None                                                                                                                                                                             
CFLAGS_x86_64_unknown_linux_gnu = None                                                                                                                                                                             
HOST_CFLAGS = None                                                                                                                                                                                                 
CFLAGS = None                                                                                                                                                                                                      
DEBUG = Some("true")                                                                                                                                                                                               
running: "cc" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "-m64" "-Wall" "-Wextra" "-o" "/dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/rust-to-c-c494e004f80bed4b/out/src/double.o" "-c" "src/double.c"
exit code: 0                                                                                                                                                                                                       
AR_x86_64-unknown-linux-gnu = None                                                                                                                                                                                 
AR_x86_64_unknown_linux_gnu = None                                                                                                                                                                                 
HOST_AR = None                                                                                                                                                                                                     
AR = None                                                                                                                                                                                                          
running: "ar" "crs" "/dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/rust-to-c-c494e004f80bed4b/out/libdouble.a" "/dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/rust-to-c-c494e004f80bed4b/out/src/double.o"
exit code: 0                                                                                                                                                                                                       
cargo:rustc-link-lib=static=double                                                                                                                                                                                 
cargo:rustc-link-search=native=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/rust-to-c-c494e004f80bed4b/out                                                                                              
     Running `rustc --crate-name rust_to_c src/main.rs --color always --crate-type bin --emit=dep-info,link -C debuginfo=2 -C metadata=26d4d0c4a319dc63 -C extra-filename=-26d4d0c4a319dc63 --out-dir /dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps -C incremental=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/incremental -L dependency=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps --extern libc=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/deps/liblibc-0446e5f9b8b82390.rlib -L native=/dev/shm/rust-ffi-examples/rust-to-c/target/debug/build/rust-to-c-c494e004f80bed4b/out -l static=double`
    Finished dev [unoptimized + debuginfo] target(s) in 1.96s
    

Look at the last few lines—there are the commands! I believe paths like "rust-to-c-c494e004f80bed4b" can change from machine to machine, so don't depend on them in any way.

from rust-ffi-examples.

albfan avatar albfan commented on June 27, 2024

https://docs.rs/cc/1.0.25/cc/struct.Build.html#method.cargo_metadata and rustc-link-lib=static=libdouble.a were the keys. Details in https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script too.

Thanks for this master class

from rust-ffi-examples.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.