-2

I have to reset system date and time either on Linux, or on Windows. How do I check OS without using ifdef, or if defined?

alexander.sivak
  • 4,352
  • 3
  • 18
  • 27
  • 2
    What build system are you using? – lubgr Sep 24 '20 at 09:50
  • Boost has header for checking this. See: https://stackoverflow.com/questions/46090534/how-to-get-platform-ids-from-boost It internally uses ifdefs I guess, but none will be in your code. – Łukasz Ślusarczyk Sep 24 '20 at 09:55
  • 4
    Even if you detect the OS without any `ifdef`, how are you planning to use OS-specific API without any `ifdef`? – Kaldrr Sep 24 '20 at 09:55
  • @lubgr CMake... – alexander.sivak Sep 24 '20 at 09:56
  • 3
    Why can't you use ifdef and what OS-specific thing are you planning to do without using ifdef somewhere? – Dan M. Sep 24 '20 at 09:58
  • 3
    **You probably cannot.** Think of weird operating systems such as [netbsd](http://netbsd.org/) or [Solaris](https://www.oracle.com/solaris) or [GNU hurd](https://www.gnu.org/software/hurd/hurd.html) – Basile Starynkevitch Sep 24 '20 at 10:21
  • 2
    The short answer is that you can't. If you don't check for particular macros (whether set by your compiler or set in a build script that can detect the OS and define macros for your build accordingly) you will be relying on some other (non-standard in C++) library function or program to detect the OS. But, to use such a library function or program, you need to check the OS to determine if that library function or program is available. At that point, the argument becomes infinitely recursive (like the old children's song "there's a hole in the bucket"). – Peter Sep 24 '20 at 11:21

2 Answers2

1

With CMake, you can conditionally add files to a target by using generator expressions. With that, you can isolate any platform-specific code in files, and use them in your build specification as follows.

add_executable(reset-time
    $<$<PLATFORM_ID:Darwin>:resettime-macos.cpp>
    $<$<PLATFORM_ID:Linux>:resettime-linux.cpp>
    $<$<PLATFORM_ID:Windows>:resettime-windows.cpp>)

And as @eeroika pointed out in the comments, a good alternative is

add_executable(reset-time
    resettime-$<LOWER_CASE:${CMAKE_SYSTEM_NAME}>.cpp)

as it requires not adjustment (of the CMakeLists.txt) when porting your project to a new system and you can catch a missing implementation file earlier, i.e., before linking.

lubgr
  • 37,368
  • 3
  • 66
  • 117
  • I can't user 2 files, just one. – alexander.sivak Sep 24 '20 at 10:04
  • 4
    @lubgr I don’t think you should delete the answer — because while it may not be useful to the OP (given their specific constraints), it seems like it can still be useful to other people in the future who come across the question and answer from a Google search or whatever. Don’t you think? Or, on the other hand, if you think the problem itself it very specific to the OP’s situation and not likely to be something that others will come across, then yeah in that case I guess maybe you’d want to consider deleting the answer. – sideshowbarker Sep 24 '20 at 10:16
  • @sideshowbarker Thanks for your comment. I think I should leave it then, for future reference. – lubgr Sep 24 '20 at 10:22
  • 2
    @alexander.sivak about that *“I can't user 2 files, just one”* constraint, you should edit/update the question to explicitly mention that constraint. Otherwise, you’re risking to waste the time of other people here who make an effort to help find an answer without knowing until afterwards that you have that otherwise-secret/surprise constraint. – sideshowbarker Sep 24 '20 at 10:25
  • 1
    Alternatively: `resettime-$.cpp` Advantage of this is that CMake list doesn't need changing if the program is ported to a new system. Also, you'll get an error about missing file rather than undefined linker errors. – eerorika Sep 25 '20 at 04:08
0

You could set a global macro definition to the compiler based on which operating system you are compiling for. For example -D IS_WINDOWS=1. You could then use #if IS_WINDOWS instead of #ifdef. I don't know why this would be useful but it achieves what you asked.

eerorika
  • 232,697
  • 12
  • 197
  • 326