简体   繁体   English

Linux平台下的C ++编程

[英]C++ Programming in Linux Platform

I am a software engineer and i work in VC++, C++ in WIndows OS. 我是一名软件工程师,我在WIndows OS中使用VC ++,C ++。

Are there any major differences when it comes to coding in C++ in Linux environment. 在Linux环境中使用C ++进行编码时是否存在重大差异?

Or is it just some adjustments that we have to make when we need to code in C++ in Linux. 或者,当我们需要在Linux中使用C ++进行编码时,我们必须进行一些调整。

It would depend on the types of projects you've worked on and what native windows APIs you made use of. 这取决于您所使用的项目类型以及您使用的本机Windows API。 For example if you used the native Windows API for everything, you're going to have a pretty big task ahead of you, it'd be worth making your project(s) work nicely with Wine instead. 例如,如果您使用本机Windows API进行所有操作,那么您将面临一项非常重要的任务,那么值得让您的项目与Wine完美配合。

In the Linux environment you have the man pages, quite detailed documentation of almost everything :). 在Linux环境中,您有手册页,几乎所有内容的详细文档:)。 As mentioned above, look at POSIX, and while I recommend Qt - it provides a LOT of abstractions for things you might want to learn to do the Linux way (eg sockets, filesystem...) 如上所述,看看POSIX,虽然我推荐Qt - 它为你可能想要学习的Linux方式提供了大量的抽象(例如套接字,文件系统...)

  1. Use the POSIX API instead of the Win32 API. 使用POSIX API而不是Win32 API。
  2. Use gtkmm, Qt, or wxWidgets instead of MFC. 使用gtkmm,Qt或wxWidgets而不是MFC。

Linux programming world is very different from you are familiar with in Windows world. Linux编程世界与Windows世界中熟悉的非常不同。 You have to understand it and get used to it. 你必须了解它并习惯它。 Once you understand you will not want to come back. 一旦你明白你就不想回来了。

  1. You have many small/good tools that works with each other rather then all-in-one MSVC solution. 你有许多小/好的工具可以互相协作,而不是一体化的MSVC解决方案。 For example: 例如:

    In Linux you have a compiler as stand-alone tool (Gnu compiler collection), you have build system as stand-alone tool (autotools, CMake). 在Linux中,您有一个编译器作为独立工具(Gnu编译器集合),您将构建系统作为独立工具(autotools,CMake)。 You have GNU Debugger as stand alone tool and you have very good editors as stand alone tool (like hard core vim/emacs). 你有GNU Debugger作为独立工具,你有很好的编辑器作为独立的工具(如硬核vim / emacs)。

    There are integrated development environments like Eclipse, Netbeans, KDevelop, Anjuta but still you have to understand how stuff works. 有Eclipse,Netbeans,KDevelop,Anjuta等集成开发环境,但你仍然需要了解其中的工作原理。

    You should understand that each separate tool is very powerful and integrates with others. 您应该了解每个单独的工具都非常强大并且与其他工具集成。

  2. OS Level API is designed for simplicity. OS Level API旨在简化。 You'll rarely will find calls like CreateProcessEx with bizzilion parameters rather you have simple fork() + exec() . 您很少会使用bizzilion参数找到CreateProcessEx之类的调用而不是简单的fork() + exec() man is you real friend in all connected to system API and standard C library. man是你所有连接到系统API和标准C库的真正朋友。

  3. GUI - You have two big GUI libraries Qt/GTK. GUI - 你有两个大的GUI库Qt / GTK。 Qt is great C++ library that makes GUI development enjoyable work (unlike MFC). Qt是一个很棒的C ++库,它使GUI开发变得令人愉快(与MFC不同)。 GTK has both C and C++ APIs GTK and GTKmm (no experience with them). GTK有C和C ++ API GTK和GTKmm(没有经验)。

  4. i18n/l10n/unicode - this is where Linux programming makes life easier. i18n / l10n / unicode - 这是Linux编程让生活更轻松的地方。 Almost everything is UTF-8. 几乎所有东西都是UTF-8。 No wide API crap, no issues with opening Chinese file names with simple fopen or ifstream, no 3rd part library that can't open file with Unicode name. 没有广泛的API废话,使用简单的fopen或ifstream打开中文文件名没有问题,没有第三部分库无法打开带有Unicode名称的文件。 Great built in tools available like gettext, and good translation toolkits like KBabel. 很好的内置工具,如gettext,以及像KBabel这样的好翻译工具包。

  5. Libraries - this is where Linux programming makes you hate Windows. 库 - 这是Linux编程让你讨厌Windows的地方。 Almost every single free library is already installed or available with simple apt-get or yum install . 几乎所有的免费库都已安装或通过简单的apt-getyum install no debug/release incompatibility crap, no DLL_EXPORT-ing, simple robust, making shared objects is as simple as working with static libraries (and most do not use static libraries at all). 没有调试/发布不兼容的废话,没有DLL_EXPORT,简单健壮,使共享对象就像使用静态库一样简单(并且大多数都不使用静态库)。

My $0.02 我的0.02美元

(I'm Linux programmer that have deal a lot with windows development)... (我是Linux程序员,与windows开发有很多交易)...

It depends on how many windows-specific things you've been using. 这取决于你一直在使用多少个特定于Windows的东西。 The standard part of C++ is the same, but using that will not get you much further than command-line applications. C ++的标准部分是相同的,但使用它不会比命令行应用程序更进一步。

There's also the whole makefile-instead-of-letting-VS-build-for-you thing. 还有整个makefile-instead-letting-VS-build-for-you。 Depending on what tool (or IDE) you decide to use in Linux, that could be a big difference. 根据您决定在Linux中使用的工具(或IDE),这可能是一个很大的不同。

I have worked quite a bit on both platforms and like them both, but in general I found most developers to like one and hate the other. 我在两个平台上都做了很多工作,并且都喜欢它们,但总的来说,我发现大多数开发人员喜欢一个并且讨厌另一个。

I would describe *nix environment as "geek friendly": many excellent and very flexible tools on your disposal. 我会将* nix环境描述为“极客友好”:您可以使用的许多优秀且非常灵活的工具。 Some of them introduce hard learning curve, and some are simply broken but still popular for some reason (make) but if you are willing to invest some time in properly learning them, the reward is high. 他们中的一些人引入了艰难的学习曲线,有些人只是因为某种原因(制作)而被打破但仍然很受欢迎,但如果你愿意投入一些时间来正确学习它们,那么奖励就会很高。 In fact, I use many *nix tools even when working on Windows: vim, grep, perl, etc... 事实上,即使在Windows上工作,我也使用了许多* nix工具:vim,grep,perl等...

On the other hand, Windows platform offers Win32 API which has way more functionality than POSIX, is very well documented and supported by very good tools. 另一方面,Windows平台提供的Win32 API具有比POSIX更多的功能,非常好的文档和非常好的工具支持。 Debuggers on Windows (especially windbg) are generally more powerful that any *nix debugger I have tried, although gdb is generally good enough for most tasks. Windows上的调试器(特别是windbg)通常比我尝试过的任何* nix调试器都更强大,尽管gdb对于大多数任务来说通常都足够好。 Deployment of executables is also easier than in Linux world - in fact the only truly reliable way to deploy software on Linux is to ship source code and build it on clients' machines via config/make. 可执行文件的部署也比Linux世界更容易 - 事实上,在Linux上部署软件的唯一真正可靠的方法是发送源代码并通过config / make在客户机上构建它。

我建议使用像SCons这样的Buildsystem,它在Linux和Win32上运行良好。

Take a look at the source to some open-source project that runs on both Linux and Windows. 看一下在Linux和Windows上运行的一些开源项目的源代码。 Typically, over 80% of the code is identical, and the bigger the project the less the system-specific part tends to be. 通常,超过80%的代码是相同的,项目越大,系统特定部分往往越少。 Unfortunately, there can be hard parts (threading, non-blocking network IO, GUI details) in the system-specific code. 不幸的是,系统特定的代码中可能存在硬件(线程,非阻塞网络IO,GUI细节)。

There are some major differences that I can think of: 我可以想到一些主要的差异:

  • Tools. 工具。 Good and bad points. 好与坏点。 If you are used to Visual Studio, there is nothing quite like that available. 如果你已经习惯了Visual Studio,那么就没有什么比这更好的了。 Each Linux IDE has some issues. 每个Linux IDE都存在一些问题。 On the other hand, especially debugging tools are very good. 另一方面,特别是调试工具非常好。 But all in all, you are supposed to create your own working environment from what's available. 但总而言之,您应该根据可用的方式创建自己的工作环境。
  • API's. 蜜蜂。 Documentation varies wildly. 文档差异很大。 Some components are well documented, but often you end up reading the source code to figure out how something is supposed to work. 有些组件已有详细记录,但通常最终会阅读源代码以确定某些内容应该如何工作。 On the other hand, you have source code so eventually you have all the tools possible to figure out why something doesn't work. 在另一方面,你源代码,这样最终你拥有所有的工具可能要弄清楚为什么有些东西不能工作。
  • The Linux programming community is usually very good as long as you remember to behave and you find the right places. Linux编程社区通常非常好,只要你记得行为并找到合适的地方。 SO isn't half bad in some issues, but sometimes you need to find other places. 在某些问题上,SO并不是坏事,但有时你需要找到其他地方。
  • Things are not quite as automatic as you might have learned in the Windows world. 事情并不像你在Windows世界中学到的那样自动。 Yes, some tools allow you to create projects without Makefile knowledge, but really, you should learn how to use them. 是的,有些工具允许您创建没有Makefile知识的项目,但实际上,您应该学习如何使用它们。 In Windows it's much more common that you never edit the project files (eg Makefiles) by hand. 在Windows中,您永远不会手动编辑项目文件(例如Makefile)。
  • If you want to work in kernel space (drivers etc) C is a better bet than C++ since the kernel is written with that. 如果你想在内核空间(驱动程序等)中工作,那么C是比C ++更好的选择,因为内核是用它编写的。
  • And I agree with people suggesting Qt. 我同意人们建议Qt。 Very nice widget set. 非常好的小部件集。 Beats at least Swing (yes, I know, it's Java) hands down. 至少击败Swing(是的,我知道,这是Java)。 And Qt Creator isn't half bad. 而Qt Creator也不差一半。
  • Don't underestimate the power of shell scripting! 不要低估shell脚本的威力! Something very few Windows programmers have figured out, but you can do a hell of a lot with them to help your work. 很少有Windows程序员想到了,但你可以用它们做很多事情来帮助你的工作。

A typical windows programmer who is used to Visual C++ might find the following aspects of Linux C++ programming novel, or difficult: 一个习惯于Visual C ++的典型Windows程序员可能会发现Linux C ++编程的以下几个方面是新颖的,或者很难:

  1. Linux programming isn't linux programming, it's Unix programming. Linux编程不是linux编程,而是Unix编程。 Unix programming's roots go back a lot further than the MS-DOS roots of Windows, and it shows in a lot of places. Unix编程的根源可以追溯到Windows的MS-DOS根本,它在许多地方都有所体现。

  2. Windows programmers tend to think about the environment, they tend to think about the IDE tools (your GUI editor, compiler, debugger) first. Windows程序员倾向于考虑环境,他们倾向于首先考虑IDE工具(您的GUI编辑器,编译器,调试器)。 Unix programmers tend to be arranged in various tribes, many core Unix (linux) C++ programmers are very comfortable working from the command line without an IDE, and some, I'm sure, use visual-studio style IDEs on Linux, of which there are many. Unix程序员往往被安排在不同的部落中,许多核心Unix(Linux)C ++程序员在没有IDE的情况下从命令行工作非常舒服,而且我确定在Linux上使用visual-studio风格的IDE,其中有一些很多。

  3. I personally found I had to learn how to read (and maybe write) a makefile, build a bunch of standard Linux/Unix applications from source (and understand how to type my way through steps like 'autoconfiguration' and the various "--command-line-options" one might select there), before I get the feel, and the flavour of the environment. 我个人发现我必须学习如何阅读(也许写)一个makefile,从源代码构建一堆标准的Linux / Unix应用程序(并了解如何通过'autoconfiguration'和各种各样的步骤输入我的方式 - 命令-line-options“人们可以选择那里”,然后才能感受到环境的味道。

  4. Until you are a seasoned Linux system administrator you might want to stick with the newbie-friendly Linux distributions (like Ubuntu). 在您成为经验丰富的Linux系统管理员之前,您可能希望坚持使用新手友好的Linux发行版(如Ubuntu)。

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

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