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)
- Clean out old msys2/mingw stuff in CI HOT 12
- We should document what external dependencies we assume for our test suites
- Tracking issue for `-Zterminal-urls` HOT 2
- We should document more clearly env vars used between bootstrap <-> compiletest <-> test suites HOT 1
- Decide which methods to add to new `Range` types HOT 2
- error: --remap-path-prefix must contain '=' between FROM and TO because path contains spaces HOT 7
- Incorrectly suggests replacing end characters of function call with double quotes HOT 1
- The implementation `From<&[T; N]>` for `Option<&[T]>` seems missing HOT 7
- When a trait is missing items, there are many follow-up errors distracting from the root cause HOT 1
- When passing too many lifetimes to a struct, there are tons of unrelated errors about needing explicit lifetime annotations HOT 2
- Tracking Issue for `optimize_for_size` standard library feature HOT 5
- LazyCell, LazyLock are missing some big-picture documentation
- Audit usage of `command_output` methods in rmake.rs tests
- core::slice::sort::partial_insertion_sort: Redundant call to insertion_sort_shift_right HOT 2
- Excess object in `staticlib` archive eventually causing link error HOT 2
- Tracking Issue for `lazy_cell_consume` HOT 1
- Missed enum layout optimization depending on enum variant field reordering HOT 2
- binary operators hide "trait bounds not satisfied" errors
- Memcpy generated for Vec::push(Default::default()) instead of initialization in-place
- ICE: `expected region for ..` 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.