본문 바로가기
Rust

Rust 프로젝트, 컴파일, 빌드, 릴리즈

by 올리고당 2021. 6. 12.

목차


cargo

cargo( 카고 )는 Rust의 빌드 시스템이자 패키지 매니저입니다. cargo를 통해 프로젝트를 만들고, 컴파일, 빌드, 실행, 라이브러리 다운로드 등 여러 가지 일을 편리하게 할 수 있는 강력한 툴입니다. 오늘은 cargo의 간단한 사용법을 알아보겠습니다.

 


프로젝트 만들기

프로젝트를 만들 디렉토리로 간 후, 아래의 명령어를 입력해주세요.

cargo new [프로젝트 이름]

 

cargo new 실행화면

 

cargo new에 의해 생성되는 파일들

 


빌드 그리고 실행하기

프로젝트의 디렉토리로 들어간 후, 다음 명령어를 입력해주세요.

cargo build 또는 cargo run

 

cargo build 실행화면

 

cargo run 실행화면

 

cargo build(run)에 의해 추가된 파일

 

실행파일 위치

 


Cargo.toml 그리고 dependency

Cargo.toml ( Tom's Obvious Minimal Language ) 파일을 열어보면, 아래와 같이 package ( 패키지 ) 정보와 dependency ( 디펜던시 ) 정보를 확인할 수 있습니다.

 

[package]
name = "blog_cargo"
version = "0.1.0"
authors = ["plan5886 <56795942+plan5886@users.noreply.github.com>"]
edition = "2018"

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

[dependencies]

 

 다른언어에서 library( 라이브러리 )라고 부르는 것을 Rust에선 crate ( 크레이트 )라고 부릅니다. 프로젝트에 crate ( library )를 추가하려면 어떻게 해야 될까요?

 

  1.  https://crates.io/ 에 접속
  2. 필요한 crate를 검색
  3. dependecies 아래 추가 ( crate 이름 = "버전" )
  4. 빌드 또는 실행

 

위의 과정을 거치면, cargo가 알아서 다운로드하여 줍니다. 매우 편리하죠?

rand crate를 추가하는 과정을 예로 보겠습니다. [dependencies] 아래에 필요한 crate의 이름과 버전을 다음과 같이 명시해주면 됩니다.

 

# 위 생략

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

[dependencies]
rand = "0.8.3"

 

crate 추가 후, 실행화면.

use 식별자를 통해 코드에 추가하여 사용할 수 있습니다.

use rand::Rng;

fn main() {
    println!("Hello, world!");
}

 


컴파일하기

간단히 컴파일만 해보려면, 아래의 명령어를 입력해주세요.

cargo check

 

cargo check 실행화면

 

cargo check 실행화면: warning & error

 

아래의 명령어를 입력하면, 에러에 대한 더 자세한 정보를 얻을 수 있습니다.

rustc --explain [에러 번호]

 

컴파일 에러가 있을 때, 에러번호를 알려줌

 


릴리즈를 위한 빌드

릴리즈를 위해 빌드를 하려면, 다음과 같은 옵션을 추가해 빌드 명령어를 입력해주세요.

cargo build --release

 

cargo build --release 실행화면
release 실행파일 위치

 


공식 도서

https://doc.rust-lang.org/book/

 

공식 도서 번역본

https://rinthel.github.io/rust-lang-book-ko/

 

The Cargo Book

https://doc.rust-lang.org/cargo/index.html

 

Cargo.toml vs Cargo.lock

https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

 

Cargo.toml vs Cargo.lock - The Cargo Book

Cargo.toml and Cargo.lock serve two different purposes. Before we talk about them, here’s a summary: Cargo.toml is about describing your dependencies in a broad sense, and is written by you. Cargo.lock contains exact information about your dependencies.

doc.rust-lang.org

 


읽어주셔서 감사합니다.

 

* 피드백은 댓글로 남겨주세요.*

'Rust' 카테고리의 다른 글

[example] Rust 표준입력, 한줄로 받은 입력 나누기  (1) 2021.06.20
Rust 메모리관리와 Ownership  (0) 2021.06.16
Rust 함수, 제어문  (0) 2021.06.15
Rust 변수,상수, 데이터 타입  (1) 2021.06.14
Rust 개발환경 구축하기  (0) 2021.06.11