开发者

Organize cross-platform C/C++ project [closed]

Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 11 ye开发者_StackOverflow中文版ars ago.

Improve this question

What is the usual/recommended way how to organize a multi-platform (Windows, Linux) C/C++ library project?

How to name function and provide OS-dependent implementations, organize includes etc.? Please, constructive ideas only -- no pointers as "Look at Linux.", ideas strongly welcome.


A lot depends here depends on the build system you use. Many cross platform build systems have their own way of organizing stuff.

As for code itself, it's not uncommon to provide a header, but link against the implementation file for a given system, so you may have:

system.h
system_linux.cpp
system_windows.cpp
system_osx.cpp
...

...for gathering of OS dependent stuff.

But all in all, the best way is to actually look at some (smaller than Linux) multi-platform projects :P.


I use folders to put the dependent header/source files as this example shows

fs/filesystem.hpp <-- main generic header that is what you include in any project and you can create a #define in there to ensure that a person only includes this one and if they include a sub one below, it will give a compile error pointing to this file as the one to include.

fs/linux/filesystem.hpp <-- header for linux #included with #ifdef LINUX in main file above

fs/windows/filesystem.hpp <-- header for windows #included with #ifdef WINDOWS in main file above

fs/linux/filesystem.cpp <-- implementation for linux

fs/windows/filesystem.cpp <-- implementation for windows

Then in my build system it's easy to use the "$target" to include the relevant source files per os for the build.

The trick is to create a interface that you can typedef and hide from the user so that they only see this and not the implementation details

I personally hate source files littered with multiple OS #ifdefs throughout class definitions, types, headers, etc. and I prefer to fragment everything for the sake of clarity and ease of reading.


CMake is a good meta-buildsystem for many platforms. Boost works on most platforms. Qt is also very nice.

try to keep the number of dependencies low.

CMake let's you ask questions about the platform in a somewhat platform-independent way. Boost and Qt abstract away the platform so you don't have to care(as much).


The approach I use is to wrap all OS-specific functions into my own interface. Then I implement this interface for each different OS (mainly Windows and Linux).

At compile time, I use preprocessor to check which platform it is and then include the appropriate version.

I do this a lot with threads, disk I/O, and clocks/time.

For example:

#ifdef _WIN32

#include <windows.h>
#include "windows_code.h"

#elif __linux

#include <unistd.h>
#include <pthread.h>
#include "linux_code.h"

#else
#error "Unrecognized Platform"
#endif
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜