Determine if Linux or Windows in C++ Determine if Linux or Windows in C++ windows windows

Determine if Linux or Windows in C++


Use:

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)static const std::string slash="\\";#elsestatic const std::string slash="/";#endif

BTW, you can still safely use this slash "/" on Windows as windows understands this perfectly. So just sticking with "/" slash would solve problems for all OSes even like OpenVMS where path is foo:[bar.bee]test.ext can be represented as /foo/bar/bee/test.ext.


Generally speaking, you'd have do do this with conditional compilation.

That said, if you're using boost::filesystem you should be using the portable generic path format so that you can forget about things like this.


By default, Visual Studio #defines _WIN32 in the preprocessor project settings.

So you can use

// _WIN32 = we're in windows#ifdef _WIN32// Windows#else// Not windows#endif