请帮助我,如何在proc.so,
中定义的att variable中的act.exe中访问运行时间varibale的数据(att)我创建了proc.so,并与attol.exe和stub.exe链接attol.exe更新" att"变量和stub.exe正在访问" att"变量,并打印att的值。
我使用以下命令来编译代码:
g -wall -c attol.cc proc.cc stub.cc
g -shared -dynamiclib -fpic -o libproc.so proc.o -ldl
g -rdynamic -o attol.exe attol.o/users/hbharti/dlopen/proc/libproc.so -ldl
g -rdynamic -o stub.exe stub.o/users/hbharti/dlopen/proc/libproc.so -ldl
当我在不同的终端运行两个.exe时,然后显示" att:4"值,但stub.exe显示不正确的值,
但是stub.exe应显示" 4"值或更新值。
out put attol.exe:
./attol.exe
att的值是:4
输入ATT的价值:
out put stub.exe:
./stub.exe
att:0
----完整的代码详细信息----
proc.h:
#ifndef __X_H_INCLUDED__
#define __X_H_INCLUDED__
extern int att;
int fun();
#endif
proc.cc:
#include<iostream.h>
#include "proc.h"
int att;
int fun ()
{
att=4;
return 0;
}
上面的代码正在生成proc.o,然后使用以下命令转换为proc.so:
g -wall -c attol.cc proc.cc stub.cc
g -shared -dynamiclib -fpic -o libproc.so proc.o -ldl
attol.cc:
#include <iostream.h>
#include "proc.h"
using namespace std;
int main ()
{
int ch=1;
fun();
cout<<"n Value of att is : "<<att;
do{
cout<<"n Enter the value of att : ";
cin>>att;
cout<<"n Do you want to continue the : ";
cin>>ch;
}while(ch!=0);
return 0;
}
attol.cc文件通过使用以下命令
创建attol.exeg -rdynamic -o attol.exe attol.o/users/hbharti/dlopen/proc/libproc.so-so-ldl
out put:
att的值是:4
输入ATT的价值:
stub.cc:
#include <iostream.h>
#include <dlfcn.h>
int main ()
{
void *handle;
char *error;
handle = dlopen ("/users/hbharti/DLOPEN/proc/libproc.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
int *att =(int*) dlsym(handle, "att");
if ((error = dlerror()) != NULL) {
fputs(error, stderr);
exit(1);
}
cout<<"n Att : " <<*att;
cout<<"n " ;
dlclose(handle);
}
stub.cc文件通过使用以下命令
创建stub.exeg -rdynamic -o stub.exe stub.o/users/hbharti/dlopen/proc/libproc.so-lidl
以代码来判断,核心逻辑似乎存在一个基本问题。
将共享对象(
*.so
)加载到执行过程的内存地址空间中。
但是,它是不是在多个过程中共享的。当2个或多个可执行文件尝试访问相同的共享对象(*.so
)时,它们都将其映射到各自的内存地址空间中。
共享对象(*so
)中的数据(甚至是全球)是不是在2个或多个可执行文件上共享。