0

I want to include dirent.h in my visual studio project. I wish to have compatibility both with unix and windows. That is why I am considering using https://github.com/tronkko/dirent . In that project, the documentation says:

If you wish to distribute dirent.h alongside with your own source code, then copy include/dirent.h file to a new sub-directory within your project and add that directory to include path on Windows while omitting the directory under Linux/UNIX. This allows your project to be compiled against native dirent.h on Linux/UNIX while substituting the functionality on Microsoft Windows.

which are the steps to do that in visual studio? (I am using 2017 version but I suppose that it will be similar to older versions)

roalz
  • 2,699
  • 3
  • 25
  • 42
Learning from masters
  • 2,032
  • 3
  • 29
  • 42
  • If you're using Visual Studio but not any other build system, your code can't be automatically compiled on UNIX at all. A decent recommendation would be CMake, but of course please do your own researches and do not ask opinion based questions on Stack Overflow. – Tatsuyuki Ishi Mar 16 '17 at 11:03
  • Use the properties sheet for the project. – stark Mar 16 '17 at 11:09
  • @TatsuyukiIshi Visual Studio 2017 now supports compiling C++ projects for Linux. – roalz Mar 16 '17 at 11:17
  • 1
    Just use `#include `. That's _way_ easier than `dirent`, although it does require C++17. – MSalters Mar 16 '17 at 14:43

1 Answers1

2

A classical solution used to differentiate #includes is to use preprocessor directives such as #if or #ifdef.

For example, you can use:

#ifdef _MSC_VER
#include "stuff specific to Microsoft Visual Studio"
#else
#include "stuff not specific for Microsoft Visual Studio"
#endif

in your case, you may want something like:

#ifdef _MSC_VER
#include "msvc/dirent.h"
#else
#include <dirent.h>
#endif
roalz
  • 2,699
  • 3
  • 25
  • 42