lemonsf
November 13th, 2009, 01:01 PM
Greetings,
Just porting some libs to nix, and I have a problem compiling a dynamic lib... All the code has ported ok, compilation fine, but linking not so ( no punn intended! ). Any help on this would be much appreciated...
My problem is when it comes to linking a static lib that itself links to a dynamic lib...
g++ main.cpp -L./ -lstaticLib -o main
I get the error
.//libstaticLib.a(staticLib.o): In function 'staticFunc(int)':
staticLib.cpp:(.text+0x16): undefined reference to 'dlopen'
staticLib.cpp:(.text+0x39): undefined reference to 'dlsym'
collect2: ld returned 1 exit status
which means it can't find 'dlsym' and 'dlopen'.. fair enough, so I locate the 'libdl.so' which contains these functions, but when I list the symbols...
nm -s /usr/lib/libdl.so
nm: libdl.so: no symbols
This can't be right can it?
Why would there be the 'shell' to the dynamic lib but with nothing in it? I've tried re-installing build-essential but still the same libdl.so...
All the source that I have used, and the commands to compile are below.. In summary main ( main.cpp ) calls "staticFunc(int)" ( in staticLib.cpp which is compiled to libstaticLib.a ), which in turn calls "dynamicFunc(int)" ( in dynamicLib.cpp which is compiled to libdynamicLib.so )... Hope I haven't confused anyone yet...
The code: ( N.B This is code is cross-platform )
main.cpp
#include "liba.h"
#include <iostream>
int main( int argc, char** argv )
{
int result = staticFunc( 1 );
std::cout << "result : " << result;
return 0;
}
staticLib.h
#ifndef STATICLIB_INCLUDE
#define STATICLIB_INCLUDE
int staticFunc( int );
#endif
staticLib.cpp
#include "staticLib.h"
#include "dynamicLib.h"
#ifdef WIN32
#include "windows.h"
#define GETFUNCTION GetProcAddress
#else
#include <dlfcn.h>
#define GETFUNCTION dlsym
#endif
typedef int (*_dynamicFunc)( int );
int staticFunc( int val )
{
#ifdef WIN32
HINSTANCE dInst = LoadLibrary( "dynamicLib.dll" );
#else
void* dInst = dlopen( "./libdynamicLib.so", RTLD_LAZY );
#endif
if ( dInst )
{
_dynamicFunc function = 0;
function = (_dynamicFunc) GETFUNCTION( dInst, "dyanmicFunc" );
if ( function )
return function( val );
}
return 0;
}
dynamicLib.h
#ifndef DYNAMICLIB_INCLUDE
#define DYNAMICLIB_INCLUDE
#ifdef WIN32
#define dllEXPORT __declspec( dllexport )
#else
#define dllEXPORT
#endif
#ifdef __cplusplus
extern "C"
{
#endif
dllEXPORT int dynamicFunc( int );
#ifdef __cplusplus
}
#endif
#endif
dyanmicLib.cpp
#include "dynamicLib.h"
#ifdef WIN32
BOOL WINAPI DllMain( HINSTANCE dllInst, DWORD reason, LPVOID reserved ) { return true; }
#endif
dllEXPORT int dynamicFunc( int value )
{
return ++value;
}
to create the dynamic lib..
g++ -c -fPIC dynamicLib.cpp -o d.o
ld -G d.o -o libdynamicLib.so
which creates libdyanmicLib.so, no problems... then to create the static lib
g++ -c staticLib.cpp -L./ -ldynamicLib -o s.o
ar -rv libstaticLib.a s.o
which creates libstaticLib.a, again no problems...so to compile and link main.cpp
g++ main.cpp -L./ -lstaticLib -o main
from which I get...
.//libstaticLib.a(staticLib.o): In function 'staticFunc(int)':
staticLib.cpp:(.text+0x16): undefined reference to 'dlopen'
staticLib.cpp:(.text+0x39): undefined reference to 'dlsym'
collect2: ld returned 1 exit status
Many thanks in advance for your time, any help on this would be appricated..
Just porting some libs to nix, and I have a problem compiling a dynamic lib... All the code has ported ok, compilation fine, but linking not so ( no punn intended! ). Any help on this would be much appreciated...
My problem is when it comes to linking a static lib that itself links to a dynamic lib...
g++ main.cpp -L./ -lstaticLib -o main
I get the error
.//libstaticLib.a(staticLib.o): In function 'staticFunc(int)':
staticLib.cpp:(.text+0x16): undefined reference to 'dlopen'
staticLib.cpp:(.text+0x39): undefined reference to 'dlsym'
collect2: ld returned 1 exit status
which means it can't find 'dlsym' and 'dlopen'.. fair enough, so I locate the 'libdl.so' which contains these functions, but when I list the symbols...
nm -s /usr/lib/libdl.so
nm: libdl.so: no symbols
This can't be right can it?
Why would there be the 'shell' to the dynamic lib but with nothing in it? I've tried re-installing build-essential but still the same libdl.so...
All the source that I have used, and the commands to compile are below.. In summary main ( main.cpp ) calls "staticFunc(int)" ( in staticLib.cpp which is compiled to libstaticLib.a ), which in turn calls "dynamicFunc(int)" ( in dynamicLib.cpp which is compiled to libdynamicLib.so )... Hope I haven't confused anyone yet...
The code: ( N.B This is code is cross-platform )
main.cpp
#include "liba.h"
#include <iostream>
int main( int argc, char** argv )
{
int result = staticFunc( 1 );
std::cout << "result : " << result;
return 0;
}
staticLib.h
#ifndef STATICLIB_INCLUDE
#define STATICLIB_INCLUDE
int staticFunc( int );
#endif
staticLib.cpp
#include "staticLib.h"
#include "dynamicLib.h"
#ifdef WIN32
#include "windows.h"
#define GETFUNCTION GetProcAddress
#else
#include <dlfcn.h>
#define GETFUNCTION dlsym
#endif
typedef int (*_dynamicFunc)( int );
int staticFunc( int val )
{
#ifdef WIN32
HINSTANCE dInst = LoadLibrary( "dynamicLib.dll" );
#else
void* dInst = dlopen( "./libdynamicLib.so", RTLD_LAZY );
#endif
if ( dInst )
{
_dynamicFunc function = 0;
function = (_dynamicFunc) GETFUNCTION( dInst, "dyanmicFunc" );
if ( function )
return function( val );
}
return 0;
}
dynamicLib.h
#ifndef DYNAMICLIB_INCLUDE
#define DYNAMICLIB_INCLUDE
#ifdef WIN32
#define dllEXPORT __declspec( dllexport )
#else
#define dllEXPORT
#endif
#ifdef __cplusplus
extern "C"
{
#endif
dllEXPORT int dynamicFunc( int );
#ifdef __cplusplus
}
#endif
#endif
dyanmicLib.cpp
#include "dynamicLib.h"
#ifdef WIN32
BOOL WINAPI DllMain( HINSTANCE dllInst, DWORD reason, LPVOID reserved ) { return true; }
#endif
dllEXPORT int dynamicFunc( int value )
{
return ++value;
}
to create the dynamic lib..
g++ -c -fPIC dynamicLib.cpp -o d.o
ld -G d.o -o libdynamicLib.so
which creates libdyanmicLib.so, no problems... then to create the static lib
g++ -c staticLib.cpp -L./ -ldynamicLib -o s.o
ar -rv libstaticLib.a s.o
which creates libstaticLib.a, again no problems...so to compile and link main.cpp
g++ main.cpp -L./ -lstaticLib -o main
from which I get...
.//libstaticLib.a(staticLib.o): In function 'staticFunc(int)':
staticLib.cpp:(.text+0x16): undefined reference to 'dlopen'
staticLib.cpp:(.text+0x39): undefined reference to 'dlsym'
collect2: ld returned 1 exit status
Many thanks in advance for your time, any help on this would be appricated..