How do I check OS with a preprocessor directive?

前端 未结 16 1357
星月不相逢
星月不相逢 2020-11-22 13:53

I need my code to do different things based on the operating system on which it gets compiled. I\'m looking for something like this:

#ifdef OSisWindows
// do         


        
16条回答
  •  孤街浪徒
    2020-11-22 14:08

    I wrote an small library to get the operating system you are on, it can be installed using clib (The C package manager), so it is really simple to use it as a dependency for your projects.

    Install

    $ clib install abranhe/os.c
    

    Usage

    #include 
    #include "os.h"
    
    int main()
    {
        printf("%s\n", operating_system());
        // macOS
        return 0;
    }
    

    It returns a string (char*) with the name of the operating system you are using, for further information about this project check it out the documentation on Github.

提交回复
热议问题