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

1

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

Is there an easy way to enumerate all the function names in a dynamic library? I'd like to make an AI tournament with an unknown number of competitors

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.

5

u/evaned Oct 11 '19

This to me sounds like you're asking for the wrong thing. Your different AIs don't need entry points that are named differently -- you could have them all expose something called run_ai or something. If you want a name, that could be exposed via a different function (string_view get_name() or whatever), or you could have it exposed as a constant instead of a function, or maybe you even just have the interface be just a single struct with some function pointers or something.

If you're talking about dynamically-loaded AI modules instead of shared objects that you link into the executable at link time, I don't really see any drawback to this.

(Part of why I suggest it though is that you'll have to do a bit of work to actually get to the symbol tables of your loaded shared libraries, so this will be easier as well as what seems to be the norm.)

3

u/drcforbin Oct 11 '19

Agreed. Having a standard API to be exposed by the libraries is a better solution than guessing at functions and parsing their names by pattern. If you want more complicated interfaces, versioned interfaces, different kinds of APIs, COM (more or less) did this by having a single function that would be implemented by every shared library, which when called would return a function table, including a function for retrieving pointers to other tables by id, each representing a different kind of interface...it let you basically query whether an object supported an interface, and then to use it.

That said, do consider security...loading untrusted shared libraries can be dangerous.

1

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

That said, do consider security...loading untrusted shared libraries can be dangerous.

It runs in a sandboxed account, so the best it could do is delete or modify all the files in the local directory. Somebody did try that last time (modifying the other AIs to make invalid moves), but all they managed to do was cause a crash, which gave them an automatic loss. The people in the AI tournament are all friends of mine, so I don't see a need for more security than this.