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

182
Vistas
How to tell gcc to keep my unused labels?

Edit: Thanks to @NateEldredge, I better defined my question in How to 'tag' a location in a C source file for a later breakpoint definition?


I use those labels to setup breakpoints in gdb. So no matter if I add/remove lines of code after/before the label, the breakpoint is still correct.

If I add -Wno-error=unused-label to the compilation options, the compiler does not yell at me, but the label disappears from the assembly.

If instead, I use __attribute__((unused)) in the code, the result is the same: no complain, but the label is gone.

Is there a correct way of getting this done (instead of just a hack)?

Here is my toy example:

int main(void){
    int a = 15;
 label: __attribute__((unused))
    a = a + 23;
    return a;
}

After compilation, it results in:

main:
        push    ebp
        mov     ebp, esp
        sub     esp, 16
        mov     DWORD PTR [ebp-4], 15
        add     DWORD PTR [ebp-4], 23
        mov     eax, DWORD PTR [ebp-4]
        leave
        ret

Here an interactive version of the same example: https://godbolt.org/z/zTqd9bM6q


$ gcc --version
gcc (GCC) 10.3.1 20210422 (Red Hat 10.3.1-1)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

If you want to have a label that doesn't get removed or renamed, try this:

    asm volatile("mylabel:");

Note that having this label might affect how GCC optimizes your function. However, the volatile keyword will probably help prevent it from doing anything that would cause problems.

Also note that you can use __asm__ instead of asm. Both appear to work in GCC.

over 4 years ago · Santiago Trujillo Denunciar

0

You can silence the unused label warning... by using it:

int main(int argc, char** argv) {
    int a = 15;
    goto label; label:
    a = a + 23;
    return a;
}

This keeps the label in the assembly (albeit using an internal name):

main:
.LFB0:
        push    ebp
        mov     ebp, esp
        sub     esp, 16
        mov     DWORD PTR [ebp-4], 15
        nop
.L2:
        add     DWORD PTR [ebp-4], 23
        mov     eax, DWORD PTR [ebp-4]
        leave
        ret

I would suggest using a macro to reduce effort and show intent better:

#define DEBUG_LABEL(x) goto x; x:
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