Click here to Skip to main content
13,409,878 members (37,803 online)
Rate this:
Please Sign up or sign in to vote.
#include <stdio.h>

#ifdef __cplusplus
extern "C" {

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

#ifdef __cplusplus

#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,"%s","core end\n");
    g_map[xx].fn = fn; //---------->core at this, why ?? who can tell me????
    g_map[xx].des = dex;
    return 0;

#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 )
    return 0;

#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;

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


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

//the follow is exe file
#include <dlfcn.h>
#include <stdio.h>

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

//i build this use
//g++ main.cpp -rdynamic -ldl
when i type this in centos
it core
who can tell me why?
thanks a lot :)
Posted 29-Oct-12 19:17pm
Updated 30-Oct-12 3:14am
CPallini 30-Oct-12 7:13am
The code you posted is not the actual one (e.g. 'value' type is not declared).
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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:
#include "OtherClass1.h"
#include "OtherClass2.h"

class OtherClass3;

class CDll
   void func();
   void func2();

   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()

void shared_dll_func2()

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. :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You have not put any values into your map so you cannot use an indexer to refer to an element within it. My bad.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

no , when i use g_map[xx],it will auto create key=xx and a value.
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.180221.1 | Last Updated 30 Oct 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100