언어/C&C++ 기본

[C++] How C++ Works

차가운오미자 2021. 6. 14. 18:23

이 강의의 필기임.

https://www.youtube.com/watch?v=SfGuIVzE_Os&list=PLlrATfBNZ98dudnM48yfGUldqGD0S4FFb&index=5

 

 

C++이 Executable 파일이 되기 까지: 

src files (text) -> compiler -> binary (library, exe etc)

 

#include <iostream> //preprocessor statement, happens before compile
// find a file called iostream, take all the contents of this file and put in here
// iostream includes cout function

int main(){
// entry point of the application
    std::cout << "Hello World!" << std::endl;
    std::cin.get() // will wait until it gets an enter
}

 

코드는 기본적으로 라인 바이 라인으로 실행된다.

main은 스페셜 케이스라서 int를 반환한다고 했음에도(선언에) return문을 안넣어도 자동으로 0을 받는다.

  • << : overloaded operator, 함수처럼 생각하면 된다.

Include (Preprocessor statement)

include 문은 evaluated before compilation. This is why it's called a preprocessor statement. Once the include sentence is evaluated, the whole source file is compiled

 

compiler changes this code (src file code) to machine code.

Properties

In visual studio, on the upper side there are two options,

1. Solution Configurations > set as "Debug" in default (other option: "Release")

* 디버그 컴파일과 릴리즈 컴파일의 차이

  • Release: 초기화 x, 같은 문자열 상수도 다른 공간에 할당, 코드 최적화 -> 속도 faster, 문제점 없을 때 빌드
  • Debug: 실행파일에 디버깅 정보를 삽입하며 debug 파일 안에 실행 파일 생성. 실행 상태 파악 가능. 계속 체크하니까 속도 slower

=> 결국 근본적인 차이는 디버깅 정보가 실행파일에 포함되는지 여부이다.

참고) https://okdy.tistory.com/entry/DEBUG%EC%99%80-RELEASE-%EC%BB%B4%ED%8C%8C%EC%9D%BC%EC%9D%98-%EC%B0%A8%EC%9D%B4

 

2. Solution Platforms> 기본적으로 x86 로 설정되어 있다(else: x64), 이게 어느 플랫폼에서 돌아갈지를 지정 (which platform we target for)

Properties 종류:

  • Configuation Properties
  • Configuration: Active(Debug)
  • Platform: Active(win32)

=> Properties에서 더 자세한 사항 확인, 변경 가능

 

Configuration Type: 기본적으로 Application(.exe) 셋되어 있다. 컴파일러가 산출하는 아웃풋 바이너리 형식. 

* Properties > C/C++ 에서 어떻게 이 파일이 컴파일 될 것인지를 세팅한다. 

 

Header Files

Header files are "included", not compiled. It is compiled when the whole cpp file is compiled. It creates an object file for indivdual cpp files. They need to be linked. This is what the linker does. Linker glues them together.

(해석) 헤더파일들은 'include' 되는 것이 compile 되는 것이 아니다. cpp 파일이 컴파일될 때 cpp 파일의 일부로서 컴파일 되는 것이다. 컴파일러는 각 cpp 파일별로 obj 파일을 생성한다. 이 obj 파일들은 연결되어야 하는데, 이 연결을 링커가 담당한다. 

 

Compilation

Ctrl + F7 > 컴파일만

 

* 디버깅을 위해서는 Output 창의 messages를 살피기. 여기에서 더 많은 에러에 관한 정보를 얻을 수 있다. 

* Error list는 거의 있으나 마나이다. 

 

Build

프로젝트를 빌드하면 exe 파일이 만들어진다.

 

Declaration vs. Definition

Declaration

- 컴파일러는 함수가 실제로 어디에 존재하는 지에 대해서 신경쓰지 않는다. 그냥 선언이 되어 있으면 어디에 있겠거니 하고 간주해버린다. 이렇게 컴파일러에게 어딘가에 함수 body 가 있을거야! 하고 알려주는 게 선언. 

#include <iostream>

void shout(const char * mes); // declaration of the function "shout"

int main(){

    shout("Hello world");
    cin.get();
}

 

Definition

함수가 실제로 동작하는 것을 정의하고 있는 것이다. function의 body part.

void shout(const char* mes){
    cout << mes << endl;
}

 

Linker

- 함수의 정의가 어디에 있는지 찾는다. 

- 다양한 함수들을 잇는다. 

- 각 cpp 파일마다 obj 파일이 만들어진다. 그러면 링커가 이들을 이어 붙이는 역할을 한다. 

- 위의 예시에서 'shout' 함수의 정의를 찾아서 이거를 shout을 선언하고 있는 부분과 이어붙여서 exe 파일을 최종적으로 만들어내는 것이다. 

 

 

'언어 > C&C++ 기본' 카테고리의 다른 글

[C++] Class  (0) 2021.06.25
[C++] Header Files  (0) 2021.06.14
[C++] Variables, Functions, Pointers, References  (0) 2021.06.14
[C++] Linker  (0) 2021.06.14
[C++] Compiler  (0) 2021.06.14