Language: C ++ OS: Linux, ubuntu

I want to load into my program my plugin as a dynamically loadable library (*. So). When I try to get a segmentation fault .

Here is the application code:

/* * File: Context.h * Author: denis * * Created on 13. ledna 2017, 23:33 */ #ifndef CONTEXT_H #define CONTEXT_H #include <iostream> namespace app { namespace context { class IO { public: IO() { } void printHello() { std::cout << "Hello worgld" << std::endl; } void print(const char* str) { std::cout << str << std::endl; } }; } } #endif /* CONTEXT_H */ 

 /* * File: Module.h * Author: denis * * Created on 13. ledna 2017, 23:43 */ #ifndef MODULE_H #define MODULE_H #include "Context.h" namespace app { class Module { public: Module() { } void SetContext(app::context::IO* context) { appContext = context; } virtual void Run() = 0; protected: app::context::IO* appContext; }; } #endif /* MODULE_H */ 

 /* * File: main.cpp * Author: denis * * Created on 13. ledna 2017, 23:32 */ #include <cstdlib> #include <iostream> #include <dlfcn.h> #include "include/sdk/Module.h" int main(int argc, char** argv) { std::cout << "Start APP" << std::endl; std::cout << "Enter number [1] to load module or type [2] to exit" << std::endl; int n = 0; app::context::IO* appContext = new app::context::IO(); while (true) { std::cin >> n; std::cout << n << std::endl; switch (n) { case 1: { void* hndl = dlopen("libdl1.so", RTLD_NOW); if (hndl == NULL) { std::cerr << dlerror() << std::endl; } else { void* make = dlsym(hndl, "make"); app::Module* module = static_cast<app::Module*> (make); module->SetContext(appContext); module->Run(); dlclose(hndl); } } break; case 2: return 0; break; } } return 0; } 

Here is the library code (plugin):

 /* * File: Santana.h * Author: denis * * Created on 14. ledna 2017, 1:16 */ #ifndef SANTANA_H #define SANTANA_H #include "sdk/Module.h" namespace bla { namespace bla { class Santana : public app::Module { public: Santana() { } void Run() { appContext->printHello(); appContext->print("from dl1"); } }; } } #endif /* SANTANA_H */ 

 /* * File: dl1.h * Author: denis * * Created on 13. ledna 2017, 23:40 */ #ifndef DL1_H #define DL1_H #include "Santana.h" extern app::Module* make(); #endif /* DL1_H */ 

 /* * File: dl1.cpp * Author: denis * * Created on 13. ledna 2017, 23:40 */ #include "sdk/Module.h" #include "dl1.h" app::Module* make() { return new bla::bla::Santana(); } 

Where is this problem? I have no debugger.

Closed due to the fact that off-topic participants Vadim Ovchinnikov , Harry , αλεχολυτ , pavel , aleksandr barakin Jan 15 '17 at 14:03 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Vadim Ovchinnikov, aleksandr barakin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 3
    “I have no debugger.” - it means it's time to get it. - αλεχολυτ
  • The computer does not pull. - Petrovich Denis
  • 3
    Stupid some say. Try to provide a minimal reproducible example , preferably without using many different files. - αλεχολυτ
  • This is exactly the code that I use. This is a SANDBOX and it spits out the segmentation fault . This is all code, there is no other. - Petrovich Denis
  • 2
    весь and the минимальный is still different concepts. For example, why all these namespace , comments in the file header, why not limit it to two files (program, library) to provide a demonstration of the problem? The simpler and clearer your task will be described - the greater the chance that you will be helped with its solution. - αλεχολυτ

1 answer 1

Here is an expose

in the library file instead of:

 app::Module* make() { return new bla::bla::Santana(); } 

need to

 extern "C" app::Module* make() { return new bla::bla::Santana(); } 

and load the library instead:

 void* make = dlsym(hndl, "make"); app::Module* module = static_cast<app::Module*> (make); module->SetContext(appContext); module->Run(); 

like this

 app::Module* (*make)() = reinterpret_cast<app::Module * (*)()>(dlsym(hndl, "make")); if (make) { app::Module* module = make(); module->SetContext(appContext); module->Run(); } 

And here it is written in more detail.

:)