编写 ANSI C++ 的资源代码
我上次大量使用 C++ 是几年前的事了,而且是严格在 Windows 平台上完成的。具体来说,我使用 Microsoft Visual Studio 作为我的 IDE,并开发了一些习惯模式来使用 Microsoft 的 C++ 版本。例如,我使用 void main() 而不是标准的 int main()。
现在,我正在上一门课程,要求开发符合 ANSI C++ 的程序,并且代码将使用 g++ 进行编译。我很快了解到 system ("PAUSE") 在 g++ 中不起作用,可能是 Microsoft 的事情。
有谁知道有什么好的资源(网站、教程、书籍),我可以在那里学习更多更好的 ANSI C++ 投诉吗?
谢谢。
The last time I heavily used C++ was years ago, and it was strictly done on the Windows platform. Specifically, I used Microsoft Visual Studio as my IDE and developed some habitual patterns to use Microsoft's C++ version. For example, I used void main() instead of the standard int main().
Now, I am taking a class where it is required to develop programs to be ANSI C++ compliant and the code will be compiled using g++. I quickly learned that system ( "PAUSE" ) does not work in g++ and is probably a Microsoft thing.
Does anyone know of any good resources ( sites, tutorials, books ) where I can learn what more to be better ANSI C++ complaint?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您使用的是 g++,请使用 -pedantic 和 -std=c++98 标志进行编译。 ANSI C++ 的唯一标准实际上是 ISO 标准文档,不推荐给初学者。顺便说一下,您对 system("PAUSE") 的看法是错误的 - system() 是 ANSI C++ 的一部分 - 然而它的参数不是标准化的。
If you are using g++, then compile with the -pedantic and -std=c++98 flags. The only standard for ANSI C++ is really the ISO Standard document, which cannot be recommended to a beginner. You are mistaken about system("PAUSE"), by the way - system() is part of ANSI C++ - it's parameter is not standardised, however.
大多数 C++ 书籍都与平台无关(当然,除非它们是专门针对 Visual C++ 编写的)。以下是 ## 上的专家推荐的一些好书 C++
Most C++ books will be platform agnostic (of course unless they are made specifically for Visual C++). Here are some good books that are recommended by the pros on ##C++
当您的代码不符合 ISO C++ 标准时,GCC 会很好地告诉您(不是说它是 ISO 标准,也不是 ANSI 标准)。设置警告选项-Werror -Wall,简单修复所有警告;你很快就会改掉不合规的习惯,收到的警告也会越来越少。
GCC will do a good job of telling you when your code is not ISO C++ compliant (not that it is an ISO standard, not an ANSI standard). Set the warning options -Werror -Wall, and simply fix all warnings; you soon get out of non-compliant habits and ger fewer and fewer warnings.
gcc 有很多很好的教程,有一个标志可以确保代码也兼容 ANSI C,
gcc -ansi -Wall -fsyntax-only -pedantic
Per,注释 另外,您可以使用
* -Wextra 打开一些额外的警告,
更新感谢大写的更新。,并提到 -pedantic
There are plenty of good tutorials gcc, has a flag that will make sure the code is ANSI C compliant too,
gcc -ansi -Wall -fsyntax-only -pedantic
Per, comments In addition you can use
* -Wextra to turn on a few extra warnings,
update thanks for update on capitalization., and mention of -pedantic
很多海湾合作委员会的粉丝。
在 Visual Studio 下编译时要尽可能严格的重要标志是:
/Za 禁用 Microsoft 特定扩展,/W4 是最高警告级别(/Wall 除外,它会抱怨一些荒谬的事情)。
我还建议您使用现代版本的 Visual Studio - 旧版本 (VC6) 非常不符合标准,并且 Microsoft 官方假装它们在这一点上从未存在过。
Lots of gcc fans.
The important flags to compile with under Visual Studio to be as strict as possible are:
/Za disables Microsoft specific extensions and /W4 is the highest warning level (except /Wall, which complains about ridiculous things).
I'd also recommend you use a modern version of Visual Studio - old versions (VC6) were ridiculously non-conformant, and Microsoft officially pretends they never existed at this point.
我强烈推荐这两个:
当然,如果您想要 C++ 的教程介绍,那么它们都不是很好,但看起来您已经了解了一些 C++,并且需要学习更多内容并纠正坏习惯。根据我个人的经验,上述两个方法对于做到这一点非常有用。
关于
comp.lang.c++
,请确保您完全阅读了他们的常见问题解答,并在发布之前在那里潜伏了一段时间。当然,这同样适用于 stackoverflow,尽管这里可能不需要潜伏。使用
g++
,使用g++ -ansi -pedantic -Wall -Wextra -Weffc++
编译程序,并确保您理解所有警告。我用:I would highly recommend these two:
Granted, they both are not terribly great if you want a tutorial introduction to C++, but looks like you already know some C++, and need to learn more, and correct bad habits. From my personal experience, the above two are highly useful in doing exactly that.
About
comp.lang.c++
, make sure you fully read their FAQ and lurk there a while before posting. The same applies to stackoverflow of course, although lurking may not be necessary here.Using
g++
, compile your programs withg++ -ansi -pedantic -Wall -Wextra -Weffc++
, and make sure you understand all the warnings. I use:我会推荐。
网站:
书籍:
说/听专家并了解他们所说的内容和原因。
I would recommend.
websites:
books:
talking / listening to experts and understanding what they have to say and why.
C++ Primer(第 4 版)似乎是当今最好的初学者书籍,因为它采用现代方法并教授该语言的所有重要部分。 《Accelerated C++》是另一本专业人士经常推荐的书,它是一本很好的入门书,但我不建议再购买它,因为《C++ Primer》很好地取代了它,并更好地涵盖了该语言。
注意:C++ Primer Plus(这是一本相当糟糕的书)与C++ Primer 无关。
当然,Stroustrup 的《C++ 编程语言》总是需要提及的,因为它是由该语言之父编写的。许多人认为它很有启发性,但我个人不推荐它来学习语言。
我提到的所有书籍都只描述了 ISO 标准,没有非标准扩展。这些书中的程序应该适用于任何 C++ 编译器。
C++ Primer (4th Ed) appears to be the best beginner book these days as it takes a modern approach and teaches all the important parts of the language. Accelerated C++ is another book often recommended by professionals and it serves as a good introduction, but I wouldn't recommend buying it anymore because C++ Primer nicely replaces it and covers the language better.
Note: C++ Primer Plus (which is a rather bad book) has nothing to do with C++ Primer.
Stroustrup's The C++ Programming Language always needs to be mentioned, of course, because it is written by the father of the language. Many people find it enlightening, but I personally would not recommend it for learning the language.
All the books I mentioned only describe the ISO standard with no non-standard extensions. The programs in these books should work with any C++ compiler.
每当我有疑问时,特别是关于标准 C++ 库的问题,我喜欢访问 www.cplusplus.com。
I like visiting www.cplusplus.com whenever I have a doubt, specially about the standard C++ library.