vscode-cpptools icon indicating copy to clipboard operation
vscode-cpptools copied to clipboard

Use higher language standard version in build tasks, if compiler's default is old

Open v-ericawu opened this issue 3 years ago • 3 comments

Environment

  • OS and version: Mac x64 10.14.6
  • VS Code: 1.65.1
  • C/C++ extension: 1.9.5
  • GDB / LLDB version: GDB 11.2

Bug Summary and Steps to Reproduce

Steps to reproduce:

  1. Create a folder named 'Test' and add a .cpp file in it.
  2. Open 'Test' with VS Code and add the following code to.cpp file:

#include <bits/stdc++.h>

using namespace std;

int main() { //Input: cost = [1, 100,1,1,1,100,1,1,100,1] //Output: 6 vector<int> cost;

cost = {1, 100, 1, 1, 1, 100, 1, 1, 100, 1}; cout << cost.size() << endl;

set<int> s; s.insert(1); s.insert(2); s.insert(3); s.insert(4); s.insert(5);

for (auto i : s) { cout << i << ""; } }

  1. Click "Run and Debug" -> C++ (GDB/LLDB) -> C/C++: g++ build and Debug active file.
  2. Observe the result of TERMINAL

Actual: issue

Expected behavior

Compile success and without errors Windows VSCode: image

Linux VSCode: image

Code sample and Logs

Note: The contents of the "[bits/stdc++.h](http://www.manongjc.com/detail/39-qtntfmfdqqrhfth.html)" file can be added manually

// 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
// <Licenses - GNU Project - Free Software Foundation>.

/** @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 <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

Additional context

  1. This issue was reported by VS FeedbackTicket 1577954

  2. This issue not repro on VSCode Windows/Linux version, while repro on Mac version

v-ericawu avatar Jul 19 '22 06:07 v-ericawu

The default std version on Mac clang might be older. The user should add something like "-std=c++17" to their "args" in tasks.json.

sean-mcmanus avatar Jul 19 '22 20:07 sean-mcmanus

The IntelliSense is using a higher Language Standard version if the compiler's default std is old. While the build task is using whatever the compiler's default is. So as Sean mentioned, if you modify your build task in tasks.json and add args to it, the issue will be resolved.

        {
            "type": "cppbuild",
            "label": "C/C++: g++.exe build active file",
            "command": "g++.exe",
            "args": [
                "-std=c++17",
                ...
            ],
           ...
        }

As an enhancement, we can add this arg to the task template, if the compiler's default language standard version is older than 14.

elahehrashedi avatar Jul 19 '22 22:07 elahehrashedi

Compile success when setting "-std=c++17": image

v-ericawu avatar Jul 20 '22 03:07 v-ericawu

Summarizing the above comments: tasks.json is not setting -std in args; In other words, VS Code or the relevant extension (ms-vscode.cpptools) has a reproducible bug on Mac version. Having the user manually set -std in tasks.json args for every C++ project (requiring std=c++17 or higher) does not resolve this issue; instead, it forces more people to spend time rediscovering this issue. I hope that clarifies things so that more people will be motivated to upvote this issue using the emoji in the Additional context section above.

vtrandal avatar Apr 26 '23 05:04 vtrandal

I believe this will also be fixed in the next XCode release (i.e. as shown at https://en.wikipedia.org/wiki/Xcode) that merges in the LLVM 16 changes from https://github.com/llvm/llvm-project/commit/3e99b8d947ac024831e59be2b604ac67a24fed94 such that the default will be updated to C++17.

sean-mcmanus avatar Apr 27 '23 21:04 sean-mcmanus