Linux 上的 C++ 开发 - 我从哪里开始?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/623040/
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++ development on linux - where do I start?
提问by Daniel Sloof
I decided to leave my windows install behind and am now running Debian as my default OS. I have always coded in Windows and specifically with Visual Studio. I am currently trying to get used to compiling my code under linux.
我决定放弃我的 Windows 安装,现在运行 Debian 作为我的默认操作系统。我一直在 Windows 中编码,特别是在 Visual Studio 中。我目前正在尝试习惯在 linux 下编译我的代码。
Although I still have a lot of documentation to read, and don't expect you guys to make it too easy for me, it'd still be nice to get some pointers on where to start. I have some specific questions, but feel free to suggest/recommend anything else regarding the subject.
尽管我仍然有很多文档要阅读,并且不要指望你们让我太容易阅读,但获得一些关于从哪里开始的指示仍然很好。我有一些具体问题,但请随时提出/推荐有关该主题的任何其他内容。
- What are recommended guides on creating a make file, how do I compile from this makefile (do I call g++ myself, do I use 'make'?)
- Looking at other linux software, they almost always seem to have a 'configure' file. What exactly does it do? Does it only check if the required libraries are installed or does it more than just checking requirements?
- How do I link libraries, and how does this relate to my makefile or g++ parameters? In windows I would compile the library, include some header files, tell my linker what additional lib file to link, and copy a dll file. How exactly does this process work in linux?
- Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
- 关于创建 make 文件的推荐指南是什么,我如何从这个 makefile 编译(我自己调用 g++,我是否使用“make”?)
- 看看其他 linux 软件,它们似乎总是有一个“配置”文件。它究竟有什么作用?它是只检查是否安装了所需的库,还是不仅仅是检查要求?
- 我如何链接库,这与我的 makefile 或 g++ 参数有什么关系?在 Windows 中,我会编译库,包含一些头文件,告诉我的链接器要链接哪些额外的 lib 文件,然后复制一个 dll 文件。这个过程在 linux 中究竟是如何工作的?
- 对代码编辑器的建议?我目前正在使用 nano 并且我听说过 vim 和 emacs,但不知道它们对彼此的好处是什么。还有其他的吗,为什么我会考虑它们而不是前三个中的任何一个?注意:我不是在寻找 IDE。
Any help, links to guides & documentation (preferably those that are aimed at beginners) are very much appreciated!
非常感谢任何帮助、指南和文档的链接(最好是针对初学者的)!
采纳答案by MandyK
What are recommended guides on creating a make file, how do I compile from this makefile (do I call g++ myself, do I use 'make'?)
关于创建 make 文件的推荐指南是什么,我如何从这个 makefile 编译(我自己调用 g++,我是否使用“make”?)
You build from the makefile by invoking "make". And inside your makefile, you compile and link using g++ and ld.
您可以通过调用“make”从 makefile 进行构建。在您的 makefile 中,您使用 g++ 和 ld 进行编译和链接。
Looking at other linux software, they almost always seem to have a 'configure' file. What exactly does it do? Does it only check if the required libraries are installed or does it more than just checking requirements?
看看其他 linux 软件,它们似乎总是有一个“配置”文件。它究竟有什么作用?它是只检查是否安装了所需的库,还是不仅仅是检查要求?
It's a script usually used to set up various things based on the environment being used for building. Sometimes it's just a basic shell script, other times it invokes tools like Autoconf to discover what is available when building. The "configure" script is usually also a place for the user to specify various optional things to be built or excluded, like support for experimental features.
它是一个脚本,通常用于根据用于构建的环境设置各种事物。有时它只是一个基本的 shell 脚本,有时它会调用 Autoconf 之类的工具来发现构建时可用的内容。“配置”脚本通常也是用户指定要构建或排除的各种可选内容的地方,例如对实验功能的支持。
How do I link libraries, and how does this relate to my makefile or g++ parameters? In windows I would compile the library, include some header files, tell my linker what additional lib file to link, and copy a dll file. How exactly does this process work in linux?
我如何链接库,这与我的 makefile 或 g++ 参数有什么关系?在 Windows 中,我会编译库,包含一些头文件,告诉我的链接器要链接哪些额外的 lib 文件,然后复制一个 dll 文件。这个过程在 linux 中究竟是如何工作的?
ld is the GNU linker. You can invoke it separately (which is what most makefiles will end up doing), or you can have g++ delegate to it. The options you pass to g++ and ld determine where to look for included headers, libraries to link, and how to output the result.
ld 是 GNU 链接器。您可以单独调用它(大多数 makefile 最终会这样做),或者您可以将 g++ 委托给它。您传递给 g++ 和 ld 的选项确定在何处查找包含的头文件、要链接的库以及如何输出结果。
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
对代码编辑器的建议?我目前正在使用 nano 并且我听说过 vim 和 emacs,但不知道它们对彼此的好处是什么。还有其他的吗,为什么我会考虑它们而不是前三个中的任何一个?注意:我不是在寻找 IDE。
Vim and Emacs are very flexible editors that support a whole bunch of different usages. Use whatever feels best to you, though I'd suggest you might want a few minimal things like syntax highlighting.
Vim 和 Emacs 是非常灵活的编辑器,支持大量不同的用法。使用任何你觉得最好的东西,尽管我建议你可能需要一些最小的东西,比如语法高亮。
回答by Brian Gianforcaro
Just a note to go with MandyK's answers.
只是与 MandyK 的答案一起使用的注释。
Creating make files by hand is usually a very unportable way of building across linux distro's/unix variants. There are many build systems for auto generating make files, building without make files. GNU Autotools, Cmake, Scons, jam, etc.
手动创建 make 文件通常是一种非常不可移植的跨 linux 发行版/unix 变体的构建方式。有许多构建系统可以自动生成 make 文件,无需 make 文件即可构建。GNU Autotools、Cmake、Scons、jam等。
Also to go more in depth about configure.
还要更深入地了解配置。
- Checks available compilers, libraries, system architecture.
- Makes sure your system matches the appropriate compatible package list.
- Lets you specify command line arguments to specialize your build, install path, option packages etc.
- Configure then generates an appropriate Makefile specific to your system.
- 检查可用的编译器、库、系统架构。
- 确保您的系统匹配适当的兼容包列表。
- 允许您指定命令行参数来专门化您的构建、安装路径、选项包等。
- 然后配置生成特定于您的系统的适当 Makefile。
回答by sebnow
What are recommended guides on creating a make file, how do I compile from this makefile (do I call g++ myself, do I use 'make'?)
关于创建 make 文件的推荐指南是什么,我如何从这个 makefile 编译(我自己调用 g++,我是否使用“make”?)
I learned how to write makefiles by reading the GNU Make manual.
我通过阅读GNU Make 手册学会了如何编写 makefile 。
Looking at other linux software, they almost always seem to have a 'configure' file. What exactly does it do? Does it only check if the required libraries are installed or does it more than just checking requirements?
看看其他 linux 软件,它们似乎总是有一个“配置”文件。它究竟有什么作用?它是只检查是否安装了所需的库,还是不仅仅是检查要求?
The configure file is usually associated with autotools. As the name of the script suggests, it allows you to configure the software. From the perspective of the developer this mostly means setting macros, which determine variables, which libraries are available, and such. It also tests for the availability of libraries. In the end the script generates a GNU Makefile, which you can then use to actually build and install the software.
配置文件通常与autotools相关联。正如脚本名称所暗示的那样,它允许您配置软件。从开发人员的角度来看,这主要意味着设置宏、确定变量、可用的库等。它还测试库的可用性。最后,脚本会生成一个 GNU Makefile,然后您可以使用它来实际构建和安装软件。
The GNU build system is only one of many. I don't particularly like the GNU build system as it tends to be slower than others, and generates an ugly Makefile. Some of the more popular ones are CMake, Jam (Boost Jammight be of interest for C++) and waf. Some build systems simply generate Makefiles, while others provide a completely new build system. For simple projects writing a Makefile by hand would be easy, but "dependency checking" (for libraries, etc) would also have to be done manually.
GNU 构建系统只是众多构建系统中的一个。我不是特别喜欢 GNU 构建系统,因为它往往比其他系统慢,并且会生成一个丑陋的 Makefile。一些比较流行的是CMake、 Jam (Boost Jam可能对 C++ 感兴趣)和waf。一些构建系统只是生成 Makefile,而另一些构建系统则提供全新的构建系统。对于简单的项目,手动编写 Makefile 会很容易,但“依赖项检查”(对于库等)也必须手动完成。
Edit: Brian Gianforcaroalso pointed this out.
编辑:Brian Gianforcaro也指出了这一点。
回答by David Cournapeau
Your question is a bit too general, but here is what I would recomment:
你的问题有点太笼统了,但这是我的建议:
Editor: vim and emacs are popular. What matters the most, as with most tools, is to master one. I like using vim because vi (its descendant) is available everywhere, but that may not be very relevant, specially if you stay on Linux. Any programming editor is fine.
configure: unless you do big projects, don't bother with it. It is a nightmare to use and debug. It only makes sense if you intend to distribute your project - in that case, read the autobook: http://sources.redhat.com/autobook/. As other said, there are alternatives (cmake, scons, etc...). I am quite familiar with both scons and autotools, but I still use make for small (couple of files) projects.
编辑:vim和emacs很流行。与大多数工具一样,最重要的是掌握一个。我喜欢使用 vim,因为 vi(它的后代)随处可用,但这可能不是很重要,特别是如果您继续使用 Linux。任何编程编辑器都可以。
配置:除非你做大项目,否则不要理会它。使用和调试是一场噩梦。只有在您打算分发您的项目时才有意义 - 在这种情况下,请阅读自动书:http: //sources.redhat.com/autobook/。正如其他人所说,有替代品(cmake、scons 等...)。我对 scons 和 autotools 都很熟悉,但我仍然将 make 用于小型(几个文件)项目。
Concerning shared library: it is almost as windows, except that you link against the shared library directly - there is no .lib vs .dll distinction in Linux. For example. for one library foo with a function foo:
关于共享库:它几乎与 Windows 一样,只是您直接链接到共享库 - Linux 中没有 .lib 与 .dll 区别。例如。对于一个带有函数 foo 的库 foo:
int foo(void)
{
return 1;
}
You would build it as follows:
您将按如下方式构建它:
gcc -fPIC -c foo.c -o foo.o
gcc -shared foo.o -o libfoo.so
A main (of course in real life you put the API in a header file):
一个主要的(当然在现实生活中你把 API 放在一个头文件中):
int foo(void);
int main(void)
{
foo();
return 0;
}
And then, you link it as:
然后,您将其链接为:
gcc -c main.c -o main.o
gcc main.o -o main -L. -lfoo
The -L. is here to say that you want the linker to look in the current directory (contrary to windows, this is never done by default in Linux), the -lfoo says to link against the library foo.
-L。在这里是说您希望链接器查看当前目录(与 Windows 不同,Linux 中默认情况下从未这样做),-lfoo 表示链接库 foo。
回答by gnomed
So to get you started I will first point you to this guidefor makefiles, it also covers some linking stuff too.
It's just a little something my university Computer Science prof gave us I found it to be very clear and concise, very helpful.
所以为了让你开始,我首先会向你指出这个makefiles指南,它也涵盖了一些链接的东西。
这只是我的大学计算机科学教授给我们的一些东西,我发现它非常清晰简洁,非常有帮助。
And as for an IDE, I use eclipseusually because it handles the makefile as well. Not to mention compile and standard output are right at your fingertips in the program.
It was mainly intended for Java developing, but there is a C/C++ plugin too!
至于 IDE,我通常使用eclipse,因为它也处理 makefile。更不用说编译和标准输出在程序中触手可及。
它主要用于Java开发,但也有一个C/C++插件!
回答by thekidder
Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
对代码编辑器的建议?我目前正在使用 nano 并且我听说过 vim 和 emacs,但不知道它们对彼此的好处是什么。还有其他的吗,为什么我会考虑它们而不是前三个中的任何一个?注意:我不是在寻找 IDE。
Vi and Emacs are the two quintessential Unix editors; if you are set on using a text editor rather than an IDE, one of them or their derivatives (vim, xemacs, etc) is the way to go. Both support syntax highlighting and all sorts of features, either by default or via extensions. The best part about these editors is the extensibility they offer; emacs via a variety of lisp, and vim via its own scripting language.
Vi 和 Emacs 是两个典型的 Unix 编辑器;如果您打算使用文本编辑器而不是 IDE,则可以选择其中之一或其衍生产品(vim、xemacs 等)。两者都支持语法高亮和各种功能,默认情况下或通过扩展。这些编辑器最好的部分是它们提供的可扩展性;emacs 通过各种 lisp,vim 通过自己的脚本语言。
I personally use Emacs, so I can't say much about Vim, but you should be able to find lots of information about both online. Emacs has several good tutorials and references, including this one.
我个人使用 Emacs,所以我不能对 Vim 说太多,但是你应该可以在网上找到很多关于这两者的信息。Emacs 有几个很好的教程和参考资料,包括这个。
EDIT [Dec 2014]: There seems to be a trend of cross-platform and highly extendable editors recently. This could be a good choice if you'd like something less than an IDE, but more graphical than vi/emacs and native-feeling across multiple platforms. I recommend looking at Sublimeor Atom; both of these work across Windows/Linux/Mac and have great communities of plugins and themes.
编辑 [2014 年 12 月]:最近似乎有一种跨平台和高度可扩展的编辑器的趋势。如果您想要的不是 IDE,而是比 vi/emacs 更图形化和跨多个平台的原生感觉,那么这可能是一个不错的选择。我建议查看Sublime或Atom;这两者都适用于 Windows/Linux/Mac,并且拥有很棒的插件和主题社区。
回答by Steve Rowe
I recommend the book The Art of Unix Programmingby ESR. It covers choice of editor, programming language, etc. It also gives a good sense for the mindset behind programming on Unix or Linux.
我推荐ESR 的The Art of Unix Programming这本书。它涵盖了编辑器、编程语言等的选择。它还很好地了解了在 Unix 或 Linux 上编程背后的思维方式。
For editors, you probably want either Vim or Emacs. They are both different and which one is better is more about personal taste than anything else. I use Vim. It is great for quickly moving around the code and making changes. I didn't like Emacs as much but many people do. Emacs is extremely extensible and can be used for everything from a news reader to an ide. Try both and see what you like.
对于编辑器,您可能需要 Vim 或 Emacs。它们都是不同的,哪个更好更关乎个人品味。我使用 Vim。它非常适合快速移动代码并进行更改。我不太喜欢 Emacs,但很多人喜欢。Emacs 具有极强的可扩展性,可用于从新闻阅读器到 ide 的所有内容。尝试两者,看看你喜欢什么。
回答by David Z
- Recommendations for code editors? I am currently using nano and I've heard of vim and emacs, but don't know what the benefits of them are over eachother. Are there any others, and why would I consider them over any of the previous three? Note: I am not looking for an IDE.
- 对代码编辑器的建议?我目前正在使用 nano 并且我听说过 vim 和 emacs,但不知道它们对彼此的好处是什么。还有其他的吗,为什么我会考虑它们而不是前三个中的任何一个?注意:我不是在寻找 IDE。
If you're using Linux with a window manager (KDE, Gnome, etc.) you could also consider using the standard text editor for your window manager. The main benefit it would have over vim/emacs/nano is that it would seem more familiar to one coming from a Windows environment - an editor written to run on the window manager has a menu bar, file open/save dialogs, undo/redo, and plenty of other neat features that console editors probably can't match. (Though emacs and vim are pretty sophisticated these days, so who knows ;-P)
如果您使用带有窗口管理器(KDE、Gnome 等)的 Linux,您还可以考虑为您的窗口管理器使用标准文本编辑器。与 vim/emacs/nano 相比,它的主要好处是,对于来自 Windows 环境的人来说,它看起来更熟悉 - 编写在窗口管理器上运行的编辑器具有菜单栏、文件打开/保存对话框、撤消/重做,以及许多其他控制台编辑器可能无法比拟的简洁功能。(虽然现在 emacs 和 vim 非常复杂,所以谁知道;-P)
On KDE (which is what I use) I can recommend KWrite, which is a well-featured but fairly basic text editor with syntax highlighting; or Kate, which is a fancier text editor with some extra features: session management, a builtin terminal panel, automatic invocation of make
, and several plugins including a C/C++ symbol viewer. I usually use Kate for my C++ work when I don't want to bother with setting up a full IDE project. (FYI the IDE for KDE is KDevelop)
在 KDE(这是我使用的)上,我可以推荐 KWrite,这是一个功能齐全但相当基本的文本编辑器,具有语法突出显示功能;或者 Kate,这是一个更高级的文本编辑器,具有一些额外的功能:会话管理、内置终端面板、自动调用make
以及包括 C/C++ 符号查看器在内的几个插件。当我不想费心设置完整的 IDE 项目时,我通常使用 Kate 来完成我的 C++ 工作。(仅供参考,KDE 的 IDE 是 KDevelop)
回答by SingleNegationElimination
the space between invoking g++ directly and using an autotools build chain is pretty narrow. Get good at autotools, which is really the closest thing to a 'project' available in the Linux/Open Source world.
直接调用 g++ 和使用 autotools 构建链之间的空间非常狭窄。擅长自动工具,这真的是最接近 Linux/开源世界中可用的“项目”的东西。
回答by rep_movsd
For someone coming from Visual Studio, all this commandline stuff might seem arcane and messy. Before you turn into a bash shell/vim/emacs junkie, try a few GUI based tools first so you have some transition time...
对于来自 Visual Studio 的人来说,所有这些命令行的东西可能看起来神秘而凌乱。在你变成一个 bash shell/vim/emacs 瘾君子之前,先尝试一些基于 GUI 的工具,这样你就有一些过渡时间......
- QT 4.5 with its QT Creator mini-IDE. This is the best framework, lightyears ahead of the competition.
- Eclipse (C++) - From my experience with this on Windows, I find it's astounding ( This is probably the best Java application ever written )
- KDevelop
- Anjuta
- If you use Delphi, Lazarus/FreePascal is a good alternative.
- QT 4.5 及其 QT Creator 迷你 IDE。这是最好的框架,领先竞争对手光年。
- Eclipse (C++) - 根据我在 Windows 上的经验,我发现它令人震惊(这可能是有史以来最好的 Java 应用程序)
- KDevelop
- 安茹塔
- 如果您使用 Delphi,Lazarus/FreePascal 是一个不错的选择。
I'm sure the longhairs will scoff and claim that vim or emacs gives them the best and fastest development environment, but different strokes for different folks. Someone accustomed to an IDE will take some time to switch or may not wish to switch at all. For all their editing prowess, creating GUI apps is certainly not a job for 80x25 tools. It takes years to become an expert with the command line side of things, its more of a transformation of worldview than anything else.
我敢肯定,长毛人会嗤之以鼻,声称 vim 或 emacs 为他们提供了最好和最快的开发环境,但对不同的人来说却有不同的招数。习惯使用 IDE 的人需要一些时间来切换,或者可能根本不想切换。对于他们所有的编辑能力,创建 GUI 应用程序肯定不是 80x25 工具的工作。成为命令行方面的专家需要数年时间,这更像是世界观的转变。