Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dll.cpp:
C++
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

void show(){
    fprintf(stdout,"%s","dll show call.");
}

#ifdef __cplusplus
}
#endif

dll_1.cpp:
C++
#include "dll.h"

map<string,value> g_map;

int register_funk(string xx,fn_cmd_handler_t fn, string dex) { // removed obscenity
    fprintf(stdout,"%s","core begin\n");
    fprintf(stdout,"%u",g_map.size());
    fprintf(stdout,"%s","core end\n");
    fprintf(stdout,"-------fn-%p",fn);
    fprintf(stdout,"%s","----\n");
    fprintf(stdout,"%s","----\n");
    g_map[xx].fn = fn; //---------->core at this, why ?? who can tell me????
    g_map[xx].des = dex;
    return 0;
}

dll_2.cpp:
C++
#include "dll.h"

int samxxxx( int machine_id, vector<string> &vecValue );

static int __inss = register_funk("xxxx",samxxxx,"funk"); // removed obscenity

int samxxxx( int machine_id, vector<string> &vecValue )
{
    fprintf(stdout,"%s","ssss--------->");
    return 0;
}

dll.h:
C++
#ifndef __xxxx__
#define __xxxx__

#include ,map>
#include <vector>
#include <string>
#include <stdio.h>
using namespace std;
typedef int ( *fn_cmd_handler_t )( int machine_id, vector<string> &vecValue );

typedef struct {
    fn_cmd_handler_t fn;
    string des;
}VALUE;


int register_funk(string xx,fn_cmd_handler_t fn, string dex);  // removed obscenity

#endif

//and i build this so use
//g++ -g -O2 -shared -Wall -o a.so *.cpp

//the follow is exe file
C++
//main.cpp
#include <dlfcn.h>
#include <stdio.h>

int main()
{
    void *handle = dlopen("/home/samuel/Temp/funk/a.so", RTLD_LAZY);
    fprintf(stdout, "%s\n", dlerror());
    dlclose(handle);
    return 0;
}

//i build this use
//g++ main.cpp -rdynamic -ldl
when i type this in centos
>./a.out
it core
who can tell me why?
thanks a lot :)
Posted
Updated 30-Oct-12 2:14am
v7
Comments
CPallini 30-Oct-12 7:13am    
The code you posted is not the actual one (e.g. 'value' type is not declared).

Both your static int __inss and map<string,value> g_map; are global variables and the order of their initialization isn't specified because they reside in separate .cpp and .obj files. In your case you were not lucky and __inss was initialized before g_map so g_map is used before its actual initialization because the initialization of __inss uses g_map. g_map.size() doesn't crash because its just substraction of two pointers that doesn't crash even if the pointers are uninitialized or zero but putting an item to the map is more complex to survive. You can solve the problem by putting g_map and __inss to the same .cpp file and making sure that g_map is above __inss becuase inside the same .cpp (compilation unit) the initialization order is top to bottom while the destruction is bottom to top.

If you were lucky and g_map would initialize before __inss then you would probably face this error at a later point in your project when your linker decides to swap the initialization order.

My general advice: Don't use global variables at all if you don't need them. Declare a main class for your DLL, and instatiate just that as a global variable and then put everything into that class as member variables. Something like this:
C++
#include "OtherClass1.h"
#include "OtherClass2.h"

class OtherClass3;

class CDll
{
public:
   CDll();
   ~CDll();
   void func();
   void func2();

private:
   std::map<x,y> m_Map;
   int m_BlahBlah;
   int m_XXX;
   OtherClass1 m_OtherClass1;
   OtherClass2 m_OtherClass2;
   OtherClass3* m_OtehrClass3;
};

// The only static variable in the DLL
CDll g_Dll;


// functions exported by the shared lib:
void shared_dll_func()
{
   g_Dll.func();
}

void shared_dll_func2()
{
   g_Dll.func2();
}

...
 
Share this answer
 
v5
Comments
CPallini 30-Oct-12 8:46am    
Good point, my 5.
pasztorpisti 30-Oct-12 8:48am    
Thank you!
denghuancong 31-Oct-12 5:10am    
nice , thanks. :)
You have not put any values into your map so you cannot use an indexer to refer to an element within it. My bad.
 
Share this answer
 
v2
no , when i use g_map[xx],it will auto create key=xx and a value.
 
Share this answer
 
Comments
Richard MacCutchan 30-Oct-12 8:00am    
You need to step into this code with your debugger to see what is happening. As CPallini says, the code above does not have a definition for value used in your map.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900