libtool
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Issues with dlopen and compile of modules...


From: Ralf Wildenhues
Subject: Re: Issues with dlopen and compile of modules...
Date: Fri, 22 Oct 2004 07:34:21 +0200
User-agent: Mutt/1.5.6+20040722i

* Chris Bowlby wrote on Thu, Oct 21, 2004 at 10:00:33PM CEST:
> Hi All,
> 
>  I've been working on a project for some time now and am working into the 
> system a means of creating dynamic modules that will need to be loaded via 
> dlopen (will use libltdl once I've got my initial testing done - for 
> portability reasons), my sample code is as follows:
> 
> #include <dlfcn.h>
> #include <unistd.h>
> 
> #include <iostream>
> 
>  void main(void) {
>   void (*test_function) (int &);

See below.

>   void *handle;
> 
>   handle = dlopen("libtest/libtest.la", RTLD_NOW);

This will not work.  lt_dlopen can open .la files, dlopen can only open
the corresponding shared objects, most likely called libtest.so (or
similar with version numbers) on your system.

>   if (!handle) {
>    cerr << dlerror() << "\n";

This will not work in ISO C++.
   using namespace std;
or similar will be necessary.

>    exit(EXIT_FAILURE);

Better include stdlib.h (or, FWIW, cstdlib) for exit().

>   }
> 
> //  test_function = dlsym(handle, "test_fun");
> 
>   dlclose(handle);
>  }
> 
>  There are two issues that I've come up with, during compile time with the:
> 
>   test_function = dlsym(handle, "test_fun");
> 
>  uncommented it complains about:
> 
> test.cxx: In function 'void main(void)':
> test.cxx:17: ANSI C++ forbids implicit conversion from 'void *' in 
> assignment

This has nothing to do with libtool, and everything with C++ type
safety.  You have to cast the return value of dlsym to the correct type.

>  The second issue is that dlopen (when I get the system to compile the 
> program by commenting out the dlsym line) complains that libtest/libtest.la 
> is not of the right format, I'm compiling it using:
>
> lib_LTLIBRARIES: libtest.la

Typo: you want

  lib_LTLIBRARIES = libtest.la

> libtest_la_SOURCES = test.cxx
> libtest_la_LDFLAGS = -module


The last and (to me) most interesting issue: apart from the fact that in
C, already conversion from void pointer to function pointer is
undefined, is there any ISO C++ reason against conversion to a C++
function pointer (like test_function above)?  I don't think so, but I'm
not really sure.

Regards,
Ralf




reply via email to

[Prev in Thread] Current Thread [Next in Thread]