c-preprocessor

Automatically inserting filename & line number in logging statements of a C program

自古美人都是妖i 提交于 2019-12-04 05:26:54
问题 I am writing a program for an embedded ARM processor in C . I would like to see the source filename and line number in the logging statements. As the compiled code has no knowledge of line numbers and source files, I am looking for ways to have this inserted automatically before / during the compile process. Are there any standard tools or compiler features that I can use for this? I am using GCC. For example: This is what I would write in the source file: log("<#filename#> <#linenumber#> :

MSBuild C++ - command line - can pass defines?

主宰稳场 提交于 2019-12-04 05:19:25
Is there a way to convert something like this: #define ERROR_LOG_LEVEL 5 Into something that msbuild via command line will pass to its projects? msbuild.exe {???}ERROR_LOG_LEVEL=5 target I've read the responses to similar questions, and it looks like the answer is no , just want to double-check in case some genius has found a workaround. Macros may be defined by passing the /D option to the compiler. You can specify the /D option from MSBuild using the AdditionalOptions of ClCompile : <ItemDefinitionGroup> <ClCompile> <AdditionalOptions>/DERROR_LOG_LEVEL=5 %(AdditionalOptions)<

ANSI C: standard definition for the size of the __DATE__ and __TIME__ strings?

好久不见. 提交于 2019-12-04 04:58:21
问题 Is there a standard definition for the size of the __DATE__ and __TIME__ strings in ANSI C? The motivation behind this question is: I have two applications running on two different CPUs. During runtime, app #1 receives date and time (as part of version-info) from app #2. Of course, app #2 takes them from the preprocessor __DATE__ and __TIME__ definitions. So I would like to know whether or not I can statically allocate in app #1 an array, into which I can copy the info received from app #2.

Any way to parse preprocessed source through external tool before it compiles?

。_饼干妹妹 提交于 2019-12-04 04:27:42
问题 I want the compiler to run preprocessing, generate all the .i files like it normally does if I just use the "generate preprocessed file" option, and then run an external tool, wait for it to complete, and then go on with the compilation of those .i files (which by now can be modified of course). If that is not possible, is there a way to run an external tool on every file that is being compiled before preprocessing and compilation? (Would probably be a hell to debug in environment like that,

Preprocessor-aware code navigation in IDE for C project

一曲冷凌霜 提交于 2019-12-04 04:26:24
Background I spend a lot of time navigating and editing convoluted scientific C codes. Usually they contain hundreds of optional features switched on and off with preprocessor directives. This makes it almost impossible to say at a glance whether the current block of code is activated in my current setup or not. The code itself does not help as every feature is smudged all over the place and everything is usually done using global variables. Question Is there an IDE that can handle preprocessor directives by folding/shading the inactive code? I imagine one can maintain a project with a config

Why does stringizing an euro sign within a string literal using UTF8 not produce an UCN?

余生长醉 提交于 2019-12-04 04:07:32
The spec says that at phase 1 of compilation Any source file character not in the basic source character set (2.3) is replaced by the universal-character-name that designates that character. And at phase 4 it says Preprocessing directives are executed, macro invocations are expanded At phase 5, we have Each source character set member in a character literal or a string literal, as well as each escape sequence and universal-character-name in a character literal or a non-raw string literal, is converted to the corresponding member of the execution character set For the # operator, we have a \

A group of variadic macros

此生再无相见时 提交于 2019-12-04 03:43:49
问题 I would like to have a group of variable number of arguments passed into a macro. I have following macros which is incorrect: #define M_NARGS(...) M_NARGS_(__VA_ARGS__, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0) #define M_NARGS_(_10, _9, _8, _7, _6, _5, _4, _3, _2, _1, N, ...) N #define M_CONC(A, B) M_CONC_(A, B) #define M_CONC_(A, B) A##B #define M_ID(...) __VA_ARGS__ #define M_LEFT(L, R) L #define M_RIGHT(L, R) R #define M_FOR_EACH(ACTN, ...) M_CONC(M_FOR_EACH_, M_NARGS(__VA_ARGS__)) (ACTN, __VA

for loop macro coding style

可紊 提交于 2019-12-04 03:42:04
问题 One of my tutors at university suggests using macros to reduce repetition in c99 code, like this. #define foreach(a, b, c) for (int a = b; a < c; a++) #define for_i foreach(i, 0, n) #define for_j foreach(j, 0, n) #define for_ij for_i for_j Which can be used like this: for_ij { /*do stuff*/; } for_i { /*do stuff*/; } Another tutor with industrial background discourages its use, claiming it was seen as an anti-pattern at his former employer (but he does not know the reason behind this) . In

Why is stddef.h not in /usr/include?

我的未来我决定 提交于 2019-12-04 03:17:06
问题 I have compiled the gnu standard library and installed it in $GLIBC_INST . Now, I try to compile a very simple programm (using only one #include : #include <stdio.h> ): gcc --nostdinc -I$GLIBC_INST/include foo.c The compilation (preprocessor?) tells me, that it doesn't find stddef.h . And indeed, there is none in $GLIBC_INST/include (nor is there one in /usr/include ). However, I found a stddef.h in /usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/include . Why is that file not under /usr/include

How do I write a cpp __DIR__ macro, similar to __FILE__

此生再无相见时 提交于 2019-12-04 03:12:06
问题 The __FILE__ and __LINE__ macros are built into the C Pre-Processor, and are often used for printing debug output with file names and line numbers. I need something similar, but with just the name of the directory at the end of the path. For instance if my code is in: /home/davidc/some/path/to/some/code/foo/bar I need a macro that will just give me "bar", if the code is in /home/davidc/some/path/to/some/code/foo/bee then I need it to give me "bee". Any thoughts? (btw, this is for a C++