3

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...

Joze
  • 1,285
  • 4
  • 19
  • 33
  • What do you want to easily change about the function declaration? What its arguments are? Its return type? Its calling convention? ...? – Sneftel Oct 04 '13 at 15:27
  • 2
    Are you missing a semicolon at the end of the macro? Try `#define MYDECLARATION(name) void name (void);` – SheetJS Oct 04 '13 at 15:28
  • My aim it's to get a generic code between 2 different architecture. My problem is the syntax of the thread declaration. – Joze Oct 04 '13 at 15:30
  • The semicolon will be added when I gonna use the MACRO : MYDECLARATION(my_thread); (I just added it in the topic) – Joze Oct 04 '13 at 15:31
  • The error must be somewhere else. Probably you are not showing what causes you trouble. My suspicion would be that you have a space between `MYDECLARATION` and `(name)`. – Jens Gustedt Oct 04 '13 at 15:34
  • 2
    [Works like a charm](http://ideone.com/gbb1XO). You're not telling us something relevant. Show us the actual problem. – bitmask Oct 04 '13 at 15:34
  • @JensGustedt No, there is no space. – Joze Oct 04 '13 at 15:36
  • @bitmask I have updated with more info. – Joze Oct 04 '13 at 15:43
  • 1
    Does the compiler error actually contain `paramters`? Or did you write it by memory? – Shahbaz Oct 04 '13 at 15:45
  • 1
    If you write the last declaration (`__task void Mythread(void) { /* ... */ }`) without a macro, does the compiler still complain, or does it work ok? – Shahbaz Oct 04 '13 at 15:47
  • Probably there's nothing wrong with the macro but..See [here](http://stackoverflow.com/questions/11115795/error-92-identifier-list-parameters-may-only-be-used-in-a-function-definition). –  Oct 04 '13 at 15:50
  • Then it's `keil` specific because (apart from the `__task` thing) it's perfectly valid C. – bitmask Oct 04 '13 at 15:51
  • 1
    Is __task a precompiler macro? And if so, does the pre compiler you are using do multiple passes? If not, the __task macro may not be getting run. – DAhrens Oct 04 '13 at 16:06
  • 2
    `gcc` has an `-E` option to output the preprocessed code. Maybe you can do the same with `keil`. Then you can figure out what the preprocessor produces from your code. – bitmask Oct 04 '13 at 16:10
  • One more edit in my post with more tests... – Joze Oct 04 '13 at 16:13

1 Answers1

1

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.

lnogueir
  • 1,859
  • 2
  • 10
  • 21
dexterous
  • 6,422
  • 12
  • 51
  • 99