Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 753
eang Avatar asked Jan 20 '13 18:01

eang


2 Answers

  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.

like image 106
Mats Petersson Avatar answered Sep 29 '22 19:09

Mats Petersson


  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!

like image 39
Ulrich Eckhardt Avatar answered Sep 29 '22 20:09

Ulrich Eckhardt