-
전처리기 지시문(Preprocessor directives) - 3프로그래밍 언어/C & C++ 2020. 6. 28. 18:36
분량이 많아져서 세번째 글까지 나눠서 나머지 지시문에 대해 설명합니다
전처리기 지시문에는 아래와 같은 종류가 있습니다.
- Macro definitions (#define #undef)
- Conditional inclusions (#ifdef #ifndef #if #endif #else #elif)
- Line control(#line)
- Error directive(#error)
- Source file inclusion(#include)
- pragma directive(#pragma)
* predefined macro names( __something__)
6. pragma directive(#pragma)
-#pragma
pragma는 컴파일러에 다양한 옵션을 설정하기 위해 사용됩니다. 이 옵션들은 어떤 환경에서 어떤 컴파일러를 쓰느냐에 따라 달라질 수 있습니다.
보편적으로 쓰이는 pragma를 몇개 보자면,
#pragma once : 이 지시문은 이 지시문이 사용된 헤더 파일을 단 한 번만 포함시키라는 뜻입니다. 같은 헤더 파일을 여러 소스 코드에서 include 해줄 수 있는데, 그럴 경우 해당 헤더 파일은 딱 한 번만 include하도록 만듭니다.#pragma once
#prama warining : 특정 에러를 무시하고 싶을 때 사용합니다.
예를 들자면 scanf를 사용할 때 에러가 난다고 컴파일러에서 경고를 하는 경우가 있는데, 그럴 경우 아래와 같은 코드를 추가해줌으로써 무시할 수가 있습니다.#pragma warning(disable: 4996)
예시로, GNU gcc에서 사용되는 Pragma입니다.https://gcc.gnu.org/onlinedocs/cpp/Pragmas.html#Pragmas
Pragmas (The C Preprocessor)
Sometimes, there is an identifier that you want to remove completely from your program, and make sure that it never creeps back in. To enforce this, you can poison the identifier with this pragma. #pragma GCC poison is followed by a list of identifiers to
gcc.gnu.org
7. predefined macro names(__something__)
항상 정의되어 있는 매크로들이 있습니다. 항상 2개의 언더바(_)로 시작하는데요, 대부분 있습니다만 오래된 컴파일러에는 없기도 합니다.
__FILE__ : 현재 실행 파일의 이름을 문자열로 보여줍니다.
__LINE__ : 해당 코드의 Line number을 보여줍니다.
__FUNCTION__ : 해당 코드가 실행되는 함수명을 보여줍니다.
__DATE__ : Month date year 순으로 보여줍니다.__TIME__ : hour minute second 순으로 보여줍니다.
__cplusplus : 컴파일러가 사용하는 c++의 버전을 정수값으로 return해줍니다. (주로 많이 사용하는 경우는 extern "C"와 함께인데 다음에 extern을 정리할 때 다루려합니다.)
___STDC__: 컴파일러가 Standard C를 따르는가? 맞다면 1을 return 합니다.
#include <iostream> int main() { //FILE LINE std::cout<<__FILE__<<std::endl; std::cout<<__LINE__<<std::endl; std::cout<<__FUNCTION__<<std::endl; // DATE TIME std::cout<<__DATE__<<std::endl; std::cout<<__TIME__<<std::endl; // C compiler std::cout<<__cplusplus<<std::endl; std::cout<<__STDC__<<std::endl; } /* Output main.cpp 5 main Jun 28 2020 09:21:33 201703 1 */
이번 글에서는 전처리 지시문 중에 pragma directive, predefined macro names 를 다뤘습니다.
대부분의 내용은 아래의 사이트에서 보실 수 있습니다.http://www.cplusplus.com/doc/tutorial/preprocessor/
틀린 부분이 있거나 오타가 있다면 댓글로 지적해주시기 바랍니다:)
'프로그래밍 언어 > C & C++' 카테고리의 다른 글
[c/c++] int8_t, uint8_t, int16_t, uint16_t ? (0) 2021.03.10 전처리기 지시문(Preprocessor directives) - 2 (0) 2020.06.27 전처리기 지시문(Preprocessor directives) - 1 (1) 2020.06.26