开发者

How to find programmatically location of a dynamic library file?

开发者 https://www.devze.com 2023-01-23 18:59 出处:网络
I want to find the path in the system (if any) of a specific library right now I have a built ins path开发者_运维问答:

I want to find the path in the system (if any) of a specific library right now I have a built ins path开发者_运维问答:

dllNameFrodo = "/usr/local/frodo/lib/libfrodo.dylib";

but of course is ugly and not portable.

Any ideas ?


Instead of trying to load a hardcoded path, either package the library locally with your application so you know where it's installed, or better yet, just let the runtime loader find and load it automatically. This way the user can fiddle with LD_LIBRARY_PATH and LD_PRELOAD to pick which version to load.


It's like searching for any kind of file - given it's name or extension and some hints of where to start searching-, so if what you want is to search in a portable way you have 2 solutions :

  1. use boost::filesystem
  2. build some search functions and implement once per platform using platform-specific functions. Hint : that's what boost::filesystem is -with sugar on top- but if you don't want or can't use boost, then it's the only "portable" alternative.


On Windows you can use FindFirstFile and family.

This is pretty specialized requirement - portable implementation might be hard to find and in any case is going to use platform-specific APIs underneath if it's going to be optimal on all.

0

精彩评论

暂无评论...
验证码 换一张
取 消