跳转至

Xcode

簡介

Xcode 是一個運行在 macOS 上的集成開發工具(IDE),由 Apple Inc. 開發。

安裝

方法一

打開蘋果電腦自帶的 App Store(或者嘗試 快捷鏈接)下載 Xcode。點擊獲取,然後輸入蘋果賬號密碼開始下載安裝。

方法二

訪問 蘋果開發者下載頁面,用蘋果賬號登錄,然後找到 Xcode 最新的穩定版本安裝包(即不含 Beta 的最新版本,此處為 11.6):

點擊彈出框內藍色的文件名即可下載。得到壓縮包之後,用系統自帶的工具進行解壓,然後得到文件 Xcode.app。把這個文件移動到【應用程序】文件夾後即可使用。

基礎配置

首次打開 Xcode 時,可能會遇到下列彈出窗口:

這個窗口是 Xcode 元件的安裝引導。點擊 Install 並輸入當前用户密碼即可。

安裝完畢後,界面左側顯示:

點擊 Create a new Xcode project(創建一個新的 Xcode 項目),然後選擇上方 macOS 中的 Command Line Tool(命令行工具),並點擊右下角的 Next

接下來,我們可以給項目命名,但最重要的是選擇項目的語言。我們可以根據自己的需求,在最下方 Language 處選擇 C 或者 C++:

項目的目錄可以根據需要選擇。創建完畢後,Xcode 會自動打開這個項目,並自動創建一個 main 文件(C 語言的後綴為 .c,C++ 語言的後綴為 .cpp)。

點擊這個文件,就可以打開編輯區域:

編寫代碼後,可以按⌘B 編譯(Build),⌘R 運行(Run)。運行後拖動,得到三個部分:

一般來説我們只使用【編輯區】和【運行區】。若程序有輸入,那麼在【運行區】中進行輸入之後,就可以得到輸出。界面呈現效果:

仿照這種方式,我們就可以運行任何的單個 C/C++ 程序。

萬能頭文件的使用

在編寫代碼過程中,我們可能會使用到很多頭文件。常用的解決方法是使用萬能頭文件。

我們在源代碼第一行引入萬能頭文件,然而編譯過程中卻提示:'bits/stdc++.h' file not found。即該頭文件未找到。

這是因為在 macOS 上默認使用 libc++ 作為 C++ 標準庫實現,而萬能頭 bits/stdc++.hGNU libstdc++ 所獨有的。

不過,我們可以手動編寫一個萬能頭文件來使用。

步驟 1

打開終端(Terminal.app),前往 Xcode 存儲頭文件的文件夾,即:

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

如果 Xcode 版本大於等於 12.5,那麼

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

步驟 2

創建 bits 文件夾並進入:

1
2
mkdir bits
cd bits

用 vim 創建 stdc++.h 文件:

1
vim stdc++.h

界面如下:

接着,我們需要通過 vim 編輯文件。敲擊 i(insert)鍵盤即可進入插入/編輯模式(下方出現 -- INSERT --):

將下面這段代碼塊複製並粘貼到終端中:

萬能頭文件代碼塊
  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

該文件來源於 10.2.0 版本的 libstdc++ 並經少許修改以兼容 libc++。

按鍵盤左上角的Esc退出編輯模式,然後直接輸入 :wq 並換行即可保存文件。

步驟 3

關閉終端,回到 Xcode。重新按下 ⌘B/⌘R 進行編譯,發現編譯成功:

優缺點

優點:由蘋果開發,適合 Mac 用户,界面齊全、美觀。

缺點:Xcode 主要用來蘋果程序的開發,對於競賽來説功能冗餘,安裝包大小較大,而且僅能在 Mac 端上使用。