Skip to content

Xcode

Introduction

Xcode is an Integrated Development Environment (IDE) that runs on macOS, developed by Apple Inc.

Installation

Method 1

Open the App Store that comes with your Mac (or try the quick link) to download Xcode. Click "Get", then enter your Apple ID and password to start downloading and installing.

Method 2

Visit the Apple Developer Downloads page, log in with your Apple ID, then find the latest stable version of Xcode (i.e., the latest version without Beta, here it's 11.6):

Click the blue filename in the pop-up window to download. After obtaining the compressed file, extract it using the system's built-in tool to get the file Xcode.app. Move this file to the "Applications" folder to use it.

Basic Configuration

When opening Xcode for the first time, you might encounter the following pop-up window:

This window is the installation guide for Xcode components. Click Install and enter the current user's password.

After installation, the left side of the interface displays:

Click Create a new Xcode project, then select Command Line Tool under macOS at the top, and click Next in the bottom right corner.

Next, you can name the project, but the most important part is selecting the project's language. You can choose C or C++ at the Language section at the bottom according to your needs:

The project directory can be chosen as needed. After creation, Xcode will automatically open this project and create a main file (suffix .c for C, .cpp for C++).

Click this file to open the editing area:

After writing code, you can press ⌘B to Build and ⌘R to Run. After running, drag to get three parts:

Generally, we only use the 【Editing Area】 and 【Running Area】. If the program requires input, you can input in the 【Running Area】 and then get the output. The interface looks like this:

Following this method, you can run any single C/C++ program.

Using the Universal Header File

During code writing, you might use many header files. A common solution is to use the universal header file.

We include the universal header file in the first line of the source code, but during compilation, it prompts: 'bits/stdc++.h' file not found. This means the header file was not found.

This is because macOS uses libc++ as the default C++ standard library implementation, while the universal header bits/stdc++.h is unique to GNU libstdc++.

However, we can manually create a universal header file to use.

Step 1

Open Terminal (Terminal.app), navigate to the folder where Xcode stores header files, i.e.:

1
cd /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1

If the Xcode version is greater than or equal to 12.5, then

1
cd /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/

Step 2

Create a bits folder and enter it:

1
2
mkdir bits
cd bits

Create the stdc++.h file using vim:

1
vim stdc++.h

The interface is as follows:

Next, we need to edit the file via vim. Press i (insert) on the keyboard to enter insert/edit mode (-- INSERT -- appears at the bottom):

Copy and paste the following code block into the terminal:

Universal Header File Code Block
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2020 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 <cstdbool>
#include <cstdint>
#include <ctgmath>
/* https://stackoverflow.com/a/25892335/15125422 */
#if defined(__GLIBCXX__) || defined(__GLIBCPP__)
#include <cstdalign>
#include <cuchar>
#endif
#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 <type_traits>
#include <typeindex>
#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 <memory_resource>
#include <optional>
#include <string_view>
#include <variant>
#endif

#if __cplusplus > 201703L
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <stop_token>
// #include <syncstream>
#include <version>
#endif

This file originates from libstdc++ version 10.2.0 and has been slightly modified for compatibility with libc++.

Press the Esc key at the top left of the keyboard to exit edit mode, then directly type :wq and press Enter to save the file.

Step 3

Close the terminal and return to Xcode. Press ⌘B/⌘R again to compile, and you will find the compilation is successful:

Pros and Cons

Pros: Developed by Apple, suitable for Mac users, with a complete and aesthetically pleasing interface.

Cons: Xcode is primarily used for Apple program development. For competitive programming, it is feature-redundant, has a large installation package size, and is only available on Mac.