Search code examples
c++dlopendynamic-loadingdlsym

Loading shared library dynamically using dlopen


I'm trying to load TestCode.so using dlopen. getNumber() is the function I want to use from the TestCode.so. But when I'm loading the .so. I'm not able to use the function. Its giving the segmentation fault.

Example program: TestHeader.hpp

#ifndef _HEADER_HPP
#define _HEADER_HPP

typedef struct
{
        int number;
} Test;

#endif

TestCode.cpp

#include "TestHeader.hpp"

extern "C" void getNumber( Test* tObj, int number)
{
        tObj->number = number;
}

main.cpp

#include "TestHeader.hpp"
#include <iostream>
#include <dlfcn.h>
#include <stdio.h>
int main() {
        using std::cout;
        using std::cerr;
        Test* tMainObj = NULL;    
        typedef int (*TestNumber)(Test*, int);

        void* thandle = dlopen("./TestCode.so", RTLD_LAZY);
        if (!thandle) {
                cerr << "Cannot load TestCode: " << dlerror() << '\n';
                return 1;
        }

        // reset errors
        dlerror();

        // load the symbols
        TestNumber getAge = (TestNumber) dlsym(thandle, "getNumber");
        const char* dlsym_error = dlerror();
        if (dlsym_error) {
                cerr << "Cannot load symbol getNumber: " << dlsym_error << '\n';
                return 1;
        }
        printf("Getting my age\n");
        int myAge = 25; 
        getAge(tMainObj,myAge);
        printf("My age from the so is: %d\n",tMainObj->number);

        dlclose(thandle);
}

Output:

Getting my age Segmentation fault (core dumped)

For Compilation and creating shared library. I used following command,

g++ -fPIC -c -Wall TestHeader.hpp
g++ -fPIC -c -Wall TestCode.cpp 
g++ -shared TestCode.o -o TestCode.so
g++ -fPIC -c -Wall main.cpp
g++ main.cpp -o main TestCode.o -ldl

Can anyone help me out understanding this part? Thanks in advance.


Solution

  • The reason is that you never allocate any Test object. The pointer is NULL(use nullptr), so
    tObj->number = number; is UB and likely a segfault. There's no reason for test to be a pointer.

    Test tMainObj;getAge(&tMainObj,myAge); is simpler and gets the job done.