xcode 在 C++14 中编译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25854252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:33:47  来源:igfitidea点击:

Compile in c++14

c++xcodeterminalputtyc++14

提问by Joe

So in my CSE course we are given a header file to use right now for our programs that we're writing.

所以在我的 CSE 课程中,我们得到了一个头文件,用于我们正在编写的程序。

Unfortunately I can't get terminal to compile using that header, it gives quite a few errors (compiling with just 'g++'). Also, when I'm at my university and I'm using PuTTY I get the same errors while using this header. However, I don't get the errors when I compile with 'g++ -std=c++14'.

不幸的是,我无法让终端使用该头文件进行编译,它会出现很多错误(仅使用“g++”进行编译)。此外,当我在我的大学并使用 PuTTY 时,我在使用此标头时遇到了相同的错误。但是,当我使用“g++ -std=c++14”进行编译时,我没有收到错误消息。

I've tried compiling with this command on terminal on my mac, but it says it doesn't recognize the c++14 part.

我已经尝试在 Mac 上的终端上使用此命令进行编译,但它说它无法识别 c++14 部分。

dhcp-10-202-147-243:hw1pr1 Admin$ g++ -std=c++14 hw1pr1.cpp
error: invalid value 'c++14' in '-std=c++14'

Any help on how I could get this to work would be greatly appreciated. Hopefully this all made some sort of sense.

任何关于我如何让它工作的帮助将不胜感激。希望这一切都有意义。

Here's the error I get when I compile with the header file I'm talking about in terminal with just g++.

这是我在仅使用 g++ 的终端中使用头文件进行编译时遇到的错误。

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ext/hash_map:212:5: warning: 
      Use of the header <ext/hash_map> is deprecated. Migrate to <unordered_map>
      [-W#warnings]
#   warning Use of the header <ext/hash_map> is deprecated.  Migrate to ...
    ^
In file included from read_first_name.cpp:1:
./std_lib_facilities_4.h:43:20: error: no matching function for call to object
      of type 'hash<char *>'
            return hash<char*>()(s.c_str());
                   ^~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ext/__hash:39:12: note: 
      candidate function not viable: 1st argument ('const value_type *'
      (aka 'const char *')) would lose const qualifier
    size_t operator()(char *__c) const _NOEXCEPT
           ^
In file included from read_first_name.cpp:1:
./std_lib_facilities_4.h:112:8: warning: comparison of unsigned expression < 0
      is always false [-Wtautological-compare]
                if (i<0||size()<=i) throw Range_error(i);
                    ~^~
./std_lib_facilities_4.h:118:8: warning: comparison of unsigned expression < 0
      is always false [-Wtautological-compare]
                if (i<0||size()<=i) throw Range_error(i);
                    ~^~
3 warnings and 1 error generated.

This error doesn't happen and the program will compile fully when I use PuTTY and 'g++ std=c++14'

当我使用 PuTTY 和 'g++ std=c++14' 时,不会发生此错误并且程序将完全编译

回答by Deduplicator

There's lots of change between C++ standards, so what is valid in one revision need not be in another.

C++ 标准之间有很多变化,因此在一个修订版中有效的内容不一定在另一个修订版中有效。

g++ defaults to -std=gnu++98for C++, which is the decades old C++98-standard enhanced with GNU extensions (most of which are conformant).

g++ 默认-std=gnu++98为 C++,这是几十年前用 GNU 扩展增强的 C++98 标准(其中大部分是一致的)。

Choose the proper revision: -std=c++1y -pedanticis a very close approximation to C++14.

选择正确的修订版:-std=c++1y -pedantic非常接近 C++14。

What changes introduced in C++14 can potentially break a program written in C++11?

C++14 中引入的哪些更改可能会破坏用 C++11 编写的程序?

回答by Halcyon Serenade

Looking at what you say you're having to use and the name format of that .cpp file, I think I'm in the same class. A year later, looks like, but here's my solution for archive's sake:

看看你说你必须使用什么以及那个 .cpp 文件的名称格式,我想我在同一个班级。一年后,看起来像,但这是我为了存档的解决方案:

The std_lib_facilities.h header comes with the Bjarne Stroustrup textbook, "Programming: Principles and Practices Using C++". For those unaware, Bjarne Stroustrup invented C++ (he has a pretty good idea what he's talking about). Incidentally, the book is a fantastic way to learn C++, if one takes the time to actually read it. The std_lib_facilities.h header is just a convenient header file for beginners in C++, containing links to all the major standard libraries used in the textbook, as well as some helper functions that help account for potential mistakes or errors, or are just convenient for learning (such as an error() function that handles simple exception throwing for the student, or adding an "out of bounds" check for vectors). It's ultimately just a way to allow students to hop right into code without having to learn specifics about the header. Stroustrup keeps updated with C++ and thus includes several libraries that require the c++11 standard. The CSCE department wants its students (at least in this early class) to connect to the department's Unix system and compile from there, in order to avoid confusion with downloading and updating compilers.

std_lib_facilities.h 标头随 Bjarne Stroustrup 教科书“编程:使用 C++ 的原则和实践”提供。对于那些不知道的人,Bjarne Stroustrup 发明了 C++(他对自己在说什么非常清楚)。顺便说一句,这本书是学习 C++ 的绝妙方式,如果有人花时间实际阅读它的话。std_lib_facilities.h 头文件只是一个方便 C++ 初学者的头文件,包含指向教科书中使用的所有主要标准库的链接,以及一些帮助解释潜在错误或错误的帮助函数,或者只是方便学习(例如为学生处理简单异常抛出的 error() 函数,或为向量添加“越界”检查)。它' s 最终只是一种让学生直接跳到代码中而无需了解标题细节的方法。Stroustrup 使用 C++ 进行更新,因此包含几个需要 C++11 标准的库。CSCE 系希望它的学生(至少在这个早期班级)连接到该系的 Unix 系统并从那里编译,以避免与下载和更新编译器混淆。

I happened to already have had a couple C++ classes beforehand, and thus already had g++ set up on my Ubuntu laptop. I avoided including the std_lib_facilities for as long as possible since I was getting the same error as Topic Creator Joe, where g++ didn't recognize the "c++11" part (manually including the required libraries worked fine until we had to use a class from the textbook that used one of the header's helper functions) . Eventually, I found a help topic online that advised me simply to update my g++ compiler to 4.7 or higher, since 4.6 and lower doesn't have support for C++11 (or, of course, C++14). It was oddly rather involved compared to updates one might be used to on Mac or Windows, and I doubt the exact process would apply, but that is (was?) likely the problem: it's just an older version of g++, and it needs an update to compile C++11 and later. I recommend searching for ways to update g++/gcc for Mac.

我碰巧事先已经有几个 C++ 类,因此已经在我的 Ubuntu 笔记本电脑上设置了 g++。我尽可能避免包含 std_lib_facilities,因为我遇到了与 Topic Creator Joe 相同的错误,其中 g++ 无法识别“c++11”部分(手动包含所需的库工作正常,直到我们不得不使用课本中使用标题的辅助函数之一的类)。最终,我在网上找到了一个帮助主题,建议我简单地将我的 g++ 编译器更新到 4.7 或更高版本,因为 4.6 及更低版本不支持 C++11(当然,也包括 C++14)。与在 Mac 或 Windows 上可能习惯的更新相比,这有点奇怪,而且我怀疑确切的过程是否适用,但这就是(是?)可能的问题:它只是一个旧版本的 g++,它需要更新才能编译 C++11 及更高版本。我建议寻找为 Mac 更新 g++/gcc 的方法。

Should, y'know, anyone else with this problem stumble upon this and not have their problem solved yet.

你知道,其他任何有这个问题的人都应该偶然发现这个问题并且他们的问题还没有得到解决。