Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

#define not propagating to a C header file

Tags:

c

I am trying to compile the code from the following link to print the backtrace when a signal is generated:

http://www.linuxjournal.com/article/6391?page=0,1 (from article http://www.linuxjournal.com/files/linuxjournal.com/linuxjournal/articles/063/6391/6391l3.html)

I made the necessary changes (REG_EIP -> REG_RIP). I also changed "#include <ucontext.h>" to "#include <sys/ucontext.h>" to debug my issue which I will explain below.

The top of the file is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <execinfo.h>

/* get REG_EIP from ucontext.h */
#define __USE_GNU
#include <sys/ucontext.h>

...

With the code as is, I get the following error:

# gcc ./st2.c -rdynamic  -o st2
./st2.c: In function ‘bt_sighandler’:
./st2.c:22: error: ‘REG_RIP’ undeclared (first use in this function)
./st2.c:22: error: (Each undeclared identifier is reported only once
./st2.c:22: error: for each function it appears in.)

However, when I copy the line "#define __USE_GNU" to the top of "/usr/include/sys/ucontext.h" (which I know is a very bad idea and is only temporary) as follows:

#ifndef _SYS_UCONTEXT_H                                                            
#define _SYS_UCONTEXT_H 1

#define __USE_GNU  
#include <features.h>
#include <signal.h>                                                                
#include <bits/wordsize.h>

............

#endif // _SYS_UCONTEXT_H

My program compiles and run correctly.

I am baffled why the #define in my program does not "flow" into the header file "sys/ucontext.h", and adding #define directly into sys/ucontext.h makes a difference. Any help would be very appreciated.j

Thank you, Ahmed.

like image 458
Ahmed A Avatar asked Jan 25 '26 13:01

Ahmed A


2 Answers

Figured it out. ucontext.h is included from signal.h, and since __USE_GNU is not defined by the inclusion time, REG_RIP does not get defined. Doing an addition #include <ucontext.h> in my C file had no effect.

Adding the line "#define __USE_GNU" right after #include <stdio.h> solved the issue.

#include <stdio.h>
#define __USE_GNU
#include <stdlib.h>
#include <signal.h>
#include <execinfo.h>

Adding the #define before stdio.h does not help, as stdio.h includes features.h which undef __USE_GNU

Thanks to everybody for your help.

like image 136
Ahmed A Avatar answered Jan 28 '26 03:01

Ahmed A


I suspect something else is #including sys/ucontext.h before your #include gets to it.

The protection in the header file (#ifndef _SYS_UCONTEXT_H, #define _SYS_UCONTEXT_H) prevents the header file from being #included multiple times. If this file has already been included before your #define __USE_GNU, it will have no effect.

Does it compile if you move your #define to the top of your C file?

like image 22
MatthewD Avatar answered Jan 28 '26 03:01

MatthewD



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!