Determine if Linux or Windows in C++

后端 未结 6 1882
忘了有多久
忘了有多久 2021-02-04 00:40

I am writing a cross-platform compatible function in C++ that creates directories based on input filenames. I need to know if the machine is Linux or windows and use the appropr

6条回答
  •  灰色年华
    2021-02-04 00:48

    Look into http://en.wikipedia.org/wiki/Uname

    If you are using g++ as your compiler/GNU then you could try the code below. POSIX compliant environments support this:

    #include 
    #include 
    #include 
    
    int main()
    {
        struct utsname sysinfo;
        if(uname(&sysinfo)) exit(9);
        printf("os name: %s\n", sysinfo.sysname);
        return 0;
    }
    

提交回复
热议问题