简体   繁体   中英

CLION detecting UNIX operating system on WINDOWS computer

I am trying to write a program that opens up a com port to communicate but works on both LINUX and WINDOWS. To do this, I am using the method outlined in many other sources:

#ifdef __unix__
   #linux code
#else
   #windows code
#endif

If I copy the code into VSCode, it functions correctly. However, when I do this in CLION, it is detecting the operating system as LINUX instead of WINDOWS and greying out the WINDOWS code. Additionally, when I run the code, it crashes because it is attempting to execute the LINUX based code. This is an example of what I have written:

#ifdef __unix__
   int comPort;
   struct termios tty;
#else
   HANDLE comPort;
   char settingString[128];
#endif

And it is executing the unix code. I understand that the comparison happens at compile time, but is there anything I am doing wrong for CLION?

I figured out the issue. My CLION toolchain was default set to WSL and I had to change it to MinGW.

__unix__ defines the Unix environment (available API like POSIX, BSD), not a target OS. You might want to use _WIN32 for detecting Windows environment. Clion probably defines both of them. See more info on Pre-defined C/C++ compiler macros.

#ifndef _WIN32
   int comPort;
   struct termios tty;
#else
   HANDLE comPort;
   char settingString[128];
#endif

By the way, #ifdef __unix__ is not detecting Linux, that should be #ifdef __linux__ .

CLion does not define any macro, it is the job of the compiler. CLion just get the information from the compiler you have configured in the toolchain section of the settings.

My knowledge of Windows development is very imperfect, but the only compilers I can think which would define __unix__ under Windows are those which are part of a Unix/Posix environment (Cygwin, WSL or some others which are far more confidential). But if you where using them the program should compile -- I presume that what you report as a crash is a failure to compile -- and the result be executable baring other issues; or if the program does compile but fail to execute, that failure should have another source, such as not finding the needed support DLL, than crashing when trying to execute the unixy part of the code.

In other words, you have an installation or configuration issue and your lack of understanding of the context make it difficult to help you as your description of the issue is confusing. There is nothing wrong with you -- everybody has to learn -- but StackOverflow is not a web site designed to provide the kind of hand holding help you need to get unstuck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM