r/cpp • • Oct 10 '19

10 differences between static and dynamic libraries every C++ developer should know

https://www.acodersjourney.com/cplusplus-static-vs-dynamic-libraries/
5 Upvotes

25 comments sorted by

View all comments

Show parent comments

4

u/drcforbin Oct 10 '19

On Linux, nm will do it, on Windows, dumpbin.

1

u/ShakaUVM i+++ ++i+i[arr] Oct 11 '19

Thanks

From inside of C++, though? I have been searching for some function that will return a list of functions in a so file so that I can find all functions starting with AI_ (like AI_Bob, AI_Charles, etc.) and have my game server put them in a match with each other without having to recompile each time.

Competitors would enter their AI into the competition by copying their so file to the executable's directory.

I could probably system() nm and parse the results, but that seems a bit clunky.

3

u/evaned Oct 11 '19

Just in case it's not clear what I mean or how to do it, I whipped up a crappy little example of what I mean:

///// DRIVER.CPP //////

#include <dlfcn.h>
#include <stdio.h>
#include <vector>

struct Ai
{
    char const* name;
    int (*do_action)(int x);
};

int main(int argc, char* argv[])
{
    std::vector<Ai> ais;
    ais.reserve(argc - 1);

    for (int i = 1; i < argc; ++i) {
        void* lib = dlopen(argv[i], RTLD_LAZY | RTLD_LOCAL);
        if (not lib) {
            fprintf(stdout, "%s: dlopen(%s): %s\n", argv[0], argv[i], dlerror());
            return 1;
        }

        void* name_v = dlsym(lib, "name");
        if (not name_v) {
            fprintf(stdout, "%s: dlsym(\"name\"): %s\n", argv[0], dlerror());
            return 1;
        }

        void* do_action_v = dlsym(lib, "do_action");
        if (not do_action_v) {
            fprintf(stdout, "%s: dlsym(\"do_action\"): %s\n", argv[0], dlerror());
            return 1;
        }

        Ai ai = {
            *static_cast<char const**>(name_v),
            reinterpret_cast<int(*)(int)>(do_action_v)
        };
        ais.emplace_back(ai);
    }

    for (int i = 0; i < (int)ais.size(); ++i)
        printf("%s: do_action(%d) => %d\n", ais[i].name, i, ais[i].do_action(i));

    return 0;
}


///// ALICE.C /////

char const * name = "Alice";

int do_action(int x)
{
    return x;
}

///// BOB.C /////

char const * name = "Bob";

int do_action(int x)
{
    return x * 2;
}

///// CAROL.C /////

char const * name = "Carol";

int do_action(int x)
{
    return x * x;
}

///// DAVE.C /////

char const * name = "Dave";

int do_action(int x)
{
    return 9;
}

///// Makefile (I hate make but whatever let's use it here) /////

CXXFLAGS=-g -Wall -Wextra -Wconversion -std=c++17
LDLIBS=-ldl

all: demo
all-built: driver alice.so bob.so carol.so dave.so
.PHONY: all all-built demo

driver.o: driver.cpp Makefile
driver: driver.o Makefile
	$(CXX) $(LDFLAGS) -o "$@" "$<" $(LOADLIBES) $(LDLIBS)

%.so: %.o Makefile
	$(CXX) $(LDFLAGS) -o "$@" "$<" -shared

demo: all-built
	./driver ./dave.so ./alice.so ./bob.so ./carol.so

Gives:

$ ./driver ./dave.so ./alice.so ./bob.so ./carol.so
Dave: do_action(0) => 9
Alice: do_action(1) => 1
Bob: do_action(2) => 4
Carol: do_action(3) => 9

1

u/ShakaUVM i+++ ++i+i[arr] Oct 11 '19

That's... yeah, exactly what I was trying to figure out what to do. I like how you use the name string to distinguish between the different AI calls with the same name.

Thank you so much!