All Questions
Tagged with macros preprocessor
220
questions
219
votes
31
answers
222k
views
__FILE__ macro shows full path
The standard predefined macro __FILE__ available in C shows the full path to the file. Is there any way to shorten the path and get just the filename? I mean instead of
/full/path/to/file.c
I see
to/...
176
votes
70
answers
132k
views
What is the worst real-world macros/pre-processor abuse you've ever come across?
What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)?
Please add a short snippet or story if it is really entertaining. The goal ...
128
votes
4
answers
87k
views
Swift: how to use PREPROCESSOR Flags (like `#if DEBUG`) to implement API keys?
In Objective-C it was sometimes useful to use static string constants to define alternate API keys (for example to differentiate between RELEASE and DEBUG keys for analytics packages, like MixPanel, ...
43
votes
2
answers
69k
views
Add preprocessor macro to a target in xcode 6
Probably this is pretty simple, but I can't find a way to define a preprocessor macro for a target in Xcode 6.
23
votes
2
answers
21k
views
How do I define preprocessor macros in Xcode 4?
I have two targets set up for my app (a lite version and a pro version) and I want to integrate some subtle differences in the code for each of them (e.g. the pro version will not show any iAd banners)...
23
votes
2
answers
48k
views
What does ##__VA_ARGS__ mean?
I would like to know what ## does in this macro definition:
#define debug(M, ...) fprintf(stderr,M "\n",##__VA_ARGS __)
I googled for an answer and I came up with the following.
The ## will remove ...
19
votes
3
answers
20k
views
how to expand VC++ macro references using Visual Studio?
Rather than right clicking on the macro identifier and then on "go to definition", is it possible to expand macro references and see what the code that is passed to the compiler looks like?
18
votes
2
answers
5k
views
How do I write universal Swift code for both iOS and macOS. In cocoa I could use #ifdef, what do I do now?
For our project we always used one source file for both platforms: iOS and macOS (previously OS X). Right now I am migrating to Swift. Unfortunately there is some files which need
import Cocoa
and on ...
14
votes
4
answers
26k
views
C Preprocessor Macro equivalent for Python
I use to define macros (not just constants) in C like
#define loop(i,a,b) for(i=a; i<b; ++i)
#define long_f(a,b,c) (a*0.123 + a*b*5.6 - 0.235*c + 7.23*c - 5*a*a + 1.5)
Is there a way of doing ...
12
votes
2
answers
13k
views
'ISO C99 requires at least one argument for the "..." in a variadic macro' When specifically using c11 and -Wno-variadic-macros [duplicate]
I have a simple: #define log(text, ...) fprintf(stderr, "stuff before" text "stuff after", ## __VA_ARGS__); which is triggering: error: ISO C99 requires at least one argument for the "..." in a ...
11
votes
5
answers
7k
views
Do macros in C++ improve performance?
I'm a beginner in C++ and I've just read that macros work by replacing text whenever needed. In this case, does this mean that it makes the .exe run faster? And how is this different than an inline ...
11
votes
2
answers
5k
views
Xcode: preprocessor macros for conditional DEBUG / TEST code
I have sections in my code (for example in the AppDelegate.m) that should not be compiled for the Unit Tests, like
#ifndef CONFIGURATION_TESTS
// Code that should not be compiled in the Unit Tests
#...
11
votes
2
answers
563
views
Macro replacement list rescanning for replacement
I'm reading the Standard N1570 about macro replacement and misunderstand some wording from 6.10.3.4.
1 After all parameters in the replacement list have been substituted
and # and ## processing ...
10
votes
2
answers
6k
views
Why isn't my cocoapods post_install hook updating my preprocessor macros?
I've been going round and round for a couple days now trying to figure out why my post_install hook isn't producing the output I'm expecting. Here's my Podfile:
source 'https://github.com/CocoaPods/...
9
votes
2
answers
2k
views
How to declare a function like macro using CMake
I can't find how to declare function like macros in CMake.
I need a macro function like:
#define MYFUNC(foo) QString( foo + "_suffix" )
to be defined by my CMakeLists.txt file. I tried:
...
8
votes
2
answers
673
views
Create a "forCount" control structure in Swift
In many projects this control structure is ideal for readability:
forCount( 40 )
{
// this block is run 40 times
}
You can do exactly that in objective-C.
Given that Swift has a very different ...
8
votes
1
answer
711
views
EXTENDS challenge: preprocessor function macros and class-like oop
Background
I've been using the C preprocessor to manage and "compile" semi-large javascript projects with multiple files and build targets. This gives full access to C preprocessor directives like #...
7
votes
2
answers
1k
views
Finding the largest size of 100 structs at compile time in C
I have 100 structs that look something like this:
struct s00 { char data[30]; };
struct s01 { char data[30]; };
struct s02 { int data[10]; };
struct s03 { double data[5]; };
struct s04 { ...
7
votes
3
answers
6k
views
with xcode, how to define preprocessor macros from the output of a command?
As the topic says, I want to be able to run a specific command during build, and have its output be the definition of a preprocessor macro.
Right now, I have several user-defined variables (in ...
6
votes
4
answers
1k
views
Get specific value of preprocessor macro
In my build settings i have define some preprocessor macros
i.e. SANDBOX_ENV=1
I want to use the value of SANDBOX_ENV in my shell script.
I have tried echo "SANDBOX value is = ${...
6
votes
2
answers
1k
views
C++ Preprocessor doesn't process define as expected
I'm having problems with cpp Preprocessor. I have this Input.h file like this:
#ifndef PLATFORM_MOBILE1111
#define MyTest WEB111
#endif
int MyTest;
I process it with this command (on OSX):
...
6
votes
1
answer
2k
views
Is it possible to prevent the removal of the comma with empty __VA_ARGS__ in Visual C++?
On Visual Studio 2005 I have a macro that looks like this (examplified!!):
#define MY_CALL(FUN, ...) \
if(prepare(x, y)) { \
FUN(__VA_ARGS__); \
}
/**/
As long as the function takes ...
6
votes
0
answers
3k
views
Accessing %(PreprocessorDefinitions) from Pre-Build Event command line in Visual Studio 2010
I’m having some difficulty with the Visual Studio 2010 build process. To put it simply, I would like to pass the preprocessor definitions set up for the currently building configuration into a batch ...
5
votes
6
answers
16k
views
Is it possible to set preprocessor macro in sln file and not in a project? (VS2008 c++)
I am maintaining a large codebase and some vcproj files are used in different solutions. Due to some horrendous configuration and dependencies it seems the best way to handle some build issues is to #...
5
votes
2
answers
4k
views
Can I conditionally check the value of a macro using the C preprocessor?
I know I can use the C preprocessor to conditionally compile something like:
#define USESPECIALFEATURE
#if defined USESPECIALFEATURE
usespecialfeature();
#endif
But I am wondering if I can do ...
5
votes
1
answer
5k
views
Xcode - defining a preprocessor macro for conditional compilation
I'm using XCode 4, and in my project build settings, I've set :
Preprocessor macros
Debug DEBUG;FULL
Release FULL
and in another target of the same project :
Preprocessor macros
Debug ...
5
votes
1
answer
10k
views
Calling multiple macros from another macro in C++
If I have two macros defined:
#define MAC1(X) {something here}
#define MAC2(X,Y) {something here}
and create a third one like this:
#define MAC3(X,Y) MAC1(X); MAC2(X,Y)
Can you please clarify how ...
5
votes
1
answer
3k
views
Does Delphi's conditional compilation allow the defined symbols to contain values?
In Delphi, you can define symbols, just like in C/C++.
Delphi:
{$DEFINE MY_SYMBOL}
C/C++:
#define MY_SYMBOL
This allows you to check whether the symbol is defined by using {$IFDEF MY_SYMBOL} in ...
5
votes
2
answers
2k
views
How to get the line number from the call site using __LINE__ or some other method?
Consider this short program that I wrote:
#include <iostream>
template<bool Debug = false>
constexpr int add(const int& a, const int& b) {
if (Debug)
...
5
votes
1
answer
3k
views
For-loop macro/preprocessor for assembly files in GCC
I am fairly certain I have seen this in code before, but I am unable to find any references on how to do it. I do expect this to be compiler or assembler specific.
I want to define an function ...
5
votes
2
answers
6k
views
GCC ARM Assembly Preprocessor Macro
I am trying to use an assembly(ARM) macro for fixed-point multiplication:
#define MULT(a,b) __asm__ __volatile__ ( \
"SMULL r2, r3, %0, %1\n\t" \
"ADD r2, r2, #0x8000\n\t" \
...
5
votes
1
answer
114
views
How does preprocessing work in this snippet?
#include<stdio.h>
#define A -B
#define B -C
#define C 5
int main() {
printf("The value of A is %dn", A);
return 0;
}
I came across the above code. I thought that after preprocessing, it ...
4
votes
2
answers
20k
views
Macro to call a function
I need a macro (or a function, but preferably a macro) that takes a function name and an unlimited number of arguments, then passes the arguments to the function. Let's say this macro is MACROFOO.
#...
4
votes
2
answers
2k
views
Is there a way for Rust macros to act as text substitutions like in C?
In C, if you wanted to, you could:
#define do {
#define end }
// ...
if (foo == bar) do
foo += 5;
bar /= foo;
end
Is there a way to do something like this in Rust?
4
votes
2
answers
2k
views
C style: Macros or preprocessor?
I've written a library to match strings against a set of patterns and I can now easily embed lexical scanners into C programs.
I know there are many well established tools available to create lexical ...
4
votes
1
answer
1k
views
Why cant I compare macro and enum using #if?
I am trying to check using #if, that a macro and enum are equal or not. the check fails even if both has same value. why?
Created a macro using #define NUMBER 2. Created enum including one entry with ...
4
votes
2
answers
5k
views
Pre-processor macro to convert an hex string to a byte array
I've defined an AES-128 key as a build symbol inside my IDE, so that it calls GCC like this:
arm-none-eabi-gcc -D"AES_KEY=3B7116E69E222295163FF1CAA1681FAC" ...
(which is equivalent to #define ...
4
votes
2
answers
315
views
Enum class scope resolution operator
When I tried to compile test.cc by g++ test.cc --std=c++14, I got the following error.
test.cc:5:26: error: expected unqualified-id before numeric constant
Colour colour = Colour::None;
test.cc
#...
4
votes
2
answers
3k
views
Solaris and Preprocessor Macros
Would someone post the results of cpp -dM < /dev/null from a Solaris 10 or above system?
I'm having trouble locating what preprocessor macros are typically defined. Solaris documentation does not ...
4
votes
3
answers
254
views
C++ (gcc) Preprocessor Macro: Automatic function generation - OpenGL Shader "Swizzle" Syntax
Let me start this question by stating that I don't know if what I am aiming to do is possible, and if it is, I don't know how to find the information.
The OpenGL shading language permits a syntax ...
4
votes
1
answer
979
views
How to change values of "-define" Erlang attributes (macro definitions)?
Normal Erlang attributes can be modified by parse tranformations. This does not apply to the "-define" attributes, as they are handled by the preprocessor. What would you do if you would like to alter ...
3
votes
1
answer
15k
views
Xcode Preprocessor Macros
In Xcode, I can edit my preprocessor macros in the project settings. I want to create a macro that refers to an environment variable. Basically, I want to be able to refer to $SRC_ROOT in my code. ...
3
votes
1
answer
6k
views
C++: Can a macro argument have a space in it?
If I define a macro taking argument as below.
#define define_int(a) int a;
And provide an argument with space in between like below
define_int(* a)
and get output?
int * a;
Intended use
#define ...
3
votes
2
answers
1k
views
Pass parameter in pascal define
I'm trying to define something in pascal (freepascal).
As in c++ you can pass variable define macro like this :
#define REP(i,k) for(int i=0; i<k; i++)
How can you do it in pascal?
I have added {...
3
votes
2
answers
2k
views
Show or log preprocessor macros during build
Is it possible to log or print preprocessor macros in XCode to the build results?
I want to see the current defined macros during a build.
For example if I have defined DEBUG and TESTSERVER as ...
3
votes
1
answer
211
views
Why are some preprocessor macros not expanded unless they are arguments to another macro?
In certain situations, some token sequences are not preprocessed fully.
For example:
#define EMPTY()
#define DELAY(x) x EMPTY()
#define PRAGMA(args) _Pragma(args)
#define WRAP( BODY ) { BODY }
#...
3
votes
1
answer
131
views
Expanding macros to Pascal Code in Inno Setup [Code] section
I was trying to make a macro to avoid duplicating code and comments.
I tried this:
#define GrowOnPage(any Page, any Component) Component.Width := Page.SurfaceWidth; Component.Anchors := [akLeft, ...
3
votes
2
answers
2k
views
How to concatenate a variable string with literal string in C macro?
Im creating a macro that accept 2 strings: variable string and literal string. How do I combine both of them to a function which accept single variable?
I know that we can combine 2 literal string in ...
3
votes
4
answers
115
views
using ## preprocessor operator with another macros output [duplicate]
I am trying to use ## preprocessor operator to determine witch GPIO to use.
here is my code :
#define GPIO_Pin_1 0x0001
#define GPIO_Pin_2 0x00CA
#define GPIO_Pin_3 0x00DE
#define GPIO_Pin_4 0x00AC
#...
3
votes
1
answer
3k
views
defining C++ preprocessor macro with quotation marks [duplicate]
I am trying to define a macro in C++ that puts quotes around a variable.
A simplified example of what I am trying to do is this:
#define PE(x) std::cout << "x" << std::endl;
and then ...