Linux Ubuntu 11.10 上的 C 和 C++ 编程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7937602/
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
C and C++ programming on Ubuntu 11.10
提问by George Bora
I've recently installed Ubuntu 11.10 and along with it the CodeBlocks IDE and I am aware that I have gcc and the std libraries by default.
我最近安装了 Ubuntu 11.10 和 CodeBlocks IDE,我知道默认情况下我有 gcc 和 std 库。
My questions are:
我的问题是:
- Do you you have any tips for a new C++ programmer on Ubuntu?
- Any libraries I should get from the start?
- A really good IDE I'm missing? (YMMV but I prefer to work in IDE's)
- Any programming boons or traps I should be aware of from the start?
- 对于 Ubuntu 上的新 C++ 程序员,您有什么建议吗?
- 我应该从一开始就获得任何图书馆?
- 我缺少一个非常好的 IDE?(YMMV 但我更喜欢在 IDE 中工作)
- 从一开始我就应该注意任何编程的好处或陷阱?
采纳答案by Basile Starynkevitch
You don't need an IDE to code in C or C++ on Ubuntu. You can use a good editor (like emacs
, which you can configure to suit your needs.).
在 Ubuntu 上用 C 或 C++ 编写代码不需要 IDE。您可以使用一个好的编辑器(例如emacs
,您可以对其进行配置以满足您的需要。)。
Some few tips for a newbie:
给新手的一些提示:
- Always compile with
-Wall -Wextra
and perhaps even with-Werror -pedantic-errors
Order of arguments to the compiler (
gcc
org++
) are really important; I recommend:- general warnings and optimization flags (e.g.
-Wall
,-g
to get debug info,-O
,-flto
etc, or-c
to avoid linking , ...) - preprocessor options like
-I
include-dirand-D
defined-symbol(or-H
to understand which headers get included) etc.. - source file[s] to compile like
hello.c
orworld.cc
- if you want to link existing object files
else.o
, add them after the source files - linker options (if relevant), notably
-L
library-dir(and probably-rdynamic
if your program uses plugins with dlopen(3)....) - libraries (like
-lfoo -lbar
from higher-level libraries likelibfoo.so
to lower-level libraries. - output file (i.e. produced executable), e.g.
-o yourexec
.
- general warnings and optimization flags (e.g.
Always correct your source code till you got no warning at all. Trust the compiler's warnings and error messages.
Learn how to use
make
and to write simpleMakefile
-s; see this example.there are other builders, e.g. http://omake.metaprl.org/etc
- Compile your code with the
-g
flag to have the compiler produce debugging information; only when you have debugged your program, ask the compiler to optimize (e.g. with-O1
or-O2
), especially before benchmarking. - Learn how to use
gdb
- Use a version control system like
svn
orgit
(even for a homework assignment). In 2015 I recommend gitoversvn
- Backup your work.
- Learn to use valgrindto hunt memory leaks.
- 总是编译,
-Wall -Wextra
甚至可能 编译-Werror -pedantic-errors
编译器(
gcc
或g++
)的参数顺序非常重要;我建议:- 一般警告和优化标志(例如
-Wall
,-g
获取调试信息-O
,-flto
等,或-c
避免链接,...) - 预处理器选项,如
-I
include-dir和-D
defined-symbol(或-H
了解包含哪些标头)等。 - 要编译的源文件 [s] 像
hello.c
或world.cc
- 如果要链接现有的目标文件
else.o
,请将它们添加到源文件之后 - 链接器选项(如果相关),特别是
-L
library-dir(-rdynamic
如果您的程序使用带有dlopen(3).... 的插件,则可能) - 库(就像
-lfoo -lbar
从高级库libfoo.so
到低级库一样。 - 输出文件(即生成的可执行文件),例如
-o yourexec
.
- 一般警告和优化标志(例如
始终更正您的源代码,直到您完全没有警告为止。相信编译器的警告和错误消息。
学习如何使用
make
和编写简单的Makefile
-s;看这个例子。还有其他构建器,例如http://omake.metaprl.org/等
- 用
-g
标志编译你的代码,让编译器产生调试信息;只有在调试完程序后,才要求编译器进行优化(例如使用-O1
或-O2
),尤其是在进行基准测试之前。 - 了解如何使用
gdb
- 使用像
svn
或这样的版本控制系统git
(即使是家庭作业)。2015 年我推荐gitoversvn
- 备份您的工作。
- 学习使用valgrind来寻找内存泄漏。
NB
NB
The advices above are not specific to Ubuntu 11.10, they could apply to other Linux distributions and other Ubuntu versions.
上述建议并非特定于 Ubuntu 11.10,它们可以适用于其他 Linux 发行版和其他 Ubuntu 版本。
回答by AusCBloke
Boostprovide a whole bunch of libraries that are commonly used and can come in handy. Anyway, I'm not really sure this questions fits in too well on a Q&A board.
Boost提供了一大堆常用的库,可以派上用场。无论如何,我不确定这个问题是否适合在问答板上。
EDIT: As suggested by Basile, Makefile
s and learning to use gdb
are great ideas. There are plenty of neat flags to use with gcc
also, for helping to debug your code, optimize it, produce assembly instructions, etc.
编辑:正如 Basile 所建议的,Makefile
s 和学习使用gdb
是好主意。还有很多整洁的标志可以使用gcc
,用于帮助调试代码、优化代码、生成汇编指令等。
回答by onemasse
QT Creatoris a good IDE, that works well also with simple Makefile based projects. Also, as a C++ programmer you should check out Diaand Dia2Codefor automatic generation of stubs from UML diagrams.
QT Creator是一个很好的 IDE,它也适用于简单的基于 Makefile 的项目。此外,作为 C++ 程序员,您应该查看Dia和Dia2Code以从 UML 图中自动生成存根。
回答by JesperE
Eclipse/CDTruns really well on Ubuntu.
Eclipse/CDT在 Ubuntu 上运行得非常好。
回答by selfsx
On the first steps of programming you should not use IDE because you will better understand what happens backside :) GCC or G++ and stdlib will be sufficient. You also should read about Makefiles, SVN(CVS, GIT), Autotools or CMake to manage your projects. If you want make GUI applications you should learn GTK+ or Qt. If you want real IDE for your needs try Eclipse with C/C++ plugins. Good luck :)
在编程的第一步,你不应该使用 IDE,因为你会更好地理解背后发生的事情:) GCC 或 G++ 和 stdlib 就足够了。您还应该阅读 Makefiles、SVN(CVS、GIT)、Autotools 或 CMake 来管理您的项目。如果你想制作 GUI 应用程序,你应该学习 GTK+ 或 Qt。如果您想要真正的 IDE 来满足您的需求,请尝试使用带有 C/C++ 插件的 Eclipse。祝你好运 :)
回答by ob_dev
If you are familiar with the command line you can use an editor like vim and gcc/g++ to compile your code, learning make svn git is also recommend.
如果你熟悉命令行,你可以使用 vim 和 gcc/g++ 等编辑器来编译你的代码,也推荐学习 make svn git。
In case you are not familiar with the command line or you prefer using the UI :NetBeansis also a good IDE you can use to develop in c/c++ and java.
如果您不熟悉命令行或者更喜欢使用 UI:NetBeans也是一个很好的 IDE,您可以使用它在 c/c++ 和 java 中进行开发。
To install netbeans: open firefox and point to apt://netbeans I hope this will help you.
安装 netbeans:打开 firefox 并指向 apt://netbeans 我希望这会对你有所帮助。
回答by Victor Vasiliev
Some tips besides those which are already mentioned:
除了已经提到的那些提示之外,还有一些提示:
- Valgrindis your friend in finding memory leaks. You may also use
valgrind --tool=callgrind
and KCacheGrind to see where does your program spend time on execution. - If you are going to distribute your program, you should learn autotools or cmake. The first is a classical tool, a bit bloated, the second is more modern.
- Geany is a nice IDE if you are looking for something lightweight. Otherwise, take a look at Code::Blocks, Eclipse/CDT and NetBeans.
- Since I am not sure what you meant by "std libraries", I should mention that besides standard C library, there are a lot of POSIX functions and interfaces, which are common to most *nix-systems, including Mac OS X.
- Valgrind是您查找内存泄漏的朋友。您还可以使用
valgrind --tool=callgrind
和 KCacheGrind 来查看您的程序在何处花费时间执行。 - 如果你打算分发你的程序,你应该学习 autotools 或 cmake。第一个是经典工具,有点臃肿,第二个更现代。
- 如果您正在寻找轻量级的东西,Geany 是一个不错的 IDE。否则,请查看 Code::Blocks、Eclipse/CDT 和 NetBeans。
- 由于我不确定您所说的“std 库”是什么意思,因此我应该提一下,除了标准 C 库之外,还有很多POSIX 函数和接口,它们对于大多数 *nix 系统(包括 Mac OS X)都是通用的。
回答by vitaut
Since you ask more than one question I will answer each separately.
由于您提出的问题不止一个,我将分别回答每个问题。
Do you you have any tips for a new C++ programmer on Ubuntu?
对于 Ubuntu 上的新 C++ 程序员,您有什么建议吗?
Learn some build system such as CMakeor SCons. Although understanding how makeand Makefiles work is useful there is a tendency of moving away from make to more high-level tools which also provide configure-likefunctionality. Make is often used for command-line build, for example with CMake you can generate Makefiles and build your projects using make.
Use a version control system such as gitor Mercurial. I also recommend keeping those your projects you care about on some external service like githubat least for the purposes of backup.
Pay attention to compiler warnings but keep in mind that warnings only catch a fraction of possible errors. A more complete picture can be obtained using static analysis toolsand dynamic analysis tools like Valgrind.
学习一些构建系统,例如CMake或SCons。尽管了解make和 Makefile 的工作原理很有用,但有一种趋势是从 make 转向更高级的工具,这些工具也提供类似配置的功能。Make 通常用于命令行构建,例如使用 CMake,您可以生成 Makefile 并使用 make 构建您的项目。
使用版本控制系统,例如git或Mercurial。我还建议将您关心的项目保留在某些外部服务(例如github)上,至少出于备份目的。
注意编译器警告,但请记住,警告只能捕获一小部分可能的错误。使用静态分析工具和Valgrind等动态分析工具可以获得更完整的图片。
Any libraries I should get from the start?
我应该从一开始就获得任何图书馆?
- You've already got the main one which is called the C++ Standard Library. Make sure that you know what it provides.
- Boostwill cover most of the remaining needs except GUI.
- Gtkmmand Qtare two major C++ GUI frameworks.
A really good IDE I'm missing? (YMMV but I prefer to work in IDE's)
我缺少一个非常好的 IDE?(YMMV 但我更喜欢在 IDE 中工作)
Eclipse - for a long time I've been thinking of it as a Java only IDE, but in fact it is an excellent IDE for almost anything (I even wrote my PhD thesis in it using TeXlipseplugin) and C/C++ support is improving all the time. Also CMake can generate Eclipse CDTproject files.
Qt Creator- another excellent C++ IDE. It is very fast and has native CMake support
Eclipse - 很长一段时间以来,我一直认为它是一个仅限 Java 的 IDE,但实际上它是几乎所有东西的优秀 IDE(我什至使用TeXlipse插件在其中写了我的博士论文)并且 C/C++ 支持正在改进每时每刻。CMake 也可以生成Eclipse CDT项目文件。
Qt Creator- 另一个优秀的 C++ IDE。它非常快并且具有原生的 CMake 支持
Any programming boons or traps I should be aware of from the start?
从一开始我就应该注意任何编程的好处或陷阱?
- From my experience the most common sources of errors in C++ are pointers and resource management in case of exceptions. Make sure you understand and use the RAIIidiom and smart pointers.
- For a more complete list of traps and recommendations see the answers to this question.
回答by wyq10
Library: I recommend you to use Boost. You can find many libraries in it. IDE: Eclipse and QTCreator are good IDEs, but I think it is also very important to use text editor + makefile. Vim, Emacs or Sublime Text is good choice. Always remember to backup your code.
库:我建议您使用 Boost。您可以在其中找到许多库。IDE:Eclipse和QTCreator都是不错的IDE,但是我觉得使用文本编辑器+makefile也很重要。Vim、Emacs 或 Sublime Text 是不错的选择。永远记住备份你的代码。
回答by SuB
I think Netbeans is good. Same UI in Microsoft Windows and Linux. Built-in version Controller and installed Git by default. No extra library added (as oposit of QT)
我认为 Netbeans 很好。Microsoft Windows 和 Linux 中的 UI 相同。内置版本Controller,默认安装Git。没有添加额外的库(作为 QT 的对立面)