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.
Related
This is my first blog or question.
I want to develop platform abstract layer(PLA) for mobile app using c++. i.e from mobile app I could able to talk to bluetooth device without bothering which is underlying OS (i.e IOS ANDROID etc) running. So, I need to discover which OS is running whether IOS or ANDROID using c++ code but without calling native API of any OS.
So, could anybody suggest your thought for the same how we can determine the OS type (ie, IOS, ANDROID) and OS version using c++ code.
To develop which IDE would be preferred to develop c++ code?
Thanks in advance..!!
Thanks,
Raju.
Even if you're developing cross platform code, when the time to compile it comes, you need to target a platform. Determining it is then a matter of checking precompiler definitions.
Most compiler will define some platform while compiling, if not, you can do so by yourself when configuring your project.
For example, Xcode will define __APPLE__ and TARGET_OS_IPHONE and TARGET_OS_MAC when compiling respectively iOS and Mac projects.
For Android, __ANDROID__ should be defined by the toolchain however I've seen multiple mk files add explicit platform flags like so:
LOCAL_CFLAGS := -DANDROID $(LOCAL_CFLAGS)
It is then a matter of using precompilation conditions:
#ifdef __APPLE__
#if TARGET_OS_IPHONE
// Configure for iPhone
#endif
#endif
#ifdef ANDROID
// Configure for Android
#endif
For determining the version, there isn't really anyway to do that without calling Native API. Fortunately, there is pattern you can use to factor out the specific implementation and put a generic interface in front of this code.
As far as IDE, this is largely a matter of choice and availability. Most popular ones are Xcode for iOS and Eclipse for Android but it is possible to use other as well.
Are there any C++ compile time macros which exists to detect which Windows OS the code is being compiled on. I basically want to support certain functions only on Win7. So I am interested in doing something like this
#if <os_macro> = WIN7
// This function would do something valid only on Win7 builds.
bool myfunction {
// do something here
}
#else
// This function would typically return false, since its not supported on OS below win7
bool myfunction {
return false;
}
#endif
Is there any other better way to do this?
The OS that it's getting compiled on is not all that important; what matters more is the OS that the code is running on, which you obviously cannot detect at compile time. But if you want your code to run on older versions of Windows, you can set WINVER and _WIN32_WINNT to certain values, which will cause newer functions not to be available etc. (just search the Windows header files for where those macros get tested to get an idea).
To test for functionality at runtime, use GetProcAddress (and possibly also LoadLibrary, if it's in a newer DLL) to test if the function is available. If it is, call it, if not, don't.
See also the predefined macros used by the Visual Studio compiler if you want to detect the compiler version etc.
There are a set of standard windows header macros to tell you the exact version of the OS
There are certain things that do require knowing which OS version at compile time.
For example import "winhttp.dll" will compile under Windows 7, but cause compile time error (C1083) under Windows 10.
But if you switch to import "winhttpcom.dll". It would compile under Windows 10 but fail under Windows 7.
So an OS macro is needed here to import appropriate dll.
At least with MS VC++, WIN32, _WIN32, and _WIN32_WINNT, for starters. Unless you need to control it at compile time, you might consider using something like GetVersionEx to detect at run-time, so the same build runs on older versions, but takes advantage of new features when they're available.
My response is 10 yrs late but posting the answer for upcoming yrs.
For detecting the version c/c++ compiler version can be used. Suitable macro is mentioned below:
_MSC_VER
_MSC_FULL_VER
Reference: MS macros
My two cents for people that want to compile / include differently between CONSOLE and WIN32 App under visual Studio (2017) in my case.
You you use wizard to create a Console App, You will have:
WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)
under:
If You use wizard for win32 GUI App:
WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
so NO _CONSOLE.
so You can write:
#ifdef _CONSOLE
// for Console
#else
// for GUI
#endif // _CONSOLE
int main()
{
#ifdef _CONSOLE
// for Console
#else
// for GUI
#endif // _CONSOLE
return 0;
}
I'm modifying a large C++ project whose first release was Windows 7-only to work on Vista and XP.
The main work will be to change the W7 API calls to use GetProcAddress at runtime (to avoid static linking with methods that are not available on the other platforms), and so I'm wondering if there are any tools that can help identify which calls need to be changed -- hopefully by examining the C++ code itself.
Failing that, would it be best to try building the project against an older Windows SDK? -- for example: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=6510
#define WINVER 0x501
Now everything that is newer than Windows XP will cause a compilation error.
Replace everything that causes an error until none remain.
If you have some sed-fu, you can probably write a filter that directly finds all #if WINVER > 0x501 blocks in the windows headers, but for me, this is a bit out of my scope :-)
I would open your binaries using the depends.exe tool (either from your VS install or from here) under WinXP and Vista to see which functions can't be statically linked under these OSes. These would be the functions which your binary is using, but which are missing in older releases of the OS. You'll have to deal with them somehow: either implement them by yourself, replace them with something else or disable some of the functionality of your app.
I'm a third year computer science student. I was raised on Visual C++ and have gotten quite proficient at using it. My school however, teaches primarily on Linux platforms. Up until now I have just programmed and debugged in Visual studio, then when I was certain I got everything working, I would recompile the source in Linux to make sure it work there.
Now however now my projects require use of the Unix API calls, Berkly sockets and sometimes pthreads.
Are there libraries available that give me access to the Unix API on windows? If so how would i go about using them Visual C++ 2010?
I really don't want to have devolve to using Gedit and Gdb for debugging complex(for me at least) code.
Typically, you would just use the platform-independent equivalents found in various libraries- usually Boost. However, if you must use the Unix-specific APIs, the only way to go will be to wrap their functionality for Windows. Likely, there already exist libraries that serve this purpose, much as you can use WINE and such for Unix, but as a Windows programmer myself I wouldn't really know.
You could try something like Cygwin to provide the Unix API in Windows, build and compile there, then make any minor adjustments to use the native API when you transfer to Linux.
You can also check each library to see if a Windows version is available. If no Win version is listed on the lib's site, Google around and see if someone has built it for Windows; that may provide you a usable version.
If you don't specifically need the Unix API, Boost or another cross-platform library is probably your best bet to provide the needed functions.
For non-Windows development, finding a replacement to VS is probably a good idea (unfortunately there's really no IDE that can compare, IMO). Code::Blocks is the nearest, but is still missing a lot of features. KDevelop has been recommended to me several times, seems nice at first glance. Eclipse is maybe the only IDE slower than VS2010, something to consider before using it.
Most Unix-only APIs aren't available for Windows. I suggest you switch to Code::blocks, which is cross-platform. It is pretty similar to VC++ in many ways, and it's easy to get used to it.
Either switch to Code::blocks, or switch to Eclipse - both of which are multi-platform. I can attest that Eclipse 3.6 works awesome debugging C++ Qt apps on Linux and Windows.
I know this is late, but a good answer is never too late! Hopefully mine is one.
The only thing I can think of is use the preprocessor.
#ifdef __unix__
...UNIX code here
#endif
and write the UNIX code in the block so that you can compile it on the UNIX system required. Then, outside those blocks you could use the WIN32 analogs inside a similar block
#ifdef _WIN32
...Windows code here
#endif
Such that an end result may look like
//Cross platform includes
//cross platform namespaces
#ifdef _WIN32
//Windows includes
//Windows namespaces
#else
//unix includes
//unix namespaces
#endif
int main()
{
DoCrossPlatformFunctions();
#ifdef _WIN32
DoWindowsFunctions();
#else
DoUnixFunctions();
#endif
DoMoreXplatformStuff();
return 0;
}
You could do some short research and find the analogs to the functions you require fairly easily and then be on your way. It's a bit cumbersome, but since you seem to have a vendetta against just writing it in a UNIX VM in the first place it's probably the best option. The best part is you don't have to step outside your Visual C++ comfort zone to do it. I hope this helps!!
I fail to compile a C++ project for mobile device with Windows Mobile (Windows CE-based) operating system and Visual C++ compiler from Visual Studio fails with:
Error 1 fatal error C1083: Cannot open include file: 'io.h'
EDIT
I am trying to compile the SQLite amalgamation, the shell.c file includes the call to this io.h but the io.h is missing from the files.
I googled and I couldn't locate how can I get this .h file.
Can someone point me in the right direction?
The io.h file is not available in SDKs for Windows CE-based systems like Windows Mobile.
In fact, io.h header has never been a part of ISO C nor C++ standards. It defines features that belongs POSIX compatibility layer on Windows NT, but not Windows CE.
Due to lack of POSIX features on Windows CE, I developed a small utility library WCELIBCEX. It does include io.h but a very minimal version and which is likely insufficient for SQLite. However, as ctacke mentioned, you should use SQLite port for Windows CE because original version of SQLite is not compilable for this platform.
p.s. Note, Your question does not specify explicitly that you're building for Windows Mobile. If one doesn't spot the .NET Compact Framework mentioned in tags, then the whole question is ambiguous.
It looks like io.h is part of standard VS, but proobably not part of WINCE edition (if there is one). From your dir /s it looks like you don't have it.
I looked at shell.c and it does not include io.h that for wince:
#if defined(_WIN32) || defined(WIN32)
# include <io.h>
#define isatty(h) _isatty(h)
#define access(f,m) _access((f),(m))
#else
/* Make sure isatty() has a prototype.
*/
extern int isatty();
#endif
#if defined(_WIN32_WCE)
/* Windows CE (arm-wince-mingw32ce-gcc) does not provide isatty()
* thus we always assume that we have a console. That can be
* overridden with the -batch command line option.
*/
#define isatty(x) 1
#endif
You are probably compiling with the wrong macros defined.
Have you considered looking at the project files from the SQLite for Windows CE site to see how they got it to compile for CE? I've never seen native code files designed for the desktop ever "just compile" for Windows CE without having to do some preprocessor work and it's likely they've got the answers to what you need in those projects.
Found something that looks like your problem here. Apparently, althought io.h is a standard Microsoft header, there is no port of it to mobile plataforms.
You are probably using some library that was not designed for use with mobile devices, and that library must be trying to use the non-mobile API.
If you see this error while trying to install a Python library, follow https://stackoverflow.com/a/16588726/284795
Basically, remove visual studio 2010, then some registry keys manually then reinstall. Worked for me.