简体   繁体   English

用c ++编译14

[英]Compile in c++14

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++'). 不幸的是我无法使用该标头编译终端,它会产生很多错误(只用'g ++'编译)。 Also, when I'm at my university and I'm using PuTTY I get the same errors while using this header. 另外,当我在大学时我正在使用PuTTY时,我在使用此标题时会遇到相同的错误。 However, I don't get the errors when I compile with 'g++ -std=c++14'. 但是,当我用'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'时,这个错误不会发生并且程序将完全编译

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++98 for C++ , which is the decades old C++98-standard enhanced with GNU extensions (most of which are conformant). g ++默认为-std=gnu++98 for C ++ ,这是几十年前使用GNU扩展增强的C ++ 98标准(大多数都符合要求)。

Choose the proper revision: -std=c++1y -pedantic is 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编写的程序?

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. 看看你说你必须使用的内容和.cpp文件的名称格式,我想我在同一个班级。 A year later, looks like, but here's my solution for archive's sake: 一年后,看起来像,但这是我的存档解决方案:

The std_lib_facilities.h header comes with the Bjarne Stroustrup textbook, "Programming: Principles and Practices Using C++". std_lib_facilities.h头文件附带Bjarne Stroustrup教科书“编程:使用C ++的原理和实践”。 For those unaware, Bjarne Stroustrup invented C++ (he has a pretty good idea what he's talking about). 对于那些没有意识到的人,Bjarne Stroustrup发明了C ++(他非常清楚他在谈论什么)。 Incidentally, the book is a fantastic way to learn C++, if one takes the time to actually read it. 顺便说一句,如果花时间实际阅读它,这本书是学习C ++的绝佳方式。 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). std_lib_facilities.h头文件只是一个方便的C ++初学者头文件,包含教科书中使用的所有主要标准库的链接,以及一些帮助解决潜在错误或错误的辅助函数,或者只是方便学习(例如一个error()函数,它为学生处理简单的异常抛出,或者为向量添加“越界”检查)。 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. Stroustrup使用C ++进行更新,因此包含了几个需要c ++ 11标准的库。 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. CSCE部门希望其学生(至少在早期课程中)连接到该部门的Unix系统并从那里进行编译,以避免与下载和更新编译器混淆。

I happened to already have had a couple C++ classes beforehand, and thus already had g++ set up on my Ubuntu laptop. 我事先已经有过几个C ++类,因此已经在我的Ubuntu笔记本电脑上设置了g ++。 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) . 我避免包括std_lib_facilities尽可能长,因为我得到了与Topic Creator Joe相同的错误,其中g ++不识别“c ++ 11”部分(手动包括所需的库工作正常,直到我们必须使用教科书中使用其中一个标题辅助函数的类)。 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). 最后,我在网上找到了一个帮助主题,建议我简单地将我的g ++编译器更新到4.7或更高版本,因为4.6及更低版本不支持C ++ 11(当然还有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. 与在Mac或Windows上可能习惯的更新相比,这是奇怪的相关,我怀疑确切的过程是否会适用,但那可能是问题:它只是g ++的旧版本,它需要一个更新以编译C ++ 11及更高版本。 I recommend searching for ways to update g++/gcc for Mac. 我建议您搜索更新Mac的g ++ / gcc的方法。

Should, y'know, anyone else with this problem stumble upon this and not have their problem solved yet. 你应该知道,其他任何有这个问题的人都会发现这个并没有解决他们的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM