> For the complete documentation index, see [llms.txt](https://whs3-doge5k.gitbook.io/smartcontract-security-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whs3-doge5k.gitbook.io/smartcontract-security-docs/basics/2./undefined/pragma.md).

# 컨트랙트 선언과 pragma

solidity의 컨트랙트는 객체 지향 언어의 클래스처럼 동작한다. 그래서 정보를 담는 변수(상태변수)를 가지고 있고, 이 변수의 값을 바꾸는 함수도 함께 포함하고 있다.

## &#x20;Pragma solidity

`pragma solidity ^0.8.24;`와 같이 선언하며, 해당 소스 코드가 어떤 버전의 solidity 컴파일러에서 작동할지를 지정한다.

* `^0.8.24`는 "0.8.24이상, 그러나 0.9.0 미만"의 버전과 호환된다는 뜻
* 버전 충돌을 방지하고 예측 가능한 컴파일을 위해 매우 중요하다.

{% code title="solidity" %}

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
```

{% endcode %}

{% hint style="info" %}
SPDX-License\_Identifier는 라이선스를 명시하는 주석으로, 필수는 아니다.
{% endhint %}

## 컨트랙트 선언

스마트 컨트랙트에 관한 내용은 [이더리움 기초 > 스마트 컨트랙트 ](/smartcontract-security-docs/basics/1./undefined-1/undefined.md)참고

* 컨트랙트는 스마트 계약의 기본 단위이며, 변수와 함수로 구성된다.
* 하나의 solidity 파일 안에 여러 개의 컨트랙트를 정의할 수 있다.

아래 예제 코드로 Foundry 환경에서 실행해보자.

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract Hello {
    string public message = "Hello, world!";
}
```

* `Hello`라는 이름의 컨트랙트를 정의한다.
* `message`는 `public` 상태 변수이기 때문에, 자동으로 getter 함수를 생성된다.
  * 따라서, 누구나 외부에서 `Hello.message( )`를 호출해 `"Hello, world!"` 값을 확인할 수 있다.&#x20;

## Foundry 환경에서 작성

```bash
forge init contracts
cd contracts
```

`forge build`명령어로 컴파일이 정상 작동되면, prama 버전이 잘 적용된 것이다.&#x20;

공식 문서 참고 : [Solidity-Pragma](https://docs.soliditylang.org/en/latest/layout-of-source-files.html#version-pragma), [Foundry Book - Getting Started](https://book.getfoundry.sh/getting-started/installation)
