- 论坛徽章:
- 0
|
先谢过大家看我的贴子.我遇到的问题如下:
我用了一个测试程序,测试UNIX下的shared object的调用.这个程序在cygwin/linux/
unixware下都调用成功.就是在SUN OS5.8里不行.各位请看看是怎么回事.
-----------------------------------------------------------------------------------------
dlTest.c (主程序)
-----------------------------------------------------------------------------------------
/**********************************************************/
/* Test UNIX Dynamic Function Loading */
/* */
/* void *dlopen(const char *filename, int flag) */
/* Opens dynamic library and return handle */
/* */
/* const char *dlerror(void) */
/* Returns string describing the last error. */
/* */
/* void *dlsym(void *handle, char *symbol) */
/* Return pointer to symbol\'s load point. */
/* If symbol is undefined, NULL is returned. */
/* */
/* int dlclose (void *handle) */
/* Close the dynamic library handle. */
/* */
/**********************************************************/
#include <stdio.h>
#include <stdlib.h>
/* 1-dll include file and variables */
#include <dlfcn.h>
#include <link.h>
void *FunctionLib; /* Handle to shared lib file */
int (*Function)(); /* Pointer to loaded routine */
const char *dlError; /* Pointer to error string */
int main( int argc, char* argv )
{
int rc; /* return codes */
char HelloMessage[] = \"HeLlO WoRlD\\n\";
/* 2-print the original message */
printf(\" dlTest 2-Original message \\n\" ;
printf(\"%s\", HelloMessage);
/* 3-Open Dynamic Loadable Libary with absolute path */
FunctionLib = dlopen(\"/eitop/switch/test/so/bin/UPPERCASE.so\", RTLD_LAZY);
dlError = dlerror();
printf(\" dlTest 3-Open Library with absolute path return-%s- \\n\", dlError);
if( dlError ) exit(1);
/* 4-Find the first loaded function */
Function = dlsym( FunctionLib, \"printUPPERCASE\" ;
dlError = dlerror();
printf(\" dlTest 4-Find symbol printUPPERCASE return-%s- \\n\", dlError);
if( dlError ) exit(1);
/* 5-Execute the first loaded function */
rc = (*Function)( HelloMessage );
printf(\" dlTest 5-printUPPERCASE return-%s- \\n\", dlError);
/* 6-Close the shared library handle */
/* Note: after the dlclose, \"printUPPERCASE\" is not loaded */
rc = dlclose(FunctionLib);
dlError = dlerror();
printf(\" dlTest 6-Close handle return-%s-\\n\",dlError);
if( rc ) exit(1);
/* 7-Open Dynamic Loadable Libary using LD_LIBRARY path */
FunctionLib = dlopen(\"lowercase.so\",RTLD_LAZY);
dlError = dlerror();
printf(\" dlTest 7-Open Library with relative path return-%s- \\n\", dlError);
if( dlError ) exit(1);
/* 8-Find the second loaded function */
Function = dlsym( FunctionLib, \"printLowercase\" ;
dlError = dlerror();
printf(\" dlTest 8-Find symbol printLowercase return-%s- \\n\", dlError);
if( dlError ) exit(1);
/* 8-execute the second loaded function */
rc = (*Function)( HelloMessage );
printf(\" dlTest 9-printLowercase return-%s- \\n\", dlError);
/* 10-Close the shared library handle */
rc = dlclose(FunctionLib);
dlError = dlerror();
printf(\" dlTest 10-Close handle return-%s-\\n\",dlError);
if( rc ) exit(1);
return(0);
}
-----------------------------------------------------------------------------------------
lowercase.c (被编译成lowercase.so的程序)
-----------------------------------------------------------------------------------------
/********************************************/
/* Function to print input string as lower case. */
/* Returns 2. */
/******************************************* */
int printLowercase( inLine )
char inLine[];
{
char lowstring[256];
char *inptr, *outptr;
inptr = inLine;
outptr = lowstring;
while ( *inptr != \'\\0\' )
*outptr++ = tolower(*inptr++);
*outptr++ = \'\\0\';
printf(lowstring);
return(2);
}
-----------------------------------------------------------------------------------------
UPPERCASE.c (被编译成UPPERCASE.so的程序)
-----------------------------------------------------------------------------------------
/************************************************/
/* Function to print input string as UPPER case. */
/* Returns 1. */
/*********************************************** */
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);
}
-----------------------------------------------------------------------------------------
Makefile
-----------------------------------------------------------------------------------------
########################################################
# test shared object in solaris
#
WORKDIR = $(HOME)/test/so
INCDIR = $(WORKDIR)
LIBDIR = /usr/local/lib
DLLDIR = $(WORKDIR)/bin
BINDIR = $(WORKDIR)/bin
USRINCL = -I$(INCDIR)
USRLIBS = -L$(LIBDIR) -ldl -lm
CC = gcc
CPP = gcc
CCFLAGS = -c
DLLFLAGS = -fPIC
MKDLL = $(CC) -fPIC -shared -lc
DELE = rm -f
ALL:clean dlTest UPPERCASE.so lowercase.so cleantmp
clean:
$(DELE) *.o
$(DELE) $(DLLDIR)/*.so
$(DELE) $(BINDIR)/dlTest
cleantmp:
$(DELE) *.o
dlTest.o:
$(CC) $(CCFLAGS) dlTest.c
lowercase.o:
$(CC) $(CCFLAGS) $(DLLFLAGS) lowercase.c
UPPERCASE.o:
$(CC) $(CCFLAGS) $(DLLFLAGS) UPPERCASE.c
lowercase.so:lowercase.o
$(MKDLL) -o $(DLLDIR)/lowercase.so lowercase.o
UPPERCASE.so:UPPERCASE.o
$(MKDLL) -o $(DLLDIR)/UPPERCASE.so UPPERCASE.o
dlTest:dlTest.o
$(CC) -o $(BINDIR)/dlTest $(USRLIBS) $(USRINCL) dlTest.o
-------------------------------------------------------------------------------------------
问题:
在Solaris下用gcc 编译后.dlTest程序每当运行到dlopen时就core dump了.
为什么?
再次谢过了. |
|