I am trying to implement a dynamic library and connect it in my program. The library will work with ncurses, but I don't think the error is related to this.

Source library lib.cpp:

#include "panel.h" void win() { initscr(); PANEL* root_panel = new_panel(stdscr); } 

Source program main.cpp:

 #include <iostream> #include <dlfcn.h> int main() { void* library_handler = dlopen("./lib.so", RTLD_NOW | RTLD_GLOBAL); if ( ! library_handler) { std::cout << dlerror() << std::endl; return 1; }; return 0; } 

Build commands:

 $ g++ -ldl main.cpp -o main $ g++ -fpic -c lib.cpp -o lib.o $ g++ -shared -o lib.so lib.o 

Result of performance:

 $ ./main ./lib.so: undefined symbol: stdscr 

gcc version 5.3.0

Apparently you need to specify / change some compilation or linking options, but I can not google anything.

    1 answer 1

    When linking you need to add

     g++ -shared -o lib.so lib.o -lncurses++ 
    • Yes, it is me again, well done, missed when forming an example. But I got the idea: I use cmake / make, now I tried to compile with make VERBOSE = 1, it turned out that he does not indicate when linking the library - he does not accept the set command (CMAKE_EXE_LINKER_FLAGS "-lpanel -lncurses"), I will understand. Thanks anyway. - user206565
    • As usual, ask and you will understand, in this case you need CMAKE_SHARED_LINKER_FLAGS :) - user206565