Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

449
Vistas
How can I use a macro inside another macro in order to concat the definition of the macro to something? C/C++

I am trying to make a macro that will be take a predefined prefix and concatenate it to the wanted function name in the argument of the macro, but I have been unsuccessful.

For example, while this is the wanted function signature:

void somePrefix__someFunc(int a)

However, when I run it and check the symbol table of the program I end up with the expansion of:

FUNC_PREFIXsomeFunc

What am I doing wrong and how can I do this correctly?

The code I was trying to run:

#include <iostream>
#define CONCAT(A, B) A##B
#define FUNC_PREFIX somePrefix__
#define FUNC_NAME(_func) CONCAT(FUNC_PREFIX, _func)

void FUNC_NAME(someFunc)(int a)
{
    std::cout << a << std::endl;
}

int main()
{
    FUNC_NAME(someFunc)(2);
    return 0;
}

P.S: I tried using the macro without a CONCAT wrapper (directly trying to concat FUNC_PREFIX##_func), but it still did not work.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The ## is always applied before the macro "replacement list" is (re)scanned, so in case CONCAT is expanded before the other items, we won't get the desired result. Whenever we end up with a ## in the replacement list during macro replacement, it is then applied before any further replacement.

So you need an additional helper macro to expand the FUNC_PREFIX, so that it is expanded before CONCAT is:

#include <stdio.h>
#define CONCAT(A, B) A##B
#define CONCAT_EXPAND(A, B) CONCAT(A,B)
#define FUNC_PREFIX somePrefix_
#define FUNC_NAME(func) CONCAT_EXPAND(FUNC_PREFIX, func)

void FUNC_NAME(someFunc)(int a)
{
    printf("%s: %d\n",__func__, a);
}

int main()
{
    FUNC_NAME(someFunc)(2);
    return 0;
}

Output:

somePrefix_someFunc: 2
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda