File naming and general organisation for cross platform development

I am writing a cross platform socket library and will use the pimpl idiom so that users include’ing headers for the library will not need to worry about operating system specific issues.

So, for example a file portable.hpp would be like this:

// forward declare pimpl
struct socketimpl;

class portable

    socketimpl *pimpl_;  // hide OS specific details

But how do I name the implementation files.

I could have portablewin.cpp, portablelinux.cpp etc.

Would I then have specific folders for the different operating systems supported?

A folder structure like this:


Then there would be a standard makefile which would include a helper makefile which would be have the specific files etc for each supported platform.

Then use an OS specific makefile for each OS?

Is that the best plan? Or any other ideas?

Both approaches work. So pick the one you prefer, and then apply it consistently.

Some remarks to help you make your choice:

  • You’ll need different makefiles if you use different toolchains (e.g. VisualStudio on windows, gcc on linux). The POCO library is a good example for that : Look at their readme file in the Building section of their download page. Alternatively, you could use a cross platform make such as CMake.
  • You’d want to use conditional compilation in order not to compile/include OS specific headers. You could then compile all files and tell in the source file which part are to be compiled for which system (controlled by a preprocessor symbol definition in the makefile). On the other side, with the PIMPL you’d also be free to select the file to compile in the makefile.
  • It also depends on the number of platform specific files:
    • With a very few, you could use a platform subdirectory where all the files portablewin.cpp, portablelinux.cpp
    • With several related OS dependent files, it could make sense to have a subdirectory per os (e.g. boost uses this for the implementation of asio; SDL does the same).
  • Use if possible only lowercase filenames and directory names. This will avoid some headache because of OS specific case (in)sensitivity

Make decisions on one place.

You are making both the path to the file and the name of the file aware of the OS. That’s a decision being made in two places.

Be careful of how visible this decision becomes in anything that deals with the files.


Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *