I’m actually porting a Linux app to Mac OS X. My problem is that I can access the variables but not the pointers.
I declared the variables in the main.h this way:
uint64_t test;
uint64_t *test2;
In the mylib.h:
extern uint64_t test;
extern uint64_t *test2;
and in mylib.c I access the variables this way:
printf("%llu\n",test);
printf("%llu\n",*test2);
The first printf() doesn’t have any problems but the second gives me this error:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000008
Does anybody know why this happened?
My ggc command line has the following flags:
gcc -Wall -g -fPIC -c main.c gcc -shared -Wl,-undefined,dynamic_lookup -o mylib.so mylib.o
Just for someone with a similar problem:
I decided to work with getter & setter because there wasn’t problems to access the function of the main program with a dynamic library just the variables.
in mylib.c :
in main.c :
Update
I finally found a way around this getter & setter. The problem you are facing is that you’re defining your variable twice, in my case this happened during a misunderstanding of the
externkeyword. To solve your problem you need to declare the variables as extern in yourmain.he.g.:Next step is to define the variables in your
main.ce.g.:And finally remove the extern declaration from your
mylib.hThe following SO post led to my solution: link