rust x86 build

wangki·2025년 2월 27일
1

Rust

목록 보기
25/56

개요

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 명령어는 컴파일러가 컴파일할 때 사용하는 설정 정보를 출력한다.
x86x64 target에 대해서 구분지을 때 사용할 수 있다.

    if cfg!(target_pointer_width = "32") {
        println!("32bit target");
    } else {
        println!("64bit target");
    }

컴파일 타겟에 따라 포인터의 길이가 다르므로 cfg매크로를 활용하여 필요한 세팅을 설정할 수 있다.
이 방식은 타겟 아키텍처에 맞는 최적화나 특화된 코드를 작성할 때 유용합니다.

결론

rust crate는 컴파일 시점에 타겟의 아키텍처에 맞게 빌드가 된다는 점을 알았다.

0개의 댓글