Thangaraj
Thangaraj

Reputation: 3184

Linux: Export global variable of executable to be used in my shared library (C language)

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.

Upvotes: 1

Views: 5637

Answers (3)

Top-Master
Top-Master

Reputation: 8751

The int myVariable = 123; syntax should already work, because unlike MSVC, you never need __declspec(dllexport) keywords.

But if you changed compiler's default settings, try setting visibility manually, see example.

Example

In app's (not lib's) header:

// Backward compatible header recurse guard.
#ifndef MY_APP_H
#define MY_APP_H

// MARK: Helpers.
#define MY_APP_EXPORT __attribute__((visibility("default")))

// MARK: Variables.
MY_APP_EXPORT extern int myVariable;
MY_APP_EXPORT extern int myOtherVariable;

#endif // MY_APP_H

In app's (not lib's) source:

#include "my-app.h"

int myVariable = 123;
int myOtherVariable = 786;

int main() {

    // ...

    return 0; // Success.
}

Finally, your lib should not copy-paste the App's declarations, and should instead use App's header directly.

Upvotes: 0

MOHAMED
MOHAMED

Reputation: 43528

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
}

Upvotes: -1

It should work. BTW, your tst.cis lacking a #include <stdio.h>. And its main should return an ìnt and end with e.g. 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

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

 #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 e.g. 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

Upvotes: 3

Related Questions