Comments (9)
@syejing We actually, for some target platforms, invoke a C compiler as our linker driver.
Only for Windows and bare metal targets do we typically use the linker directly.
from rust.
OK, my operating system is mac os, in fact I tracked and found that I did use cc, my original intention was to see how to use rustc to convert .rs files to .ll llvm ir, use llc to convert .ll to .s assembly, and then use as to convert .s to .o machine code, and now stuck in how to use linker to turn .o into executable files.
`use std::io;
fn main() {
let mut a = String::new(); // 声明两个字符串变量
let mut b = String::new();
let s: i32; // 声明一个整型变量
io::stdin().read_line(&mut a).expect("Failed to read line"); // 输入数据,放入到变量a中
io::stdin().read_line(&mut b).expect("Failed to read line"); // 输入数据,放入到变量b中
let a: i32 = a.trim().parse().expect("Please type a number!"); // 将字符串a转换为整数
let b: i32 = b.trim().parse().expect("Please type a number!"); // 将字符串b转换为整数
s = a + b; // 求和操作
println!("The sum is {}.", s); // 输出到屏幕中
}
`
from rust.
env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL="C" PATH="/Users/admin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/usr/local/opt/llvm/bin:/Users/admin/.nvm/versions/node/v16.17.0/bin:/usr/local/anaconda3/bin:/Users/admin/Library/Java/JavaVirtualMachines/corretto-1.8.0_332/Contents/Home/bin:/usr/local/Cellar/node@12/12.18.3/bin:/Applications/IntelliJ IDEA CE.app/Contents/plugins/maven/lib/maven3/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local/share/dotnet:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Users/admin/.cargo/bin:.:/Users/admin/go_dev/go/bin" VSLANG="1033" ZERO_AR_DATE="1" cc main.o -arch x86_64 -m64 -L /Users/admin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib /Users/ ... -lSystem -lc -lm -L /Users/admin/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib -o main -Wl,-dead_strip -nodefaultlibs
and I've tried cc, but it got an error
Undefined symbols for architecture x86_64: "___rust_alloc", referenced from: core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::ha4c1a9c0063f99d9 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) core::ops::function::FnOnce::call_once$u7b$$u7b$vtable.shim$u7d$$u7d$::hb15c9b25770b9417 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) core::result::Result$LT$T$C$E$GT$::map::h907e242bb5dd5719 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) alloc::raw_vec::finish_grow::hafe90ce51047470b in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) gimli::read::line::FileEntryFormat::parse::h0b980bcfd85b723e in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) _$LT$gimli..read..line..LineProgramHeader$LT$R$C$Offset$GT$$u20$as$u20$core..clone..Clone$GT$::clone::h9f8a101362c4c6e6 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) ... addr2line::ResUnit$LT$R$GT$::find_function_or_location::_$u7b$$u7b$closure$u7d$$u7d$::he77e85d1c7940c3d in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) addr2line::Lines::parse::h634e3216ac780800 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) addr2line::Lines::parse::h634e3216ac780800 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) addr2line::Lines::parse::h634e3216ac780800 in libstd-2ad7f97be9e16ca5.rlib[3](std-2ad7f97be9e16ca5.std.73125d0ae810e61b-cgu.0.rcgu.o) ... ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
from rust.
OK, my operating system is mac os, in fact I tracked and found that I did use cc, my original intention was to see how to use rustc to convert .rs files to .ll llvm ir, use llc to convert .ll to .s assembly, and then use as to convert .s to .o machine code, and now stuck in how to use linker to turn .o into executable files.
Eh?
Last I checked, we don't actually do those things. We convert the code to the data structure forms directly used by LLVM, in memory, mostly by invoking LLVM. Then we invoke LLVM on the data structures to finish compilation. We don't write .ll to disk if we can avoid doing so, and we definitely don't call llc
on it, and LLVM doesn't invoke as
to assemble the files either. If you ask for us to --emit llvm-ir
or --emit asm
we do that on a best-effort basis, and may have subtle or even major differences from what we actually do. It's informational/for debugging, in other words.
from rust.
Hmm, I can understand the true meaning, but I want to ask how to call the linker and what is command?
from rust.
I believe this is what you want:
RUSTC_LOG=INFO rustc hello.rs 2>&1 | grep "link"
from rust.
这输出的结果和--print link-args一样
rustc src/main.rs -v --print link-args
from rust.
env -u IPHONEOS_DEPLOYMENT_TARGET -u TVOS_DEPLOYMENT_TARGET LC_ALL="C" PATH="..." VSLANG="1033" ZERO_AR_DATE="1" cc -arch x86_64 -m64 ".../symbols.o" ".../main.main.44961560ebe940ad-cgu.0.rcgu.o" "-L" "..." ".../libstd-2ad7f97be9e16ca5.rlib" "..." -lSystem -lc -lm "-L" "..." "-o" "main" "-Wl,-dead_strip" "-nodefaultlibs"
According to the first principle, I can use cc as the linker to link, but it seems to report an error, which really tortures me.
from rust.
Adding -C save-temps solved the problem. Symbols.o, main.main.o, main.0 are saved, and cc is enough. Thank you.
from rust.
Related Issues (20)
- `remove_dir_all` fails removing directores (with subdirectories) on network drives (SFTP) under windows HOT 28
- Two subsequent labels and unnecessary `jmp` with `opt-level=s` HOT 6
- Can the rust/build/x86_64-unknown-linux-gnu/stage0 directory be generated without network? HOT 2
- Strange TcpStream TTL type implementation (set_ttl/ttl) HOT 8
- Lint against using `libc::c_*` types in std if they're also avaliable in `core::ffi`
- `std::process::exit` is not thread-safe HOT 132
- compiletest: `./x test <test-name>.stderr` (or existing non-`.rs` file) should warn or error HOT 1
- rustc hangs / infinite loop on small example with generic_const_exprs and specialization HOT 1
- integer_decode() for float: example usage in docs doesn't make sense HOT 2
- Confusing "Mismatched types" error due to unsatisfied trait bound HOT 4
- Tracking Issue for `x86_amx_intrinsics` HOT 1
- Tracking Issue for `sha512_sm_x86`
- `#[coverage(..)]` attribute should apply recursively to nested functions/methods/closures HOT 1
- Coverage spans should not overlap nested items HOT 1
- rust lang analyzer is not working in vs code.
- `debuginfo` test suite shows zero tests being executed if pre-requisites are missing
- rust_eh_personality already defined in dynamic library HOT 5
- Tracking Issue for `#![feature(externally_constructed_attr)]`
- rustc overflow with size_of::<Self> constraint HOT 2
- Cargo doc fails for certain crates in a workspace with "no such file or directory (os error 2)" HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust.