Mapping macros with respect to the source file that calls it - c

I am using a macro(debug_macro) for logging debug info that is defined in log.h file. I need to map this debug_macro to process specific debug macros. I have explained my expectations in the below example.
log.h
=======
#if callee==process1
#define debug_macro proc1_debug_macro
#if callee==process2
#define debug_macro proc2_debug_macro
process1.c
===========
#include log.h
debug_macro <<<<<====== this one should call proc1debug_macro
process2.c
===========
#include log.h
debug_macro <<<<==== this one should call proc2_debug_macro
I am newbie to C programming.Please provide me any suggessions on how to implement this ? Any help would be much appreciated.
Thanks,

On the face of it, you are missing some definitions and you need to define callee before you include the header:
log.h
#define process1 10
#define process2 20
#if callee==process1
#define debug_macro proc1_debug_macro
#elif callee==process2
#define debug_macro proc2_debug_macro
#else
#error callee not defined
#endif
process1.c
#define callee process1
#include "log.h"
debug_macro <<<<<====== this one should call proc1debug_macro
process2.c
#define callee process2
#include "log.h"
debug_macro <<<<==== this one should call proc2_debug_macro
I'm not convinced this is the best way to go about it, but it is closely related to what you ask for.
I'd probably use the different debugging functions (macros) directly, so that it is clear that different code is implementing the debugging in the different processes. Or, even more likely, I'd use the same debugging code in both processes. However, my requirements aren't necessarily the same as yours.
Note that macro names are conventionally all upper-case. Also, in general, function-like macros are preferred to object-like macros. That is, it would be better to have:
#define debug_macro(a, b c) proc1_debug_macro(a, b, c)

Define general purpose macro in one file, and make simpler macro for the local file.
log.h
#define complex_macro(a, b, c) ...
process1.c
#include "log.h"
#define simple_local_macro(a) complex_macro(a, 2, 3)

Related

Why only define a macro if it's not already defined?

All across our C code base, I see every macro defined the following way:
#ifndef BEEPTRIM_PITCH_RATE_DEGPS
#define BEEPTRIM_PITCH_RATE_DEGPS 0.2f
#endif
#ifndef BEEPTRIM_ROLL_RATE_DEGPS
#define BEEPTRIM_ROLL_RATE_DEGPS 0.2f
#endif
#ifndef FORCETRIMRELEASE_HOLD_TIME_MS
#define FORCETRIMRELEASE_HOLD_TIME_MS 1000.0f
#endif
#ifndef TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS
#define TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS 50.0f
#endif
What is the rationale of doing these define checks instead of just defining the macros?
#define BEEPTRIM_PITCH_RATE_DEGPS 0.2f
#define BEEPTRIM_ROLL_RATE_DEGPS 0.2f
#define FORCETRIMRELEASE_HOLD_TIME_MS 1000.0f
#define TRIMSYSTEM_SHEARPIN_BREAKINGFORCE_LBS 50.0f
I can't find this practice explained anywhere on the web.
This allows you to override the macros when you're compiling:
gcc -DMACRONAME=value
The definitions in the header file are used as defaults.
As I said in the comment, imagine this situation:
foo.h
#define FOO 4
defs.h
#ifndef FOO
#define FOO 6
#endif
#ifndef BAR
#define BAR 4
#endif
bar.c
#include "foo.h"
#include "defs.h"
#include <stdio.h>
int main(void)
{
printf("%d%d", FOO, BAR);
return 0;
}
Will print 44.
However, if the conditional ifndef was not there, the result would be compilation warnings of MACRO redefinition and it will print 64.
$ gcc -o bar bar.c
In file included from bar.c:2:0:
defs.h:1:0: warning: "FOO" redefined [enabled by default]
#define FOO 6
^
In file included from bar.c:1:0:
foo.h:1:0: note: this is the location of the previous definition
#define FOO 4
^
I do not know the context but this can be used to give the user the availability to override the values set by those macro definitions. If the user explicitly defines a different value for any of those macros it will be used instead of the values used here.
For instance in g++ you can use the -D flag during compilation to pass a value to a macro.
This is done so that the user of the header file can override the definitions from his/her code or from compiler's -D flag.
Any C project resides on multiple source files. When working on a single source file the checks seem to (and actually) have no point, but when working on a large C project, it's a good practice to check for existing defines before defining a constant. The idea is simple: you need the constant in that specific source file, but it may have been already defined in another.
You could think about a framework/library that gives to the user a default preset that allow the user to compile and work on it.
Those defines are spreaded in different files and the final user is advised to include it's config.h file where he can config its values.
If the user forgot some define the system can continue to work because of the preset.
Using
#ifndef BEEPTRIM_PITCH_RATE_DEGPS
#define BEEPTRIM_PITCH_RATE_DEGPS 0.2f
#endif
allows the user to define the value of the macro using the command line argument (in gcc/clang/VS) -DBEEPTRIM_PITCH_RATE_DEGPS=0.3f.
There is another important reason. It is an error to re-define a preprocessor macro differently. See this answer to another SO question. Without the #ifndef check, the compiler should produce an error if -DBEEPTRIM_PITCH_RATE_DEGPS=0.3f is used as a command line argument in the compiler invocation.

What does a #define directive without an argument do?

On Apple's opensource website, the entry for stdarg.h contains the following:
#ifndef _STDARG_H
#ifndef _ANSI_STDARG_H_
#ifndef __need___va_list
#define _STDARG_H
#define _ANSI_STDARG_H_
#endif /* not __need___va_list */
#undef __need___va_list
What do the #define statements do if there's nothing following their first argument?
There are sort of three possible "values" for an identifier in the preprocessor:
Undefined: we don't know about this name.
Defined, but empty: we know about this name, but it has no value.
Defined, with value: we know about this name, and it has a value.
The second, defined but empty, is often used for conditional compilation, where the test is simply for the definedness, but not the value, of an identifier:
#ifdef __cplusplus
// here we know we are C++, and we do not care about which version
#endif
#if __cplusplus >= 199711L
// here we know we have a specific version or later
#endif
#ifndef __cplusplus // or #if !defined(__cplusplus)
// here we know we are not C++
#endif
That's an example with a name that if it is defined will have a value. But there are others, like NDEBUG, which are usually defined with no value at all (-DNDEBUG on the compiler command line, usually).
They define a macro which expands to nothing. It's not very useful if you intended it to be used as a macro, but it's very useful when combined with #ifdef and friends—you can, for example, use it to create an include guard, so when you #include a file multiple times, the guarded contents are included only once.
You define something like:
#define _ANSI_STDARG_H_
so that, later you can check for:
#ifdef _ANSI_STDARG_H_

Using C, one code, multiple headers if/else?

I have one piece of code that I can use for the same function on different sets of data which are defined in different header files . These header files may have the same variable defined differently.
I can pass a parameter to the code when I call it to specify which dataset I want to perform the function on.
What I would like to do is pass this parameter to the code where if the parameter equals X then I use headerX, or if parameter equals Y I use headerY.
It is my understanding that header files must be included before MAIN. Is it possible to include the header file after MAIN so that I can write an if/else statement to determine which header file I am calling?
If I can't do that then please help me figure this out.
You could use #ifdef - blocks to determine which data set you'd want to use before compiling. But if you wanted a different data set, you would need to change (recompile) the executable by changing that define.
Otherwise you would need to compile in C++ as straight C does not support overloaded functions.
Simply put, you just can't. You may be able to include headers before hand based on a condition. Just use #if-def blocks at the top of the file.
But you can't include it like if else:
This is WRONG
if(x == 1)
#include "header1.h"
else
#include "header2.h"
But you can do this at the top of the file:
#if SYSTEM_1
#include "system_1.h"
#elif SYSTEM_2
#include "system_2.h"
#elif SYSTEM_3
#include "system_3.h"
#endif
Or you could just use C++ which does support overloaded functions.
You can do simple metaprogramming by using the macro preprocessing phase. Create a "interface_myFunc.h" with something like
#define FUNCNAME(T) myFunc_ ## T
void FUNCNAME(theType)(theType t);
Create a "implement_myFunc.h" file with something like
void FUNCNAME(theType)(theType t) {
// do something with t
}
and then include this file in another file "myFunc.h"
#define theType toto
#include "interface_myFunc.h"
#undef theType toto
#define theType tutu
#include "interface_myFunc.h"
#undef theType tutu
and similar for the definitions, "myFunc.c"
#define theType toto
#include "implement_myFunc.h"
#undef theType toto
#define theType tutu
#include "implement_myFunc.h"
#undef theType tutu
Modern C, C11, also has ways to create a common interface for all these functions that you create by so-called type generic macros:
#define myFunc(X) \
_Generic((X), \
toto: FUNCNAME(toto), \
tutu: FUNCNAME(tutu) \
)(X)

redefining a constant in C

If I have a constant defined as such in a header file:
#define MY_CONSTANT 1
And I include a library to the whole project which includes a different definition for the same constant:
#define MY_CONSTANT 0
I naturally get conflicts while compiling. Supposing that I cannot alter my project code and that I can only change my library code, what can I do to make MY_CONSTANT as defined by my lib?
EDIT:
just to clarify, my goal is to update a constant in my code through a library. Because I'm writing a library to simulate hardware functions, I have to go by the rule that the software itself must be untouched. There is a loop of sort in the main file that uses the constant. I need to change this constant, but without actually altering it in the main file.
You can undefine the other definition
#ifdef MY_CONSTANT
#undef MY_CONSTANT
#endif
#define MYCONSTANT 0
Also, you should remove the =, and the ;
P.S. as mentioned, it will not change the code that already compiled.
You can #undef MY_CONSTANT and redefine to the value you want, but that's just asking for trouble. See if you can modify the design entirely so that MY_CONSTANTs don't clash.
Undef, redef, and then redef it back
#ifdef MY_CONSTANT
#undef MY_CONSTANT
#endif
#define MY_CONSTANT 0
/* code here */
#undef MY_CONSTANT /* not needed if you don't need the library's definition*/
#include "library.h" /* file that originally defined it
might not work if include guards prevent it
in that case #undef LIBRARY_H
although that causes more trouble :( */

A macros in the C language (#define)

I am reading source code of hoard memory allocator, and in the file of gnuwrapper.cpp, there is the following code
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x)
What's the meaning of CUSTOM_PREFIX(malloc)(x)? is CUSTOM_PREFIX a function? But as a function it didn't defined anywhere. If it's variable, then how can we use variable like var(malloc)(x)?
More code:
#ifndef __GNUC__
#error "This file requires the GNU compiler."
#endif
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#ifndef CUSTOM_PREFIX ==> here looks like it's a variable, so if it doesn't define, then define here.
#define CUSTOM_PREFIX
#endif
#define CUSTOM_MALLOC(x) CUSTOM_PREFIX(malloc)(x) ===> what's the meaning of this?
#define CUSTOM_FREE(x) CUSTOM_PREFIX(free)(x)
#define CUSTOM_REALLOC(x,y) CUSTOM_PREFIX(realloc)(x,y)
#define CUSTOM_MEMALIGN(x,y) CUSTOM_PREFIX(memalign)(x,y)
In your code, since CUSTOM_PREFIX is defined to be nothing, the string CUSTOM_PREFIX(malloc)(x) will expand to
(malloc)(x)
which is equivalent to the usual
malloc(x)
However, the CUSTOM_PREFIX allows the developer to choose a different memory management function. For example, if we define
#define CUSTOM_PREFIX(f) my_##f
then CUSTOM_PREFIX(malloc)(x) will be expanded to
my_malloc(x)
CUSTOM_PREFIX is defined as nothing, so it will just disappear, leaving behind (malloc)(x), which is the same as malloc(x). Why? I don't know. Perhaps other places in the code set CUSTOM_PREFIX to something else.
At a guess, its a macro which changes calls to malloc(x) etc. into something like:
DEBUG_malloc( x );
You can choose to supply the macro yourself, to provide a customised prefix for the functions, or not in which case the names won't be changed.

Resources