Project Home
Project Home
Trackers
Trackers
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - a exception of Shared Library ,help ! help ! help ! : (2 Items)
   
a exception of Shared Library ,help ! help ! help !  
 Now, I have create a executble application and a Shared Library , But when I use the library  I receive a error msg as 
follows:
ldd:FATAL: Could not load library liblibrarytest1_g.so.1

note: The  error  occurs  when I add the Extral  Library for the executble application . but if I don't  add the Extral 
Libray , I  will  receive another error  msg  as  follows:
Shared library is corrupted

my code list:

executble application:

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <dlfcn.h>

void *FunctionLib;
const char *dlError;
int (*Function)();

int main(int argc, char *argv[]) {
	int   rc;             /*  return codes            */
	char HelloMessage[] = "Hello World\n";

	//printf("Welcome to the QNX Momentics IDE\n");
	FunctionLib=dlopen("/home/liblibrarytest1.so",RTLD_LAZY);
	dlError=dlerror();
	printf("  dlTest  3-Open Library with absolute path return-%s- \n", dlError);

	if (dlError) exit(1);
	printf("  dlTest  2-Original message \n");
	Function=dlsym(FunctionLib,"printUPPERCASE");
	dlError=dlerror();
	printf("  dlTest  4-Find symbol printUPPERCASE return-%s- \n", dlError);
	if (dlError) exit(1);
	rc = (*Function)( HelloMessage );
	printf("  dlTest  5-printUPPERCASE return-%s- \n", dlError);
	rc = dlclose(FunctionLib);
	dlError=dlerror();
	printf("  dlTest  7-Open Library with relative path return-%s- \n", dlError);
	return EXIT_SUCCESS;
}

Shared Library:

  #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int printUPPERCASE ( inLine )
char inLine[];
{
   char UPstring[256];
   char *inptr, *outptr;

   inptr = inLine;
   outptr = UPstring;
   while ( *inptr != '\0' )
      *outptr++ = toupper(*inptr++);
  *outptr++ = '\0';
   printf(UPstring);
   return(1);
}

note :  I  have compile and link success with the two project.
Re: a exception of Shared Library ,help ! help ! help !  
On 10-11-24 12:44 AM, li jianfeng wrote:
>
> Now, I have create a executble application and a Shared Library , But
> when I use the library I receive a error msg as follows:
> ldd:FATAL: Could not load library liblibrarytest1_g.so.1

When you explicitly link your executable against the shared object, it 
adds an immediate load dependency (DT_NEEDED) and then the dynamic 
linker will try to load the library when your executable is loaded. It 
looks for the libraries in the default search path; to specify a 
non-default path like /home/.. set the LD_LIBRARY_PATH environment path 
to include the directory that contains your shared object.

> note: The error occurs when I add the Extral Library for the executble
> application . but if I don't add the Extral Libray , I will receive
> another error msg as follows:
> Shared library is corrupted

This error means the dynamic linker was unable to read the ELF header or 
the segment headers. How are you creating your shared object? Also, 
notice that in your immediate load case you are linking against 
liblibrarytest1_g.so.1 but later you are dlopen'ing. 
"/home/liblibrarytest1.so". Please verify the correct shared object is 
on your target and that it is in the right location, and that it is 
compiled for the right CPU variant for your target.

Regards,

Ryan Mansfield