1

I am using Visual Studio 2019 (updated to date, running on Windows) to rewrite old program code in cross-platform C++ language (Windows & Linux).

In the code, I am using the pre-compilation directives #if, #else, #endif to toggle platform-specific blocks of code.

Example:

76 #ifdef WINDOWS_OS
77         errno_t success = fopen_s(&arq, logConfigFileName.str().c_str(), "rt");
78 #else
79         arq = fopen(logConfigFileName.str().c_str(), "rt");
80 #endif

In the Visual Studio editor, everything looks correct (see screenshot). Project selected: Windows, Debug, x86. Lines 1 and 2 appear normally (active and without error indication) and lines 3 to 5 appear in gray indicating they are disabled, that is, it appears as it should be.

However, when compiling I get the error:

Error C4996 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. 

error on line 79, line that should be ignored by the compiler.

How can I fix this, so that my code can be compiled for both platforms without problems ?

110f716c
  • 100
  • 1
  • 2
  • 8
  • 4
    Why do you think `WINDOWS_OS` would be defined? Did you define it yourself? – ChrisMM Aug 25 '21 at 20:30
  • I would enter `xyzzy;` (garbage) in that #else section to see if that code is TRULY being ignored by your compiler (other than for the issue you mentioned). If you get an error message for THAT, then your compiler directive settings are not what you (or the IDE) think they are. – franji1 Aug 25 '21 at 20:31
  • 2
    Does this answer your question? [How do I check OS with a preprocessor directive?](https://stackoverflow.com/questions/142508/how-do-i-check-os-with-a-preprocessor-directive) – 110f716c Aug 25 '21 at 20:47
  • Please show a [mre] – Alan Birtles Aug 26 '21 at 06:33

2 Answers2

1

You don't need to create / define macros to identify the OS.

Compiler do it already. See C++ compiling on Windows and Linux: ifdef switch for the list.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
-1

Problem solved. The problem was in the definition of the WINDOWS_OS flag, I had defined it in the code with the #define WINDOWS_OS statement.

The IDE recognized the instruction, but the preprocessor didn't.

The solution was to include the WINDOWS_OS flag in the list of definitions in: Project Properties -> Settings Properties -> C++ > Preprocessor - Preprocessor Settings.

To me this does not make sense, my everyday IDE is Rad Studio and in it if you set a compiler directive in the code it is valid for everything.

Thank you all.

  • 1
    First of all, #include files are just "copy-pasted" to produce a single source code for each .cpp file, which contains everything (contents of all included .h files and that one .cpp file). Then, in this combined source blob, defines are defined only after the #define line. So your define was either after the ifdef, or it was in a different .cpp file, or you did not include the right header. – hyde Aug 25 '21 at 21:22