gonczor
gonczor

Reputation: 4146

Finding symbol with dlsym for symbol in namespace

I have a dynamic library I want to load. I have defined a function add():

#include <iostream>
#include "mymath.h"

#define EXPORT __attribute__((visibility("default")))


EXPORT
int mymath::add(int a, int b) {
    return a + b;
}

This symbol sits within a namespace called mymath:

namespace mymath {
    int add(int, int);
}

I have compiled this library with following command:

llvm-g++ -Iinclude -dynamiclib -std=c++17 src/mymath.cpp -current_version 1.0 -compatibility_version 1.0 -fvisibility=hidden -o bin/mymath.dylib

Now I want to use it within another program loading this symbol in runtime. I have come to following code:

#include <iostream>
#include <dlfcn.h>
#include "mymath.h"

int main() {
    const char* libName = "bin/mymath.dylib";
    void *libHandle;
    std::cout << "# main() starts" << std::endl;
    libHandle = dlopen(libName, RTLD_NOW);
    if(libHandle == NULL) {
        std::cerr << "Error opening mymath:\n" << dlerror() << std::endl;;
        return 1;
    }
    void (*mymath_add) = dlsym(libHandle, "mymath:add");
    if(mymath_add == NULL) {
        std::cerr << "Error opening while getting address of mymath::add:\n" << dlerror() << std::endl;;
        return 2;
    }
    std::cout << "# main() exits" << std::endl;
    return 0;
}

And get this error when running it

 $ make
clang++ -Wall -Wextra -std=c++17 -g -Iinclude -Llib src/main.cpp -o bin/main
 wgonczaronek  Wiktor-Gonczaronek  ~/Projects/…/macos-pt2  master  ? 
 $ ./bin/main
# main() starts
Error opening while getting address of mymath::add:
dlsym(0x7ffac9402a90, mymath:add): symbol not found

I have tried with symbols I found using nm command, but get same error. How can I load this symbol using namespace?

Upvotes: 1

Views: 3021

Answers (1)

Radosław Cybulski
Radosław Cybulski

Reputation: 2992

See this page for better explanation (https://en.wikipedia.org/wiki/Name_mangling).

Symbol names are mangled, when ommited to library. In C++ name mangling encodes namespaces, template arguments (if any), function arguments (if any). This will produce weird symbol like _ZN9Wikipedia7article6formatE. C++ mangling is implementation defined and might change from compiler to compiler, so trying to decode it is at best risky. I suggest moving function out of namespace and adding extern "C" prefix to force C-style name mangling. C-style name mangling is well defined, standard and will work with every compiler. So

extern "C" int add(int a, int b) {
    return a + b;
}

and:

void (*mymath_add) = dlsym(libHandle, "_add");

Upvotes: 2

Related Questions