How to develop a cross-platform C++ project?

2020-06-18 09:58发布

I'm a C++ beginner and I'm starting to develop my first cross-platform C++ project. I need to use platform-specific calls (Win32 and POSIX) so I need to compile frequently both in Windows and Linux.

Whit single-platform projects I'm using, until now, KDevelop in Linux and Visual Studio 2012 in Windows.

How can I use two different IDEs in two different Operating Systems with the same project?

  1. Should I use a single, cross-platform, IDE?
  2. Should I learn CMake (or similar) and configure it to work with both IDEs?
  3. Could/Should I host my code in the web and sync automatically with offline projects?
  4. Alternatives?

Thanks in advance to everyone.

EDIT:

Just for clarification, the project will be a simple server for a scholastic protocol. There will be a client asking for upload/retrieve some files to/from the server. With scholastic I mean that, for example, I have to use pthreads/win32 threads instead of an higher level C++ threads library.

5条回答
小情绪 Triste *
2楼-- · 2020-06-18 10:30
  1. it will help, you will quite likely need to debug on many platforms... Qt Creator, Netbeans and Eclipse come to mind.

  2. Yes. cmake, or qmake for Qt maybe

  3. Not technical question. Just use version control! github and gitorious are easy choices for open source project though.

  4. Qt is a no-brainer choice for cross-platform C++ GUI app, and also decent choice for network app with no GUI.

查看更多
何必那么认真
3楼-- · 2020-06-18 10:31
  1. Maybe - really depends on what you feel most comfortable with. As the project is non-graphical, all the IDE gives you is editing of files and compilation. So you can build the project on one machine with the IDE there, and then move the sources to another machine for compiling there.

  2. I personally would just have two makefiles, one for Linux and one for Widnows. Makes life fairly simple [you could have a "outer" makefile that picks the right one based on some clever method].

  3. Yes, you should find a version control system that works for both Windows and Linux (git, mercurial, subversion, bazaar and several others). That way, not only do you have a central repository [you can use either of your machines as "server" for any of these], but it also allows you to keep track of your changes. Definitely worthwile doing!

  4. There are hundreds of different alternatives. But the simpler you keep it, and the less complicated your tools are, the more time you get to spend on actually programming your project, rather than, for example, figure out why CMake isn't building the way you want it to.

Also, make sure that you separate out all your system-specific code to one file per architecture. That way, it's easy to port to another architecture later, and it makes MOST of your code compile on both systems.

查看更多
够拽才男人
4楼-- · 2020-06-18 10:32

My recent experience suggest to take a look at Qt. The IDE (QtCreator) it's very good, and available on all major platforms.

I've used for a fairly simple project, that uses fairly complex components, like OpenCV and ZBar. I develop on Linux, copy the source to Windows, and recompile.

I had some trouble to setup OpenCV on both platforms, so I can't say it's super easy, but it's working. Since you already know KDevelop, you should already know Qt.

I also put much value in recent trend that see Qt5 as the platform for Ubuntu on smartphones. I really hope to see this developing.

HTH

查看更多
唯我独甜
5楼-- · 2020-06-18 10:33

How can I use two different IDEs in two different Operating Systems with the same project?

Should I use a single, cross-platform, IDE?

No. I think this is a case of asking the wrong question. To make a cross-platform project, what matters is your build scripts and the system-neutral nature of your code. Sometimes it might help to have project files for your preferred IDE, but maintaining multiple project files for multiple IDEs will only make things more difficult and complex for you. Instead, you should focus on finding a build system that minimizes the amount of time you spend on project maintenance.

For that, CMake and PreMake seem to be two of the best tools to make that happen.

There are dozens of alternatives (like SCons, Cook, kbuild, Jam and Boost Jam, and many others), but since CMake and PreMake both generate project files and build scripts, they might be the best solutions.

Your mileage will vary.

Could/Should I host my code in the web and sync automatically with offline projects?

You should have robust source control that works everywhere you do. Git and Mercurial seem to work best if you use some kind of "cloud" hosting like Github or BitBucket, but they by no means require it. Depending on your work environment and team size, you may prefer Subversion or PerForce or something else, but that's up to you and your team.

查看更多
我欲成王,谁敢阻挡
6楼-- · 2020-06-18 10:37
  1. Typically, it's easy to adjust the IDE-specific project/build files to added/moved/deleted source files. Therefore, using a cross-platform IDE isn't that important.
  2. You can do that, I think that CMake can also create project files for some IDEs that can then be used to build the project.
  3. Ahem, if you want to host it online or not is your choice. What you should definitely do is to use some kind of version control. A bug-tracking system is also helpful. If you want to open-source the code anyway, using one of the existing hosting facilities is a clear yes.
  4. Not really.

One comment though: You will have much more trouble making the C++ code portable. Building on top of a toolkit like Qt is a tremendous help. If you want to stay closer to standard C++, at least consider using Boost for stuff like threads, smart pointers, filesystem access. Good luck!

查看更多
登录 后发表回答