All Questions
345
questions
106
votes
9
answers
83k
views
How can I define a string literal on the GCC command line?
On the GCC command line, I want to define a string such as -Dname=Mary. Then in the source code, I want printf("%s", name); to print Mary.
How could I do it?
101
votes
7
answers
81k
views
How to view C preprocessor output?
How do I view the output produced by the C pre-processor, prior to its conversion into an object file?
I want to see what the MACRO definitions do to my code.
66
votes
6
answers
59k
views
How to undefine a define at commandline using GCC
How do I at compile time undefine a compiler macro using GCC? I tried some compile arguments to GCC, like -D, but I can't get to see the "not defined" message.
#include <iostream>
#...
65
votes
3
answers
2k
views
In a GNU C macro envSet(name), what does (void) "" name mean?
I came across this syntax today and couldn't work out what it meant:
// Uses the GNU C statement expression extension
#define envSet(name) ({ \
static int initialised; \
static bool set; \
(void) "" ...
44
votes
3
answers
2k
views
So many parentheses in gcc standard headers
Why are constant expressions in GCC header files surrounded by parentheses, like this?
#define INTMAX_MIN (-9223372036854775807LL)
#define INTMAX_MAX (9223372036854775807LL)
What would be the ...
42
votes
4
answers
42k
views
Macros for GCC/G++ to differentiate Linux and Mac OSX?
Macros for GCC/G++ to differentiate Linux and Mac OSX?
39
votes
3
answers
44k
views
Is there a macro definition to check the Linux kernel version?
I'm wondering if there is a gcc macro that will tell me the Linux kernel version so I can set variable types appropriately. If not, how would I go about defining my own macro that does this?
39
votes
4
answers
24k
views
Equivalents to MSVC's _countof in other compilers?
Are there any builtin equivalents to _countof provided by other compilers, in particular GCC and Clang? Are there any non-macro forms?
30
votes
1
answer
10k
views
GCC define function-like macros using -D argument
Problem
I am trying to remove __attribute__ from my C code before I send it into a parser. Is there a way to define function-like macros using the -D argument?
Solution using header file
#define ...
27
votes
3
answers
1k
views
Why this macro is defined as ({ 1; })?
In multiple ARM back-end of Linux, I'm seeing in files clkdev.h this macro definition:
#define __clk_get(clk) ({ 1; })
See for example ./arch/arm/mach-versatile/include/mach/clkdev.h
This macro is ...
24
votes
5
answers
5k
views
Generating an error if checked boolean macro is not defined
I have several configuration files each one containing the definition of some boolean macro, to be set to 0 or 1. Then, in my code, I check the value of such a macro to decide which part of the code ...
24
votes
2
answers
17k
views
How do I show the value of a #define at compile time in gcc
So far I've got as far as:
#define ADEFINE "23"
#pragma message ("ADEFINE" ADEFINE)
Which works, but what if ADEFINE isn't a string?
#define ADEFINE 23
#pragma message ("ADEFINE" ADEFINE)
causes:
...
21
votes
2
answers
3k
views
How does the lambda macro create a lambda?
I found this piece of code on GitHub but didn't quite understand it:
#define lambda(ret_type, _body) ({ ret_type _ _body _; })
Then:
int (*max)(int, int) = lambda(int,
(...
17
votes
6
answers
25k
views
How big is wchar_t with GCC?
GCC supports -fshort-wchar that switches wchar_t from 4, to two bytes.
What is the best way to detect the size of wchar_t at compile time, so I can map it correctly to the appropriate utf-16 or utf-...
17
votes
5
answers
4k
views
Run a "light" preprocessor for GCC
Is there a way to run the GCC preprocessor, but only for user-defined macros?
I have a few one-liners and some #ifdef, etc. conditionals, and I want to see what my code looks like when just those are ...
16
votes
4
answers
8k
views
How to deprecate a C pre-processor macro in GCC?
I know how to use __attribute__((deprecated)) or [[deprecated]] to deprecate a function like this:
int old_fn() __attribute__ ((deprecated));
[[deprecated]] int old_fn2();
But how to deprecate a ...
15
votes
2
answers
5k
views
How to check __builtin_ function is available on gcc
I need to know is there a method for gcc to check presence of those awesome __builtin_MY_DESIRED_FUNCTIONs
For example, I'd like to use __builtin_nan and be sure it is available for my program and it ...
14
votes
2
answers
5k
views
Why does GCC keep empty functions?
In most cases if I want to create an optional feature in C, I simply create two functions like this:
#ifdef OPTIONAL_SOMETHING
void do_something(int n, const char *s)
{
while (n--) {
...
14
votes
6
answers
3k
views
Is it possible to un-const typeof in gcc pure C?
I have a macro that uses GCC's typeof to create a variable of the same type of a macro argument. The problem is: if that argument has const type, the variable created inside the macro is const and I ...
14
votes
3
answers
5k
views
Producing a list of all the preprocessor symbols defined in headers
Say I use some C or C++ library, made out of headers and some source files, that are compiled into a static or shared library I can link with.
In the headers of the library (dozens... or hundreds of ...
13
votes
7
answers
3k
views
Is there a good general method for debugging C++ macros?
In general, I occasionally have a chain of nested macros with a few preprocessor conditional elements in their definitions. These can be painful to debug since it's hard to directly see the actual ...
13
votes
2
answers
8k
views
Anyway to see list of preprocessor defined macros?
I'd like to see all macros that are defined by the invocation of the compiler I'm using. Is there any way to do this? I have seen in the manual it says you can use cpp -dM but this doesn't work ...
13
votes
2
answers
2k
views
Is there a way to pass multiple values to macro function as single defined macro value in C?
I want to declare pin definition in global header as a simple line like:
#define STATUS_LED B,7
Then I want to pass this pin definition to function above:
CMBset_out(STATUS_LED);
I don't know how ...
12
votes
5
answers
9k
views
How to create a "C single-line comment" macro
I am trying to create a "single line comment" macro in C, this is used conditionally to comment out lines of codes, according to some global macro definitions. It is the same idea expressed in this ...
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 ...
12
votes
4
answers
4k
views
Valid preprocessor tokens in macro concatenation
I tried to understand the macros in c using the concatenation preprocessor operator ## but I realized that I have problem with tokens. I thought it was easy but in practice it is not.
So the ...
12
votes
2
answers
6k
views
How to suppress GCC variadic macro argument warning for zero arguments for a particular macro definition within a source file [duplicate]
I want to suppress GCC variadic macro argument warning for zero arguments, produced for example by:
// for illustration purposes only:
int foo(int i) { return 0; };
#define FOO(A, ...) foo(A, ##...
11
votes
1
answer
6k
views
Variadic macro without arguments
I am using some logging macros, which are supposed to print out the information provided by the __PRETTY_FUNCTION__ macro and if needed name and value of up to two arguments.
A simplified version of ...
10
votes
3
answers
1k
views
Why can't we use the preprocessor to create custom-delimited strings?
I was playing around a bit with the C preprocessor, when something which seemed so simple failed:
#define STR_START "
#define STR_END "
int puts(const char *);
int main() {
puts(STR_START hello ...
10
votes
1
answer
2k
views
Can I see defined macros during compilation of a C code?
I have a piece of code which compiles without problems with x86 gcc 4.4.1 but fails with blackfin gcc 4.1.2 with many "expected unqualified-id before numeric constant" errors. I see that there are ...
10
votes
5
answers
2k
views
C test if variable is in read-only section
I'd like to write a low-level logging function that would look like:
DO_DBG("some string", val1, val2)
What I want it to do, is to store the pointer to the string rather than a copy of the string, ...
10
votes
2
answers
2k
views
C preprocessor tokenization does not expand macro?
1) Why is the macro MSG not expanded in the following expression?
#define MSG Hello
#define HELLO(name) MSG ## name
void HELLO(Dave) () {}
Using
gcc -E -P test.cpp
Output:
void MSGDave () {}
...
9
votes
4
answers
10k
views
How to workaround the GCC warning, "the address of XXX will never be NULL"?
I'm working on a C program. There is a function which takes two pointer arguments, call it cmp(). I present here a simplified stand-in for cmp() for illustrative reasons:
int cmp(struct foo *a, ...
9
votes
3
answers
627
views
Why is -lm not necessary in some cases when compiling and linking C code?
I have a sample file here:
#include <stdio.h>
#include <math.h>
int main(){
printf("%f\n", log(10));
}
When I compile it with gcc sample.c -o a it works just fine. I can run it with ....
9
votes
2
answers
8k
views
Using Likely() / Unlikely() Preprocessor Macros in if-else if chain
If I have:
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
if (A)
return true;
else if (B)
return false;
...
else if (Z)
return true;
else
...
9
votes
2
answers
4k
views
msvc's equivalent of gcc's __BASE_FILE__
Is there any equivalent of __BASE_FILE__ in Visual C++? I want to know name of the file currently being compiled by VC++.
Note: __FILE__ expands into current file, e.g. it may be one of #includes.
...
9
votes
1
answer
2k
views
what is the purpose of pre-processor macros defined in files linux/compiler.h?
I am very new to Linux kernel. And I am using the sparse tool to clean the noise present in the code. I encountered these macros:
# define __user __attribute__((noderef, address_space(1)))
...
8
votes
3
answers
4k
views
What C preprocessor macros have already been defined in gcc?
In gcc, how can I check what C preprocessor definitions are in place during the compilation of a C program, in particular what standard or platform-specific macro definitions are defined?
8
votes
1
answer
13k
views
Detect ARM NEON availability in the preprocessor?
According to the ARM ARM, __ARM_NEON__ is defined when Neon SIMD instructions are available. I'm having trouble getting GCC to provide it.
Neon available on this BananaPi Pro dev board running Debian ...
8
votes
5
answers
2k
views
C: What does this macro mean?
How do you read the second line of this macro? What does (type *)0 mean in this context?
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)...
8
votes
3
answers
419
views
More information on `({});` in C?
I've noticed that sometimes, C macros are written as something like this:
#define foo(bar) ({ ++bar; })
After some experimentation, I've found that:
({}); will compile, but do nothing. (As expected....
8
votes
1
answer
1k
views
Recognizing clang, gcc, and tcc by implementation-defined macros
I use clang, gcc, and tcc, and I'd like to be able to differentiate between the three in a common header.
Judging by their macro dumps, I expect that the presence of the
__clang__ macro will ...
8
votes
4
answers
3k
views
What is the purpose of a double negative in macro definition, like (!!(expr))? [duplicate]
Possible Duplicate:
Double Negation in C++ code.
I'm reading a code base, and find something like this:
#define uassert(msgid, msg, expr) (void)((!!(expr))||(uasserted(msgid, msg), 0))
I ...
7
votes
3
answers
13k
views
Disable "warning: the address of 'x' will always evaluate as 'true'"
The problem is this:
#define do_stuff(ret) ((ret) ? getstuff(ret) : 0)
int var;
do_stuff(&var);
test.h:34:46: warning: the address of 'var' will always evaluate as 'true' [-Waddress]
do_stuff ...
7
votes
1
answer
6k
views
How to evaluate a nested preprocessor macro
let's say I want to select the behaviour of a certain preprocessor directive evaluating at compile time the concatenation of a constant string and the result of another macro.
#define CASE1 text1
#...
7
votes
3
answers
6k
views
Macro Expansion: Argument with Commas
The code I'm working on uses some very convoluted macro voodoo in order to generate code, but in the end there is a construct that looks like this
#define ARGS 1,2,3
#define MACROFUNC_OUTER(PARAMS) ...
7
votes
4
answers
4k
views
Retrieve output target name as a string at compilation time in C++
I have a project which compiles in multiple platforms... Windows, Unix, Linux, SCO, name your flavor.
I would like to stuff the output target's name into a variable in the project source code (the ...
7
votes
1
answer
2k
views
What is the best way to boilerplate the "cold/never_inline" error handling technique in C++?
In this article, a technique is described to move error code out-of-line in gcc to help optimise the hot path for size as much as possible. An example of this would be:
#define unlikely(x) ...
7
votes
1
answer
7k
views
Macro defined in main.c not visible in another included file
I have multiple C and H files
In main.c I defined a macro, and in ws_driver.c I want to use it.
ws_driver.h is included in main.c.
main.c
#define WS_PORT PORT_D8
#define WS_BIT D8
#define WS_DDR ...
7
votes
2
answers
7k
views
Something between __func__ and __PRETTY_FUNCTION__?
I work with g++ 4.8.1 and use these two macros for debugging. However, the __func__ macro gives me only the function name, which might be misleading in the case you have many functions with the same ...