rust crate x86, x64 build
rust crate의 경우 바이너리
가 아닌 소스
형태로 제공이 되므로 사용하는 바이너리 크레이트가 설정한 타겟에 따라 빌드가 된다.
rustup show
installed toolchains
--------------------
stable-x86_64-pc-windows-msvc (default)
1.79.0-x86_64-pc-windows-msvc
1.76-x86_64-pc-windows-msvc
installed targets for active toolchain
--------------------------------------
i686-pc-windows-msvc
x86_64-pc-windows-gnu
x86_64-pc-windows-msvc
active toolchain
----------------
stable-x86_64-pc-windows-msvc (default)
rustc 1.83.0 (90b35a623 2024-11-26)
rustup show
명령어를 실행하면 현재 로컬 pc에 설치된 toolchain
에 대한 정보가 나온다. 그리고 활성화되어 있는 target
또한 나온다.
cargo build --target=i686-pc-windows-msvc
를 하게 되면 x86
으로 빌드한다는 의미이다.
만약 보여진 리스트에 i686-pc-windows-msvc
가 없다면
rustup target add i686-pc-windows-msvc
명령어를 통해서 추가하면 된다.
rustc --print cfg
debug_assertions
panic="unwind"
target_abi=""
target_arch="x86_64"
target_endian="little"
target_env="msvc"
target_family="windows"
target_feature="cmpxchg16b"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_feature="sse3"
target_has_atomic="128"
target_has_atomic="16"
target_has_atomic="32"
target_has_atomic="64"
target_has_atomic="8"
target_has_atomic="ptr"
target_os="windows"
target_pointer_width="64"
target_vendor="pc"
windows
rust --print cfg
명령어는 컴파일러가 컴파일할 때 사용하는 설정 정보를 출력한다.
x86
과 x64
target에 대해서 구분지을 때 사용할 수 있다.
if cfg!(target_pointer_width = "32") {
println!("32bit target");
} else {
println!("64bit target");
}
컴파일 타겟에 따라 포인터의 길이가 다르므로 cfg
매크로를 활용하여 필요한 세팅을 설정할 수 있다.
이 방식은 타겟 아키텍처에 맞는 최적화나 특화된 코드를 작성할 때 유용합니다.
rust crate는 컴파일 시점에 타겟의 아키텍처에 맞게 빌드가 된다는 점을 알았다.