最近换了电脑,vscode的环境是重新配的,遇到了些许问题。欢迎到我的个人博客阅读。本篇文章博客地址
1. Code Runner 对于C++的一些函数不识别
这是因为code runner默认执行命令里没有添加c++17的选项。
解决方案:
- 打开vscode设置,搜索
code runner
,找到Code-runner: Executor Map
,点击edit in settings.json
。 - Find the
cpp
and add-std=c++17
aftercd $dir && g++
Like this"cpp": "cd $dir && g++ -std=c++17 $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
2. Code Runner 运行C++是在OUTPUT里运行的,不能通过键盘输入数据
解决方案:
打开vscode设置,搜索code runner
,勾选Code-runner: Run In Terminal
3. vscode 不识别#include<bits/stdc++.h>
我有问过chatgpt关于为什么mac没有这个库,回答是这样的:
#include<bits/stdc++.h>
是一个常用的 C++ 头文件,它包含了常用的标准库和一些常见的标准头文件。在一些编译器和环境中,这个头文件是存在的,但并不是所有的环境都支持它。
在 macOS 上,通常使用的是 Clang 或者 GNU 编译器集合中的 Clang。Clang 是一个优秀的 C++ 编译器,它支持大多数 C++11、C++14、C++17 和部分 C++20 特性,但并不提供 #include<bits/stdc++.h>
这个头文件。
这个头文件通常出现在一些 Windows 平台上的编译器(比如 MinGW 和 Visual C++)中,用于方便包含所有标准库的头文件。但是在其他平台,比如 macOS 上,通常需要明确地包含所需的标准库头文件,例如 <iostream>
, <vector>
, <algorithm>
等。
所以,在 macOS 上编写 C++ 代码时,你需要根据具体需要,显式地包含你需要的标准库头文件,而不依赖于 #include<bits/stdc++.h>
。
解决方法
实际上万能头不就是把所有库都import一遍而已。解决方案我看的这一篇博客。我在此基础上优化了一部分步骤,更加简洁。
- 终端中输入
echo | g++ -v -x c++ -E -
, 其中/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
即为我们要找的路径。 - 修改include文件夹的访问权限为读写。
sudo chmod 777 /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
- 直接
cd /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
- 创建
bits
文件夹并进入。mkdir bits
,cd bits/
- 创建’stdc++.h’,
touch stdc++.h
- 打开刚刚创建的文件,
open stdc++.h
,这时应该是默认vscode进行打开的。进行编辑:
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2014 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
//
#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 <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 <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
最后保存退出即可。