빌드 프로세스에서 가장 먼저 개입되는 부분.
#include, #define, #ifdef ,#pragma once 등에 해당
https://en.cppreference.com/w/cpp/preprocessor
Preprocessor - cppreference.com
The preprocessor is executed at translation phase 4, before the compilation. The result of preprocessing is a single file which is then passed to the actual compiler. [edit] Directives The preprocessing directives control the behavior of the preprocessor.
en.cppreference.com
1. Conditionally
: #if, #ifdef, #ifndef, #elif, #else, #endif...
컴파일러에게 보내기 전에 코드 자체를 수정해준다.
2. Replacing
#define...
치환 기능
3. Predefined macros
: __cplusplus, __FILE__, __LINE__, __DATE__, __TIME__, ...
__DATE__, __TIME__ 의 경우에는 실행 시간이 나오는 것이 아니라 컴파일 했을 때의 시간이 나온다.
4. 실제 예시
프리 프로세스는 #include 부분을 가져와서 복사 붙여넣기 해준다.
실제 복붙해서 넣어보면 똑같이 실행되는 것 확인 가능.
standard library의 경우에는 #include <>, 유저가 만든 헤더 파일을 가져올 경우에는 #inlcude ""
기본적으로 #include는 cpp 파일에 넣고, 헤더 파일에서 정보가 필요할 때에만 헤더 파일에 #include 해주는 것이 맞다.
실수해서 cat.h 를 2번 포함시켰을 경우에는, redefinition of 'class cat' 에러가 뜨게 된다.
ifndef CAT_H
#define CAT_H
이런 식으로 헤더파일을 고쳐주면 아무런 문제 없이 컴파일 된다.
(복붙이니까!)
결론적으로 #pragma once 를 헤더파일 위에 써주면 중복 위험 없이 사용할 수 있다.
모던 C++의 경우
#define, #ifdef 대신 consexpr if, constexpr, c++standard library 를 사용해 주는 것이 좋다.
#include 의 경우 헤더 파일이 2번 이상 중복으로 들어가는 것을 방지하기 위해서 #pragma once 사용 추천.
프리프로세서가 사용하는 메크로에는 컴파일러마다 조금씩 다르게 정의되어 있으니 구글링을 통해 확인 후 사용하기.
'모던C++ > 컴파일 프로세스 Compile Process' 카테고리의 다른 글
4. Static Library (0) | 2022.07.13 |
---|---|
3. extern, static (0) | 2022.07.12 |
1. 헤더 파일의 의미, 사용법 (0) | 2022.07.11 |
0. C++ 빌드 프로세스, 컴파일 프로세스 (0) | 2022.07.11 |