0

Is it possible to include a pre-defined id in an identifier?

The header:

/* myfile.h */
#define func MYID##_func
static void func() {}
#undef func

The c file:

/* myfile.c */
#define MYID FOO
#include "myfile.h"
#undef MYID

#define MYID BAR
#include "myfile.h"
#undef MYID

/* check the functions exist */
void main()
{
    FOO_func();
    BAR_func();
}

My aim here is to create 2 functions: (FOO_func, BAR_func).


I tried to compile the code above, and the functions are identified as MYID_func (which fails since there are 2 of them).

I could do:

#define func FOO_func
#include "myfile.h"
#undef func

... But this doesn't scale so nicely when the file has many identifiers, since they would all have to be defined first.


The goal is to be able to avoid naming collisions when the file is included multiple times in the same C file.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
  • 1
    It's far easier to try it and see what happens, than it is to post a question asking about it and waiting for responses. – mah Jun 09 '15 at 17:51
  • I am trying, and for some reason its not working. – ideasman42 Jun 09 '15 at 17:51
  • So ask why it is not working instead. But first describe how it is not working.. – Eugene Sh. Jun 09 '15 at 17:52
  • If it's not working, asking "is it possible" isn't going to help you if (when, in this case) the answer is "yes, it's possible". Instead, post the question that actually addresses your need: post a simple example demonstrating the problem you're having so people can set you straight. – mah Jun 09 '15 at 17:52
  • 3
    But I would do if differently: `#define func_a(id) id##_func_a`. And avoid multiple inclusion mess. – Eugene Sh. Jun 09 '15 at 17:56
  • 1
    Including a function definition in a header is highly unusual. What is the real problem that you are trying to solve? Of course I don't mean the problem of multiple redefinitions, because this problem is a consequence of your choice of a solution to some other problem. – Sergey Kalinichenko Jun 09 '15 at 17:59
  • The real problem Im trying to solve - is I have a linklist merge-sort function (in a header), I want to avoid copy-pasting code twice, by ifdef'ing the inclusion of an extra argument (for `qsort_r` style callback), however there are some static functions and a struct which will have naming collisions. – ideasman42 Jun 09 '15 at 18:02
  • http://stackoverflow.com/a/217181/12711 – Michael Burr Jun 09 '15 at 18:48
  • @Michael Burr, while answers are the same, title isn't very helpful in discovering - **"Using ## in macros"** – ideasman42 Jun 09 '15 at 18:55

2 Answers2

4

You can use couple of layers of macros to accomplish what you want to.

/* myfile.h */
#define CONCAT2(A, B) A ## B
#define CONCAT(A, B) CONCAT2(A, B)
#define func() CONCAT(MYID, _func)()
static void func() {}
#undef func

You need the extra layers to make sure that MYID is expanded as a macro.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
2

The operator ## don't expand macros. If you want to do that use an extra define:

#define CONCAT_AUX(MACRO_ARG1, MACRO_ARG2) MACRO_ARG1 ## MACRO_ARG2
#define CONCAT(MACRO_ARG1, MACRO_ARG2) CONCAT_AUX(MACRO_ARG1, MACRO_ARG2)

#define func CONCAT(MYID, _func)
Mabus
  • 1,418
  • 12
  • 20