프로그래밍 언어/PS를 위한 C++

[PS를 위한 C++] 3. #include <bits/stdc++.h> 헤더파일 사용하기

nageune 2023. 10. 14. 17:00
728x90
반응형

앞으로 모든 C++로 PS를 할 경우엔 이 헤더파일 하나만 사용하면 됩니다.

#include <bits/stdc++.h>

이게 무엇인지 궁금하시죠?
 

bits.stdc++.h

파일의 확장자와 #include 에서 알 수 있듯이 헤더파일입니다. 이 헤더파일은 모~~든 헤더파일(iostream, vector 등)이 포함된 헤더파일입니다.
 

장점

  • 한 줄로 모든 헤더파일을 쓸 수 있어서 편합니다.
  • STL이나 함수가 어떤 헤더파일에 있었는지 기억할 필요가 없습니다.
  • 대부분의 온라인 사이트에서 사용가능합니다.

단점

  • 표준 헤더가 아니기 때문에 gcc가 아닌 컴파일러에선 사용할 수 없습니다.
  • 사용하지 않는 헤더파일도 불러오기 때문에 컴파일 시간이 늘어납니다.
  • 개발환경에서 사용하기 위해선 설정을 해줘야 합니다.

 

bits/stdc++.h 헤더를 개발 환경에서 사용하는 방법

특정 경로에 bits 폴더, stdc++.h 헤더파일을 만들면 됩니다.
 
저는 Mac 기준, VS Code 기준으로 설명하겠습니다. 다른 OS 또는 ide를 사용하시는 분들은 구글링 해서 하세요! 한 번 설정해 놓으면 편합니다.
 

1. /usr/local/include 경로로 이동

cd /usr/local/include

 

2. bits 폴더 만들기

mkdir bits

 

3. bits 폴더로 이동

cd bits

 

4. stdc++.h 파일 생성

touch stdc++.h

 

5. stdc++.h 파일 열어서 작성 (Finder로 경로 찾아 이동한 다음 직접 수정해도 됩니다.)

vim 또는 vi는 터미널에서 텍스트 편집이 가능한 프로그램입니다. esc 키를 누르고 i를 누르면 타이핑이 가능합니다.

vim stdc++.h

 

6. stdc++.h 파일 내용

아래 내용 작성한 다음 esc 키를 누르고 wq! 를 입력하면 저장 후 빠져나옵니다.
55번 줄 cstdalign 헤더 파일을 주석처리한 이유는 C++ 버전에 따라 에러가 발생하기도 합니다.
우리가 사용할 C++17에선 에러가 발생합니다.

// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
// #include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <type_traits>
#include <typeindex>
#include <unordered_map>
#include <unordered_set>
#endif

 
이제 VS Code에서 bits/stdc++.h 헤더파일을 불러와서 코드를 실행할 수 있습니다!


 

728x90
반응형