본문 바로가기
IT/LINUX

리눅스 CentOS solidity 컴파일러 solc 설치하기 . 샘플 컴파일

by SidePower 2022. 10. 4.

 

이더리움 공부를 위해 solidity를 설치합니다.

구글링으로만 참고해서 설치하려니 한 번에 안되네요.

너무 쉽게 생각한거 같아요.^^;;

시행착오를 거듭하다가 겨우 컴파일까지 완료했습니다.

solidity 관련 책을 사야 되나 고민 중입니다.ㅋ

 solc가 yum 패키지에 포함되어 있는지 확인
[root@localhost ~]# yum list *solc*
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.navercorp.com
 * extras: mirror.navercorp.com
 * updates: mirror.navercorp.com
Error: No matching Packages to list
▶ No matching 없다고 나오네요.

 yum 을 최신버전으로 update 합니다.
[root@localhost ~]# yum update
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.navercorp.com
 * extras: mirror.navercorp.com
 * updates: mirror.navercorp.com
base                                                                                                                         | 3.6 kB  00:00:00
extras                                                                                                                       | 2.9 kB  00:00:00
updates                                                                                                                      | 2.9 kB  00:00:00
updates/7/x86_64/primary_db                                                                                                  |  13 MB  00:00:01
Resolving Dependencies
--> Running transaction check
---> Package NetworkManager.x86_64 1:1.18.8-1.el7 will be updated
---> Package NetworkManager.x86_64 1:1.18.8-2.el7_9 will be an update
..
생략
..
---> Package zlib.x86_64 0:1.2.7-19.el7_9 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================
 Package                                 Arch                   Version                                         Repository               Size
==============================================================================
Installing:
 kernel                                   x86_64                 3.10.0-1160.49.1.el7                            updates                  50 M
Updating:
 NetworkManager                    x86_64                 1:1.18.8-2.el7_9                                updates                 1.9 M
 NetworkManager-libnm            x86_64                 1:1.18.8-2.el7_9                                updates                 1.7 M
 NetworkManager-team             x86_64                 1:1.18.8-2.el7_9                                updates                 165 k
 NetworkManager-tui                x86_64                 1:1.18.8-2.el7_9                                updates                 329 k
 NetworkManager-wifi               x86_64                 1:1.18.8-2.el7_9                                updates                 202 k
..
생략
..
 tuned                              noarch                 2.11.0-11.el7_9                                 updates                 269 k
 tzdata                                noarch                 2021e-1.el7                                     updates                 501 k
 util-linux                         x86_64                 2.23.2-65.el7_9.1                               updates                 2.0 M
 vim-minimal                        x86_64                 2:7.4.629-8.el7_9                               updates                 443 k
 virt-what                           x86_64                 1.18-4.el7_9.1                                  updates                  30 k
 wpa_supplicant                     x86_64                 1:2.6-12.el7_9.2                                updates                 1.2 M
 zlib                             x86_64                 1.2.7-19.el7_9                                  updates                  90 k

Transaction Summary
=============================================================================
Install    1 Package
Upgrade  104 Packages

Total download size: 237 M
Is this ok [y/d/N]: y
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/105): NetworkManager-libnm-1.18.8-2.el7_9.x86_64.rpm                                         | 1.7 MB  00:00:00
(2/105): NetworkManager-1.18.8-2.el7_9.x86_64.rpm                                                 | 1.9 MB  00:00:00
...
생략
...
(104/105): zlib-1.2.7-19.el7_9.x86_64.rpm                                                                  |  90 kB  00:00:00
(105/105): systemd-219-78.el7_9.3.x86_64.rpm                                                           | 5.1 MB  00:00:01
------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                               9.0 MB/s | 237 MB  00:00:26
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Updating   : bash-4.2.46-35.el7_9.x86_64                                                                           1/209
  Updating   : zlib-1.2.7-19.el7_9.x86_64                                                                             2/209
  Updating   : nspr-4.32.0-1.el7_9.x86_64                                                                              3/209
..
생략
...
  Verifying  : kpartx-0.4.9-133.el7.x86_64                                                                             207/209
  Verifying  : libsmartcols-2.23.2-65.el7.x86_64                                                                     208/209
  Verifying  : libmount-2.23.2-65.el7.x86_64                                                                            209/209

Installed:
  kernel.x86_64 0:3.10.0-1160.49.1.el7

Updated:
  NetworkManager.x86_64 1:1.18.8-2.el7_9                                    NetworkManager-libnm.x86_64 1:1.18.8-2.el7_9
  NetworkManager-team.x86_64 1:1.18.8-2.el7_9                               NetworkManager-tui.x86_64 1:1.18.8-2.el7_9
  NetworkManager-wifi.x86_64 1:1.18.8-2.el7_9                               bash.x86_64 0:4.2.46-35.el7_9
..
생략
..  
  zdata.noarch 0:2021e-1.el7                                               util-linux.x86_64 0:2.23.2-65.el7_9.1
  vim-minimal.x86_64 2:7.4.629-8.el7_9                                      virt-what.x86_64 0:1.18-4.el7_9.1
  wpa_supplicant.x86_64 1:2.6-12.el7_9.2                                    zlib.x86_64 0:1.2.7-19.el7_9

Complete!


 update 후에도 없다고 나오네요. ^^;;
[root@localhost ~]# yum list *solc*
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirror.navercorp.com
 * extras: mirror.navercorp.com
 * updates: mirror.navercorp.com
Error: No matching Packages to list

 

 epel 확장 패키지 설치

Extra Packages for Enterprise Linux
( 엔터프라이즈 리눅스 추가 패키지 )

 epel 설치
[root@localhost ~]# yum install epel-release
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.navercorp.com
 * extras: mirror.navercorp.com
 * updates: mirror.navercorp.com
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-11 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
 Package                             Arch                          Version                          Repository                         Size
=============================================================================
Installing:
 epel-release                       noarch                            7-11                             extras                             15 k

Transaction Summary
=============================================================================
Install  1 Package

Total download size: 15 k
Installed size: 24 k
Is this ok [y/d/N]: y
Downloading packages:
epel-release-7-11.noarch.rpm                                                                                  |  15 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-11.noarch                                                                       1/1
  Verifying  : epel-release-7-11.noarch                                                                       1/1

Installed:
  epel-release.noarch 0:7-11

Complete!


 epel에도 solc이 없었습니다. 흑...
[root@localhost ~]# yum install solc
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.navercorp.com
 * epel: ftp.iij.ad.jp
 * extras: mirror.navercorp.com
 * updates: mirror.navercorp.com
No package solc available.
Error: Nothing to do

 

구글링 해서 찾아보니

CentOS에서 solc 설치는 snap으로 가능하다고 합니다.

snap은 yum과 같은 리눅스 패키지 관리 도구인데요.

snap은 CentOS나 Red Hat 7.6 이상에서 설치 사용 가능한 툴입니다.

 snap 설치하기
[root@localhost ~]# yum install snapd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirror.navercorp.com
 * epel: ftp.iij.ad.jp
 * extras: mirror.navercorp.com
 * updates: mirror.navercorp.com
Resolving Dependencies
--> Running transaction check
---> Package snapd.x86_64 0:2.53.2-2.el7 will be installed
--> Processing Dependency: snap-confine(x86-64) = 2.53.2-2.el7 for package: snapd-2.53.2-2.el7.x86_64
--> Processing Dependency: snapd-selinux = 2.53.2-2.el7 for package: snapd-2.53.2-2.el7.x86_64
..
생략
..
---> Package python-IPy.noarch 0:0.75-6.el7 will be installed
---> Package setools-libs.x86_64 0:3.3.8-4.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================
 Package                           Arch                       Version                                   Repository                Size
=============================================================================
Installing:
 snapd                              x86_64                     2.53.2-2.el7                              epel                      16 M
Installing for dependencies:
 audit-libs-python                x86_64                     2.8.5-4.el7                                base                      76 k
 bash-completion                 noarch                     1:2.1-8.el7                               base                      87 k
 checkpolicy                       x86_64                     2.5-8.el7                                  base                     295 k
..생략...
 squashfuse                        x86_64                     0.1.102-1.el7                             epel                      18 k
 squashfuse-libs                  x86_64                     0.1.102-1.el7                              epel                      24 k

Transaction Summary
==========================================================================
Install  1 Package (+16 Dependent packages)

Total download size: 22 M
Installed size: 63 M
Is this ok [y/d/N]: y
Downloading packages:
(1/17): audit-libs-python-2.8.5-4.el7.x86_64.rpm                                                           |  76 kB  00:00:00
(2/17): bash-completion-2.1-8.el7.noarch.rpm                                                              |  87 kB  00:00:00
(3/17): fuse-libs-2.9.2-11.el7.x86_64.rpm                                                                     |  93 kB  00:00:00
..생략..
(16/17): squashfuse-0.1.102-1.el7.x86_64.rpm                                                                 |  18 kB  00:00:00
(17/17): squashfuse-libs-0.1.102-1.el7.x86_64.rpm                                                           |  24 kB  00:00:00
-----------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                        1.8 MB/s |  22 MB  00:00:12
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Importing GPG key 0x352C64E5:
 Userid     : "Fedora EPEL (7) <epel@fedoraproject.org>"
 Fingerprint: 91e9 7d7c 4a5e 96f1 7f3e 888f 6a2f aea2 352c 64e5
 Package    : epel-release-7-11.noarch (@extras)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : setools-libs-3.3.8-4.el7.x86_64                                                                                  1/17
  Installing : audit-libs-python-2.8.5-4.el7.x86_64                                                                           2/17
  Installing : libcgroup-0.41-21.el7.x86_64                                                                                    3/17
..생략....
  Verifying  : audit-libs-python-2.8.5-4.el7.x86_64                                                                          16/17
  Verifying  : setools-libs-3.3.8-4.el7.x86_64                                                                                 17/17

Installed:
  snapd.x86_64 0:2.53.2-2.el7

Dependency Installed:
  audit-libs-python.x86_64 0:2.8.5-4.el7      bash-completion.noarch 1:2.1-8.el7                    checkpolicy.x86_64 0:2.5-8.el7
  fuse.x86_64 0:2.9.2-11.el7                  fuse-libs.x86_64 0:2.9.2-11.el7                       libcgroup.x86_64 0:0.41-21.el7
  libsemanage-python.x86_64 0:2.5-14.el7      libzstd.x86_64 0:1.5.0-1.el7           policycoreutils-python.x86_64 0:2.5-34.el7
  python-IPy.noarch 0:0.75-6.el7              setools-libs.x86_64 0:3.3.8-4.el7           snap-confine.x86_64 0:2.53.2-2.el7
  snapd-selinux.noarch 0:2.53.2-2.el7  squashfs-tools.x86_64 0:4.3-0.21.gitaae0aff4.el7   squashfuse.x86_64 0:0.1.102-1.el7
  squashfuse-libs.x86_64 0:0.1.102-1.el7

Complete!


 클래식 snap 기능 활성화를 위해 /snap 링크 걸어둡니다.
[root@localhost /]# ln -s /var/lib/snapd/snap /snap
[root@localhost /]# ll
total 20
lrwxrwxrwx.   1 root root    7 Nov 18 03:44 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 Dec 10 00:05 boot
drwxr-xr-x.  20 root root 3080 Dec  9 18:59 dev
drwxr-xr-x.  25 root root  780 Dec 10 00:19 run
lrwxrwxrwx.   1 root root    8 Nov 18 03:44 sbin -> usr/sbin
lrwxrwxrwx.   1 root root   19 Dec 10 00:54 snap -> /var/lib/snapd/snap
drwxr-xr-x.   2 root root    6 Apr 11  2018 srv
...생략...


 snap의 통신 소켓 활성화하기
[root@localhost ~]# systemctl enable --now snapd.socket
Created symlink from /etc/systemd/system/sockets.target.wants/snapd.socket to /usr/lib/systemd/system/snapd.socket.


 snap으로 solc 설치하기
[root@localhost ~]# snap install solc
Download snap "core" (11993) from channel "stable"                                                      43% 3.05MB/s 19.6s
Warning: /var/lib/snapd/snap/bin was not found in your $PATH. If you've not restarted your session
         since you installed snapd, try doing that. Please see https://forum.snapcraft.io/t/9469
         for more details.

solc v0.5.16 from Ethereum Build Automation (builds-c) installed

 solc 설치 위치 확인
[tmach@localhost snap]$ which solc
/var/lib/snapd/snap/bin/solc

 solc 버전 확인
[tmach@localhost ~]$ solc --version
solc, the solidity compiler commandline interface
Version: 0.5.16+commit.9c3226ce.Linux.g++

만약에 solc 명령어를 실행했을때 아래와 같은 에러가 나면
리눅스 서버를 재부팅 해주세요.^^;;
[root@localhost bin]# solc --version
solc: error while loading shared libraries: libz3.so: cannot open shared object file: No such file or directory

 solidity 샘플 테스트 

solidity는 자바와 C++과 비슷하면서도 문법이 다소 차이가 있어요.^^;;

solidity를 전혀 모르는 상태에서

간단한 샘플 하나 만드는것도 시간이 좀 걸린거 같아요.ㅋ

 문자열을 주고 받는 아주 간단한 소스입니다.
[tmach@localhost test]$ vi src_sol01.sol
pragma solidity ^0.5.16;   // solidity 버전 선언

contract HelloWorld {       // 스마트 계약 시작
        string public greet;    // stinrg 변수 선언 
        function getHello() public view returns (string memory) {  // 문자열 반환 함수 선언
                return greet;           // 함수 반환값 지정
        }
        function setHello(string memory _greet) public {    // 문자열 입력 함수 선언
                greet = _greet;     //  입력 받은 문자열을 내부 변수에 대입
        }
}

 solc 컴파일
[tmach@localhost test]$ solc src_sol01.sol
Compiler run successful, no output requested.

 solc 컴파일 . --bin
bin옵션은 컴파일 결과를 바이트코드로 보여줍니다.
[tmach@localhost test]$ solc src_sol01.sol --bin

======= src_sol01.sol:HelloWorld =======
Binary:
608060405234801561001057600080fd5b5061043b806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c8063435ffe94146100465780638da9b77214610101578063cfae321714610184575b600080fd5b6100ff6004803603602081101561005c57600080fd5b810190808035906020019064010000000081111561007957600080fd5b82018360208201111561008b57600080fd5b803590602001918460018302840111640100000000831117156100ad57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610207565b005b610109610221565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561014957808201518184015260208101905061012e565b50505050905090810190601f1680156101765780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61018c6102c3565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101cc5780820151818401526020810190506101b1565b50505050905090810190601f1680156101f95780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061021d929190610361565b5050565b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102b95780601f1061028e576101008083540402835291602001916102b9565b820191906000526020600020905b81548152906001019060200180831161029c57829003601f168201915b5050505050905090565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103595780601f1061032e57610100808354040283529160200191610359565b820191906000526020600020905b81548152906001019060200180831161033c57829003601f168201915b505050505081565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106103a257805160ff19168380011785556103d0565b828001600101855582156103d0579182015b828111156103cf5782518255916020019190600101906103b4565b5b5090506103dd91906103e1565b5090565b61040391905b808211156103ff5760008160009055506001016103e7565b5090565b9056fea265627a7a72315820893bd4b9772bd5660dfc670744c9b2468d3956a6ec862d75ab01a57b70ab675b64736f6c63430005100032


 solc 컴파일 . --asm
asm옵션은 어셈블리 코드로 컴파일 결과를 보여줍니다.
[tmach@localhost test]$ solc src_sol01.sol --asm

======= src_sol01.sol:HelloWorld =======
EVM assembly:
    /* "src_sol01.sol":26:220  contract HelloWorld {... */
  mstore(0x40, 0x80)
  callvalue
    /* "--CODEGEN--":8:17   */
  dup1
    /* "--CODEGEN--":5:7   */
  iszero
  tag_1
  jumpi
    /* "--CODEGEN--":30:31   */
  0x00
..생략..
sub_0: assembly {
        /* "src_sol01.sol":26:220  contract HelloWorld {... */
      mstore(0x40, 0x80)
      callvalue
        /* "--CODEGEN--":8:17   */
      dup1
..생력..
      jump      // out

    auxdata: 0xa265627a7a72315820893bd4b9772bd5660dfc670744c9b2468d3956a6ec862d75ab01a57b70ab675b64736f6c63430005100032
}


 solc 컴파일 . --ast
ast옵션은 추상 구문 트리형태로 컴파일 결과를 보여줍니다.
[tmach@localhost test]$ solc src_sol01.sol --ast
Syntax trees:

======= src_sol01.sol =======
PragmaDirective
   Gas costs: 0
   Source: "pragma solidity ^0.5.16;"
ContractDefinition "HelloWorld"
   Source: "contract HelloWorld {\n\tstring public greet;\n\tfunction getHello() public view returns (string memory) {\n\t\treturn greet;\n\t}\n\tfunction setHello(string memory _greet) public {\n\t\tgreet = _greet;\n\t}\n}"
  VariableDeclaration "greet"
     Type: string storage ref
     Gas costs: [???]
     Source: "string public greet"
    ElementaryTypeName string
..생력..
             Type: string storage ref
             Source: "greet"
          Identifier _greet
             Type: string memory
             Source: "_greet"


 solc 컴파일 . -o
o 옵션은 디렉터리를 지정해서 해당 디렉터리안에 결과 파일을 만듭니다.
[tmach@localhost test]$ solc src_sol01.sol --ast -o Fone
Compiler run successful. Artifact(s) can be found in directory Fone.

[tmach@localhost test]$ cd Fone
[tmach@localhost Fone]$ ll
total 1
-rw-rw-r--. 1 tmach tmach 1833 Dec 10 09:25 src_sol01.sol.ast

[tmach@localhost test]$ solc src_sol01.sol --asm -o Fone
Compiler run successful. Artifact(s) can be found in directory Fone.

[tmach@localhost test]$ cd Fone
[tmach@localhost Fone]$ ll
total 1
-rw-rw-r--. 1 tmach tmach 15180 Dec 10 09:27 HelloWorld.evm

 

 

감사합니다.

반응형

댓글