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

Re: dlsym() and C++



Quoting Dennis Payne (payned@rpi.edu):

> Looks like I was right.  Although that doesn't explain why your code was
> crashing.

I've checked the code in ClanLib, and the actual cout did print the address
of the pointer (this time it is a paste, and not a rewrite of the main):

int main(int argc, char **argv)
{
    timeval tv;
    gettimeofday(&tv, NULL);

    _begin_time =
         (long) tv.tv_sec * (long) 1000+
         (long) tv.tv_usec / (long)1000;

    cout << endl;
    cout << "ClanLib - the platform independent game sdk - Linux version."
         << endl;
    cout << endl;

    if (CL_ClanApplication::app == NULL)
    {
        cout << "ClanLib: No global CL_ClanApplication instance!!!" << endl;
        return 255;
    }

    cout << "Querying system configuration." << endl;

#ifdef USE_DYN
    cout << "[[1mDynamic linking ENABLED[[0m" << endl;

    cout << "address of gzdopen: " << &gzdopen << endl;
    gzdopen = NULL;   // <-- it crashes here
    DynLib_libz libz;
    gzdopen = NULL;  
    if (!libz.link()) return 255;

#ifdef USE_X11
    DynLib_libx11 libx11;
    DynLib_libxext libxext;
    if (!libx11.link()) return 255;
    if (!libxext.link()) return 255;
#endif
#ifdef USE_SVGALIB
    DynLib_libvga libvga;
    if (!libvga.link()) return 255;
#endif
#endif

    // [the rest of clanlib's initialization here...]

    int retval = app->main(argc, argvv);

    // [deinit here]

    return retval;
}

The actual declare of the gzdopen pointer is stored in an other header file:

#ifndef USE_DYN
#include <zlib.h>
#else

#include "../detect.h"
extern "C"
{
    typedef void* gzFile;
                         
    DYNFUNC gzFile (*gzdopen)(int, const char *);
    DYNFUNC int (*gzclose)(gzFile);              
    DYNFUNC int (*gzread)(gzFile, void *, unsigned int);
}
 
class DynLib_libz : public CL_DynamicLibrary
{
protected:
    virtual void try_link()
    {                      
        filename("libz.so");
        resolve((void**) &gzdopen, "gzdopen");
        resolve((void**) &gzclose, "gzclose");
        resolve((void**) &gzread, "gzread");
    }                                       
};

where DYNFUNC is a define making it possible to include the header file from
other files as well:

#ifdef DYN_LOAD
    #define DYNFUNC
#else
    #define DYNFUNC extern        
#endif /* DYN_LOAD */

I hope the above code reveals something stupid! I'm getting quite
desperate... :-(

-- 
Magnus Norddahl
ClanSoft