Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Macro for function declaration not working

Tags:

c

macros

keil

I want to use a macro in order to easily change a function declaration, here is what I have for now :

#define MYDECLARATION(name)   void name (void)

When I call MYDECLARATION(my_thread); I get an error:

identifier-list paramters may only be used in a function definition.

I tried to use ## like that :

#define MYDECLARATION(name)   void  ##name (void)

but I am pretty sure I will get : voidmythread (void) in my code. Do you have any idea on how to do it ?

I am also interested if you know some nice tutorials about macros in general.


In response to bitmask comment :

I am using KEIL compiler then my thread are working as follow :

 #define MYDECLARATION(name)   __task void name(void)

My call :

 MYDECLARATION(Mythread); 

My definition :

__task void Mythread(void)
{
  //...
}

New test :

#define RET_TEST     __task void
#define PARAMETER    void

 RET_TEST MYDECLARATION(PARAMETER);

This is working... So I guess it's the fact to use a macro parameter into a function name which is not working...

like image 953
Joze Avatar asked Sep 23 '26 22:09

Joze


1 Answers

The best way to understand the pre-processing output is to use the -E option of gcc.

Apparently, I copied your program.

#define MYDECLARATION(name)    void name (void)

#include "stdio.h"


void my_thread()
{
  printf("hello world\r\n");    
    
}


int main(int argc, char **argv)
{

  MYDECLARATION(my_thread);
  return 0;
}

Now, $gcc -E example.c

int main(int argc, char **argv)
{

  void my_thread (void);
  return 0;
}

You know, you can't call the function like that. It should be called as my_thread(); I did a change in your macro - #define MYDECLARATION(name) name() It works fine. Hope this helps you.

like image 64
dexterous Avatar answered Sep 26 '26 12:09

dexterous



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!