0
点赞
收藏
分享

微信扫一扫

Rust语言之HelloWorld


Rust语言之HelloWorld

参考文档:

​​http://doc.crates.io/guide.html​​

1 什么是Cargo

相当于maven/ant之于java, automake之于c, Cargo是rust的项目管理工具。用 Cargo 做4件事情:

    1) 配置管理

    2) 下载项目依赖

    3) 调用编译器rustc编译,发布程序

    4) 总之开发rust的一揽子解决方案

当你安装了rust, cargo就随之安装了。

2 创建第一个rust程序HelloWorld

$ cargo new hello_world --bin



查看一下目录结构:

$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
└── main.rs

1 directory, 2 files



编译一下,并运行之:

$ cargo build

$ ./target/debug/hello_world
Hello, world!


或者:

$ cargo run

Running `target/debug/hello_world`


Hello, world!



编译release版本:

$ cargo build --release

产生的程序:

target/release/hello_world

3 发布到服务器

我在开发机器上(Ubuntu14.04)上编译的,现在发布到RHEL6服务器,然后运行:

[root@vm-repo ~]# ./hello_world


./hello_world: /lib64/libc.so.6: version `GLIBC_2.14' not found (required by ./hello_world)


./hello_world: /lib64/libc.so.6: version `GLIBC_2.18' not found (required by ./hello_world)



很显然,我的RHEL6的GLIBC太旧了。查看一下:

[root@vm-repo ~]# strings /lib64/libc.so.6 |grep GLIBC_


.

..

GLIBC_2.4

GLIBC_2.5

GLIBC_2.6

GLIBC_2.7

GLIBC_2.8

GLIBC_2.9

GLIBC_2.10

GLIBC_2.11

GLIBC_2.12

GLIBC_PRIVATE


4 升级服务器GLIBC(失败)

1) 下载glibc (​​http://ftp.gnu.org/gnu/libc/​​)

wget -c http://ftp.gnu.org/gnu/libc/glibc-2.22.tar.gz

2) 解压后安装

# tar zxf glibc-2.22.tar.gz
# cd glibc-2.22
# mkdir build
# cd build
# ../configure
# make -j4
# make install

...

checking for gawk... gawk

checking version of gawk... 3.1.7, ok
checking if gcc is sufficient to build libc... no
checking for nm... nm
configure: error:
*** These critical programs are missing or too old: as ld compiler
*** Check the INSTALL file for required versions.

看来RHEL6的gcc版本太低。需要升级gcc。太费劲,放弃。

5 直接在RHEL6上装rust (成功)

# curl -f -L https://static.rust-lang.org/rustup.sh -O
# sh rustup.sh

[root@vm-repo experiment]# rustc --version
rustc 1.2.0 (082e47636 2015-08-03)

然后重新创建hello_world:

# cargo new hello_world --bin
# cd hello_world
# cargo build --release

生成的 target/release/hello_world 可以运行!


================================================

更多内容请详细阅读​

​​http://doc.crates.io/guide.html​​


举报

相关推荐

0 条评论