简体   繁体   中英

How to access the global variable of executable in shared library (c - linux)

I want to access the global variable of executable in shared library? I have tried to compile using option -export-dynamic but no luck.

I have tried with extern key word. this also not working.

Any help or suggestion would be appreciable.

Environment c - Linux

executable:- tst.c

int tstVar = 5;

void main(){
funInso();
    printf("tstVar %d", tstVar);
}

lib:- tstLib.c

extern int tstVar;

void funInso(){
   tstVar = 50;
}

Since my code is very big, I just gave the sample which I have used in my program.

It should work. BTW, your tst.c is lacking a #include <stdio.h> . And its main should return an ìnt and end with eg return 0; .

With

/* file tst.c */
#include <stdio.h>
int tstVar = 5;
extern void funInso(void);

int main(){
  funInso();
  printf("tstVar %d\n", tstVar);
  return 0;
}

and

/* file tstlib.c */
extern int tstVar;

void funInso(){
   tstVar = 50;
}

I compiled with gcc -Wall -c tst.c the first file, I compiled with gcc -Wall -c tstlib.c the second file. I made it a library with

 ar r libtst.a tstlib.o
 ranlib libtst.a

Then I linked the first file to the library with gcc -Wall tst.o -L. -ltst -o tst gcc -Wall tst.o -L. -ltst -o tst

The common practice is to have with your library a header file tstlib.h which would contain eg

 #ifndef TSTLIB_H_
 #define TSTLIB_H_
 /* a useful explanation about tstVar.  */
 extern int tstVar;

 /* the role of funInso. */
 extern void funInso(void);
 #endif /*TSTLIB_H */

and have both tst.c and tstlib.c contain an #include "tstlib.h"

If the library is shared, you should

  1. compile the library file in position independent code mode

     gcc -Wall -fpic -c tstlib.c -o tstlib.pic.o 
  2. link the library with -shared

     gcc -shared tstlib.pic.o -o libtst.so 

    Note that you can link a shared object with other libraries. You could have appended -lgdbm to that command, if your tstlib.c is eg calling gdbm_open hence including <gdbm.h> . This is one of the many features shared libraries give you that static libraries don't.

  3. link the executable with -rdynamic

     gcc -rdynamic tst.o -L. -ltst -o tst 

Please take time to read the Program Library Howto

your tstVar variable could be defined in the lib. and you can share this variable via functions: setFunction : to edit this variable

void setFunction (int v)
{
    tstVar = v;
}

getFunction : to return the variable

int getFunction ()
{
    return tstVar
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM