Find the answer to your Linux question:
Results 1 to 3 of 3
Hi lepalom, This is Phani again.I am repeating the same problem regarding loading of shared objects in linux during runtime. I am giving a sample code which is working fine ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Posts
    5

    problem in loading a shared object in linux


    Hi lepalom,

    This is Phani again.I am repeating the same problem regarding loading of shared objects in linux during runtime.

    I am giving a sample code which is working fine in both solaris and linux.But dufing runtime in linux the dlopen() is unable go open the shared object.But in Solaris during runtime it is working fine.

    Test_Binary.c
    -------------
    #include <stdio.h>

    void output(char *name);

    int main()
    {
    char *name;
    name = (char*)malloc(sizeof(char)*20);
    strcpy(name,"Hello World");
    output(name);
    return 0;
    }


    Test_shared.c
    -------------


    #include <dlfcn.h>

    void output( char *name);


    int main(int argc, char **argv) {
    void *handle;
    double (*cosine)(double);
    char *error;
    printf("started program new ");
    handle = dlopen ("/home/tcsrasi/test1.so", RTLD_LAZY|RTLD_GLOBAL);
    if (!handle) {
    printf("\ndlopen error\n");
    fprintf (stderr, "%s\n", dlerror());
    exit(1);
    }

    cosine = dlsym(handle, "main");
    if ((error = dlerror()) != NULL) {
    printf("dlopen error\n");
    fprintf (stderr, "%s\n", error);
    exit(1);
    }

    printf ("%f\n", (*cosine)(2.0));
    dlclose(handle);
    return 0;
    }

    void output(char* name) {
    printf( "%s",name);
    }



    The above program has a function output() which is defined in the same file.

    Now I compile the above Test_binary.c to get the Test_binary.o.

    Then I create the shared object by using the above Test_binary.o.

    Now when I compile the Test_shared.c and run the .out file I get an error saying that " undefined symbol " which is not the case when I run the same in Solaris.

    I just want to know whether the dlopen() performs the same operations in both
    solaris and Linux ,if so, then what is the main cause of the problem.

    compiler options in solaris:gcc -G Test_binary.so -o Test_binary.o
    compiler options in Linux : gcc -fPIC -shared --export-dynamic Test_binary.so -o Test_binary.o.


    The argument for mode in dlopen() for solaris are RTLD_LAZY|RTLD_GLOBAL.
    The same options have been included in Linux also.I have also given all the possible combinations but still I get the same problem.

    Is there any method for opening a shared object in Linux.


    Could you help me out by giving some pointers regarding this.

    Regards
    Phani Kumar.

  2. #2
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    Hi

    I tried out your program. I figured out your problem too why you are getting the undefined symbol when you run the output file

    Here is what I did.

    Test_shared.c
    Code:
    #include <stdio.h>
    #include <dlfcn.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    
    
    void output&#40; char *name&#41;;
    
    int main&#40;int argc, char **argv&#41;
    &#123;
    	void *handle;
    	double &#40;*cosine&#41;&#40;double&#41;;
    	char *error;
    	int fp;
    
    	struct stat buf;
    	memset&#40;&buf,0,sizeof&#40;struct stat&#41;&#41;;
    
    	printf&#40;"Started program new\n "&#41;;
    
    	fp=stat&#40;"/usr/macubex/Test_Binary.so",&buf&#41;;
    	if&#40;fp == -1&#41;
    	&#123;
    		printf&#40;"File not present"&#41;;
    	&#125;
    	else
    	&#123;
    		printf&#40;"File is present \n"&#41;;
    	&#125;
    
    	handle = dlopen &#40;"/usr/macubex/Test_Binary.so", RTLD_NOW | RTLD_GLOBAL&#41;;
    	if &#40;!handle&#41;
    	&#123;
    		printf&#40;"\ndlopen error\n"&#41;;
    		fprintf &#40;stderr, "%s\n", dlerror&#40;&#41;&#41;;
    		exit&#40;1&#41;;
    	&#125;
    
    	cosine = dlsym&#40;handle, "main"&#41;;
    	if &#40;&#40;error = dlerror&#40;&#41;&#41; != NULL&#41;
    	&#123;
    		printf&#40;"dlopen error\n"&#41;;
    		fprintf &#40;stderr, "%s\n", error&#41;;
    		exit&#40;1&#41;;
    	&#125;
    
    	printf &#40;"%f\n", &#40;*cosine&#41;&#40;2.0&#41;&#41;;
    	dlclose&#40;handle&#41;;
    	return 0;
    &#125;
    
    void output&#40;char* name&#41;
    &#123;
    	printf&#40; "\n%s\n",name&#41;;
    &#125;

    Test_Binary.c

    Code:
    #include <stdio.h> 
    
    void output&#40;char *name&#41;; 
    
    int main&#40;&#41; 
    &#123; 
    	char *name; 
    	
    	name = &#40;char*&#41;malloc&#40;sizeof&#40;char&#41;*20&#41;; 
    	strcpy&#40;name,"Hello World"&#41;; 
    	output&#40;name&#41;; 
    	if&#40;name&#41;
    		free&#40;name&#41;;
    	return 0; 
    &#125;
    The commands I gave to make the file are as follows

    gcc -c Test_shared.c
    gcc -c Test_Binary.c
    gcc -shared -fpic -o Test_Binary.so Test_Binary.o
    gcc -o Test_shared Test_shared.o -lc -ldl -rdynamic

    Since you used the --export-dynamic option you were getting that error
    I changed it to the -rdynamic option out of past experience and I was able to run the program

    You can check out what the -rdynamic option is used for .....

  3. #3
    Linux Newbie
    Join Date
    May 2005
    Location
    Chennai,TamilNadu, India
    Posts
    141
    Which TCS are you working? In chennai? If then which branch?

    Just thought from your username

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •