GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Nov-2008, 06:49
spooshah spooshah is offline
New Member
 
Join Date: Oct 2008
Posts: 15
spooshah is an unknown quantity at this point

Segmentation Fault ... Shared Libraries in Linux




Halo 'World'.

I am trying to make a simple program of shared libraries on a LInux platform.
I am getting an error of segmentation fault when I try to execute the program.
It is compiling without any warnings and errors.

I know that segmentation faults are normally caused by pointer malfunctions and that is probably the case over here.

However I am not able to figure it out.
Will be very greatful if you can help me.

Am posting the programs here.

MainExample.C
CPP / C++ / C Code:
#include "header.h"
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <string.h>

void *handle;
void (*F_PRODRATES) ( double *, double *)=NULL;
char *error;

int DLL::rif_ch_attachdll_(char* LibMech , int thisLength)
{
	char buffer[1024];
	int two;
	
	strcpy(buffer,LibMech/*, (size_t)lejo*/);
	buffer[thisLength]='\0';          /* need a null terminated string */
	
	two = 2;
	
	handle = dlopen (buffer, RTLD_LAZY);		      
   	 if (!handle) {					      
		fprintf (stderr, "%s\n", dlerror());		      
	printf(" NOT attached to DLL \n .... %s .....\n",buffer);
 
                _exit(-1);
		return -1; 					      
    }
		
	printf(" successfully attached to DLL \t %s .\n",buffer);
 
	return 0;
}

void DLL::dlltest_(double* X , double* Y)
{
	if (handle == NULL) {
		fprintf (stderr, "%s\n", "no DLL attached, error");
		_Exit(-1);
		 return ;
	}
		
	if ( (*F_PRODRATES) == NULL) {
		dlerror();
	     *(void **) (&F_PRODRATES) = dlsym(handle, "test_");
	     if ((error = dlerror()) != NULL)  {                       
                  fprintf (stderr, "%s\n", error);  
             }
        }
        (*F_PRODRATES)(X , Y);	

}

int main()
{
	
	double  X,Y=0;
	char LibMech[1024];
	int thisLength;
	DLL call;
	
	strcpy(LibMech, "home/spoo0/FinalLinking/lib1Fortran.so");
/*	printf("%s",LibMech);*/
	
	thisLength = strlen(LibMech);
/*	printf("hgfhjg %s",LibMech);
	      if(thisLength < 1) 
	      {
	      thisLength= sizeof(LibMech);
	      }*/
	      
                call.rif_ch_attachdll_(LibMech , thisLength);
	
		printf("I am in Dll 12345");
		call.dlltest_(&X,&Y);
		
}

header.h
CPP / C++ / C Code:
#ifndef HEADER_H
#define HEADER_H
#include <cstdio> 
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <csignal>
#include <dlfcn.h>

#ifdef  __cplusplus
extern "C" {
#endif

class DLL {
public:
DLL();
~DLL();
int rif_ch_attachdll_(char* LibMech , int thisLength);
void dlltest_(double* X , double* Y);

private:
char* LibMech;
int thisLength;
double* X;
double* Y;
};
#ifdef  __cplusplus
}
#endif

#endif 


Just in case you are wondering why I am using such a format of header.h and everything. It is only because I am making this model to incorporate in a much larger program and this is the format I have to use.


This is my small Makefile.

g++ -c -fPIC -shared MainExample.C
g++ -fPIC -shared -o exec MainExample.o
./exec

Will gladly provide any more information that maybe require to solve this.

Thank You
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I have a segmentation fault in my project takachi C++ Forum 6 09-Mar-2008 23:30
How To Remove Linux And Install Windows? rockaway Computer Software Forum - Windows 3 06-Mar-2008 22:00
Shared Libraries with qmake and Exception Handling PiJ C++ Forum 3 18-Dec-2007 10:29
Super Cheap PHP 5, RoundCube Webmail Linux Shared Hosting hostingatoz Web Hosting Advertisements & Offers 0 04-Aug-2007 03:46
$1.25/mo, PHP 5, 250 MB Space, RoundCube Webmail Linux Shared Hosting hostingatoz Web Hosting Advertisements & Offers 0 25-May-2007 01:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.