Are there any macros to determine if my code is being compiled to Windows? [duplicate]

[Edit: I assume you want to use compile-time macros to determine which environment you’re on. Maybe you want to determine if you’re running on Wine under Linux or something instead of Windows, but in general, your compiler targets a specific environment, and that is either Windows (DOS) or it isn’t, but it’s rarely (never?) both.] …

Read more

Where can I learn about #ifdef?

The GHC documentation has a section relating to the C pre-processor that documents some of the predefined pre-processor macros. The Cabal documentation has a section relating to conditional compilation that gives an example relating to base. If you are writing a portable package, you should be using Cabal, anyway.

C# preprocessor differentiate between operating systems

What you are asking for is possible but needs a bit of work. Define a preprocessor variable in your csproj <PropertyGroup Condition=” ‘$(OS)’ == ‘Windows_NT’ “> <DefineConstants>_WINDOWS</DefineConstants> </PropertyGroup> Use that in your code #if _WINDOWS // your windows stuff #else // your *nix stuff #endif I find this technique useful when you have constants that …

Read more

Bit count : preprocessor magic vs modern C++

Why not use the standard library? #include <bitset> int bits_in(std::uint64_t u) { auto bs = std::bitset<64>(u); return bs.count(); } resulting assembler (Compiled with -O2 -march=native): bits_in(unsigned long): xor eax, eax popcnt rax, rdi ret It is worth mentioning at this point that not all x86 processors have this instruction so (at least with gcc) you …

Read more

C++: Can a macro expand “abc” into ‘a’, ‘b’, ‘c’?

I’ve created one today, and tested on GCC4.6.0. #include <iostream> #define E(L,I) \ (I < sizeof(L)) ? L[I] : 0 #define STR(X, L) \ typename Expand<X, \ cstring<E(L,0),E(L,1),E(L,2),E(L,3),E(L,4), E(L,5), \ E(L,6),E(L,7),E(L,8),E(L,9),E(L,10), E(L,11), \ E(L,12),E(L,13),E(L,14),E(L,15),E(L,16), E(L,17)> \ cstring<>, sizeof L-1>::type #define CSTR(L) STR(cstring, L) template<char …C> struct cstring { }; template<template<char…> class P, typename S, typename …

Read more

Pseudo-generics in C

You can do something like this in a header file: // // generic.h // #define TOKENPASTE(x, y) x ## y #define GET_MINIMUM(T) TOKENPASTE(get_minimum_, T) TYPE GET_MINIMUM (TYPE) (TYPE * nums, size_t len){ TYPE min = nums[0]; for (size_t i = 1; i < len; i++) { if (nums[i] < min) { min = nums[i]; } …

Read more