본문 바로가기
언어/Rust

Rust 패키지 생성 및 컴파일

by khd0801 2023. 11. 30.
반응형

 1.  Rust 패키지 생성 방법 및 구조 설명

앞서 설명한 cargo를 사용하면 러스트 패키지를 손쉽게 만들고 사용 할 수 있다. cargo가 만들어 주는 패키지에는 적절한 값으로 미리 설정해 둔 표준 메타데이터가 몇가지 제공되어 매우 편리하게 사용 할 수 있다.

만약 cargo가 실행되지 않는다면 아래 링크를 통해 cargo를 설치하길 바란다.

vim Plugin 사용방법

cargo new 명령어로 패키지 생성이 가능하며 예시는 아래와 같다.

khd0801@ubuntu20:~/study/rust$ cargo new hello
     Created binary (application) `hello` package
khd0801@ubuntu20:~/study/rust$ ls
hello
khd0801@ubuntu20:~/study/rust$ ls -al hello/
total 24
drwxrwxr-x 4 khd0801 khd0801 4096  7월 24 01:23 .
drwxrwxr-x 5 khd0801 khd0801 4096  7월 24 01:23 ..
-rw-rw-r-- 1 khd0801 khd0801  174  7월 24 01:23 Cargo.toml
drwxrwxr-x 6 khd0801 khd0801 4096  7월 24 01:23 .git
-rw-rw-r-- 1 khd0801 khd0801    8  7월 24 01:23 .gitignore
drwxrwxr-x 2 khd0801 khd0801 4096  7월 24 01:23 src

cargo가 생성한 파일 중에 Cargo.toml은 패키지의 메타데이터(rust 프로젝트의 기본 정보와 종속 패키지들의 목록 )를 담아 두기 위한 용도로 사용된다. 

khd0801@ubuntu20:~/study/rust$ cat hello/Cargo.toml 
[package]
name = "hello"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]

해당 프로그램이 다른 라이브러리에 대한 의존성을 갖게 될 때 관련된 내용을 Cargo.toml에 기록해 두면, cargo가 사용자 대신 해당 라이브러리를 내려받고, 빌드하고, 업데이트를 해 준다. Cargo.toml 파일에 대해서는 다음에 자세히 포스팅 하도록 하겠다.

 

cargo는 패키지를 생성 할 때, git 버전 관리 시스템을 사용 할 수 있도록 .git 메타데이터 폴더와 .gitignore 파일을 같이 생성하여 준다. 만약 버전 관리 시스템을 사용하지 않아 생성하고 싶지 않을 때에는 cargo new에 --vcs none을 주면 버전 관리 시스템을 생성하지 않는다.

 

src 폴더는 실제 러스트 코드를 담아두는 폴더이다. 이 폴더에는 자동적으로 생성된 프로그램이 하나 들어 있는데 해당 파일의 내용은 아래와 같다.

khd0801@ubuntu20:~/study/rust$ cd hello/src/
khd0801@ubuntu20:~/study/rust/hello/src$ ls
main.rs
khd0801@ubuntu20:~/study/rust/hello/src$ cat main.rs 
fn main() {
    println!("Hello, world!");
}

이 패키지들이 러스트 프로그램을 위해 마련되는 기본 틀이다.

 

 2.  빌드 및 바이너리 실행.

hello 프로그램을 빌드해 실행하려면 cargo run 명령을 실행하면 된다. 패키지 안에 있는 폴더이기만 하면 어느 위치에서 실행하든 상관이 없다.

khd0801@ubuntu20:~/study/rust/hello/src$ cargo run
   Compiling hello v0.1.0 (/home/khd0801/study/rust/hello)
    Finished dev [unoptimized + debuginfo] target(s) in 4.10s
     Running `target/debug/hello`
Hello, world!
khd0801@ubuntu20:~/study/rust/hello/src$ ls -al ../target/debug/
total 4348
drwxrwxr-x 7 khd0801 khd0801    4096  7월 24 01:28 .
drwxrwxr-x 3 khd0801 khd0801    4096  7월 24 01:28 ..
drwxrwxr-x 2 khd0801 khd0801    4096  7월 24 01:28 build
-rw-rw-r-- 1 khd0801 khd0801       0  7월 24 01:28 .cargo-lock
drwxrwxr-x 2 khd0801 khd0801    4096  7월 24 01:28 deps
drwxrwxr-x 2 khd0801 khd0801    4096  7월 24 01:28 examples
drwxrwxr-x 3 khd0801 khd0801    4096  7월 24 01:28 .fingerprint
-rwxrwxr-x 2 khd0801 khd0801 4419280  7월 24 01:28 hello
-rw-rw-r-- 1 khd0801 khd0801      94  7월 24 01:28 hello.d
drwxrwxr-x 3 khd0801 khd0801    4096  7월 24 01:28 incremental
khd0801@ubuntu20:~/study/rust/hello/src$ ../target/debug/hello 
Hello, world!

cargo run 명령어를 실행하면 cargo가 rust 컴파일러인 rustc로 패키지를 컴파일한 다음, 만들어지는 실행 파일을 실행시켜 준다. 실행 파일은 패키지의 최상위에 있는 target폴더의 debug 폴더안에 만들어 진다.

 

작업을 마친 뒤에는 생성된 파일들을 정라히는 일도 cargo를 통해서 할 수 있다.

khd0801@ubuntu20:~/study/rust/hello/src$ cargo clean
     Removed 21 files, 8.5MiB total
khd0801@ubuntu20:~/study/rust/hello/src$ ls -al ../target/debug/
ls: cannot access '../target/debug/': No such file or directory
khd0801@ubuntu20:~/study/rust/hello/src$ ls ../
Cargo.lock  Cargo.toml  src

cargo clean을 실행하면 target 폴더자체를 삭제하게 된다.

반응형

'언어 > Rust' 카테고리의 다른 글

Rust 언어 및 설치  (0) 2023.11.29

댓글