프로그래밍 언어/C C++

맥에서 <bits/stdc++.h> 사용하기

트리맨스 2021. 8. 17. 01:04
반응형

 

알고리즘 문제를 풀이할 때 C++을 이용해서 푸는 경우에 bits/stdc++.h 헤더를 사용하기도 한다. 이것은 gcc 컴파일러에서만 사용할 수 있는 비표준 라이브러리이다. 위 헤더 파일은 대부분의 C++ 헤더 파일이 포함되어 있어 알고리즘 문제를 풀이할 때 헤더 파일을 여러 개 불러와야 하는 수고를 줄일 수 있다. 또한 대부분의 알고리즘 저지 사이트에서 bits/stdc++.h 를 지원하기 때문에 알고리즘 풀이에서는 문제없이 사용이 가능하다.

 

하지만 bits/stdc++.h  헤더 파일은 gcc 컴파일러에서만 동작한다. 리눅스 PC는 기본 컴파일러가 gcc일 것이다. 없으면 gcc 설치하면 된다. 그렇게 하면 bits/stdc++.h 를 바로 사용할 수 있다. 그러나 맥 에서는 상황이 다르다. gcc --version 을 터미널에 입력하면 다음과 같은 사진이 나온다.

 

 

글자를 잘 읽어 보면 Apple clang 이라는 문구가 보일 것이다. 즉 맥에 기본으로 설치된 gcc는 진짜 gcc가 아니라 애플에서 clang을 커스텀한 버전이라는 것을 알 수 있다. clang에서는 bits/stdc++.h 헤더 파일을 사용할 수 없다. 그렇다면 어떻게 사용을 할 수 있을까?

 

 

맥에 헤더 파일 적용시키기


방법은 간단하다. 라이브러리에 헤더를 추가하면 된다. 위 사진에서 InstallDir 경로를 보자. 저기가 clang의 설치 경로이다. 설치 경로는 컴퓨터마다 다를 수 있으므로 gcc --version 명령어를 통해 한번 확인을 해보자.  라이브러리는 /Library/Developer/CommandLineTools/usr/include 에 있다. 해당 경로에 bits 폴더를 만들고, 그 안에 stdc++.h 파일을 추가하면 된다. 아마도 sudo 권한이 필요할 것이다. stdc++.h 파일의 내용은 다음과 같다. 해당 깃허브는 gcc 미러 깃허브이다.

https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h

 

GitHub - gcc-mirror/gcc

Contribute to gcc-mirror/gcc development by creating an account on GitHub.

github.com

 

 

아래는 예시 명령어이다. 명령어 실행 전 현재 폴더에 stdc++.h 파일을 만들어 두자.

sudo mkdir /Library/Developer/CommandLineTools/usr/include/bits
sudo cp stdc++.h /Library/Developer/CommandLineTools/usr/include/bits

 

 

 

stdc++.h


파일 내용은 21년 8월 17일 기준 최신 내용이다.

 

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

// Copyright (C) 2003-2021 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>
#include <cwchar>
#include <cwctype>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#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 <codecvt>
#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 <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

#if __cplusplus >= 201402L
#include <shared_mutex>
#endif

#if __cplusplus >= 201703L
#include <any>
#include <charconv>
// #include <execution>
#include <filesystem>
#include <optional>
#include <memory_resource>
#include <string_view>
#include <variant>
#endif

#if __cplusplus > 201703L
#include <barrier>
#include <bit>
#include <compare>
#include <concepts>
#if __cpp_impl_coroutine
# include <coroutine>
#endif
#include <latch>
#include <numbers>
#include <ranges>
#include <span>
#include <stop_token>
#include <semaphore>
#include <source_location>
#include <syncstream>
#include <version>
#endif

 

위 파일을 넣어주고 컴파일하면 잘 될 것이다.

 

반응형