473,386 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Global variables in Shared Libraries in Linux

Hi,

Can I have a Global variable declared in the application, be used in a
shared library opened through 'dlopen' ? For example:

I have a Library 'libfoo.so' with the following:

libfoo_headerfile.h
=============

extern int my_global_var;
libfoo_foo.c
========
#include "libfoo_headerfile.h"

int
foo (void)
{
my_global_var = 512;
printf("shared_lib %s - my_global_var = %d\n", __FUNCTION__,
my_global_var);
return 10;
}

I have the following in my Application:
============================

app.c
====

#include "libfoo_headerfile.h"

int my_global_var;

int
main ()
{
char *error;
void *handle;
int (*foo_fn_p) (void);

handle = dlopen("libfoo.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
printf("Main - After dlopen - my_global_var = %d\n",
my_global_var);

foo_fn_p = dlsym(handle, "foo");
if ((error = dlerror()) != NULL) {
printf("Error in dlsym");
fputs(error, stderr);
exit(1);
}

printf("foo_fn_p = %u\n", foo_fn_p());
dlclose(handle);
handle = NULL;
printf("Main - after dlclose - shared_lib_var = %d\n",
shared_lib_var);

return 1;
}

Here is my Make File:

all:
gcc -fPIC -g -c libfoo_foo.c
gcc -shared -o libfoo.so libfoo_foo.c
gcc -g -o app app.c -ldl

clean:
rm -f *.o
rm -f *.so
rm -f app

When executing 'app' I am seeing the following error:

/users/nkalyan/test/shared_libraries/libfoo.so: undefined symbol:
my_global_var

Looks like the Shared library is not recognizing 'my_global_var'.
Any ideas

Sep 4 '06 #1
1 5901
ni******@gmail.com wrote:
Hi,

Can I have a Global variable declared in the application, be used in a
shared library opened through 'dlopen' ?
dlopen is not a Standard C function and thus off-topic here. Try
comp.unix.programmer.

Robert Gamble

Sep 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
4
by: Dan Elliott | last post by:
Hello, Converting from a working C program to C++, I run into the following error: I have a header: (header.h) namespace shared{ ... struct X{ ...
17
by: Woody Splawn | last post by:
I am finding that time after time I have instances where I need to access information in a variable that is public. At the same time, the books I read say that one should not use public variables...
0
by: nidumolu | last post by:
Hi, Can I have a Global variable declared in the application, be used in a shared library opened through 'dlopen' ? For example: I have a Library 'libfoo.so' with the following: ...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
15
by: =?Utf-8?B?UGF0Qg==?= | last post by:
Just starting to move to ASP.NET 2.0 and having trouble with the Global.asax code file. In 1.1 I could have a code behind file for the global.asax file. This allow for shared variables of the...
1
by: viraj.kadakia | last post by:
I observe a behavior with shared libraries (.so) and global variables that I cannot understand ... I'd appreciate if someone can explain the behavior .. Scenario 1: aTest is an executable on...
3
by: ankugoe7 | last post by:
Is it possible to share global variables such that all applications which use shared libraries can see the changes made to the global variables by the other applications.
4
by: stuntgoat | last post by:
Hi, I want to start using Python 2.6 and 3000. I have several questions. What, in your experiences, is a functionally elegant solution to installing 2.6 and 3 from source without breaking...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.