简体   繁体   English

在 C/C++ 中,如何确定库是否为静态链接

[英]In C/C++, how do I determine if a library is statically linked or not

I have a static library that provides some functionality.我有一个提供一些功能的 static 库。 I have applications that use this library.我有使用这个库的应用程序。 Some of these applications are command line applications or run as daemons/services.其中一些应用程序是命令行应用程序或作为守护程序/服务运行。 Some of these applications are GUI applications.其中一些应用程序是 GUI 应用程序。

The library offers functionality to provide GUI elements to allow it to be configured by the user.该库提供了提供 GUI 元素以允许用户对其进行配置的功能。 I build the library in two flavours;我以两种方式构建图书馆; a GUI flavour and a CLI flavour. GUI 风格和 CLI 风格。 I then link the appropriate version to the application.然后我将适当的版本链接到应用程序。

I would like to be able to improve/simplify matters by separating the GUI functionality of this library into a separate "extension" library that provides GUI support.我希望能够通过将此库的 GUI 功能分离到提供 GUI 支持的单独“扩展”库中来改进/简化问题。 My plan is that at runtime the main library somehow detects the presence of the GUI support library.我的计划是在运行时主库以某种方式检测到 GUI 支持库的存在。 If present then it instantiates an object from the GUI library that provides the necessary functionality, and if not it instantiates an object from itself that provides the same APIs as "stub" functions that do nothing or return errors as appropriate.如果存在,则它从提供必要功能的 GUI 库中实例化一个 object,如果不存在,它从自身实例化一个 object,它提供与“存根”函数相同的 API,这些函数不执行任何操作或酌情返回错误。 The two objects would inherit from a common pure virtual abstract base class.这两个对象将从一个公共的纯虚拟抽象基 class 继承。

This must be done at runtime because I want to be able to link exactly the same binary of the main library to both CLI and GUI applications, and not rely on compile time switches which, by definition, result in differing binaries.这必须在运行时完成,因为我希望能够将主库的完全相同的二进制文件链接到 CLI 和 GUI 应用程序,而不是依赖编译时开关,根据定义,编译时开关会导致不同的二进制文件。

I have been thinking along the lines of implementing a function in the main library like this:我一直在考虑在主库中实现 function,如下所示:

bool SupportsGUI() { return false; }

and then implementing the same function in the GUI support library like this:然后在 GUI 支持库中实现相同的 function,如下所示:

bool SupportsGUI() { return true; }

and then, when linking the application, somehow force the linker to resolve the link to use the one from the GUI support library if present.然后,在链接应用程序时,以某种方式强制 linker 解析链接以使用 GUI 支持库中的链接(如果存在)。

I cannot figure out how to get this to work.我无法弄清楚如何让它发挥作用。

Assuming that both libraries are static libraries how can I, at runtime, determine from one library whether the linking application has also linked the other?假设这两个库都是 static 库,我如何在运行时从一个库中确定链接应用程序是否也链接了另一个库?

Alternatively, how can I, at link time, override a set of functions implemented in one library with an equivalent set of functions implemented in a separate library.或者,我如何在链接时用在一个单独的库中实现的一组等效函数覆盖在一个库中实现的一组函数。

This does not need to be done as runtime.不需要作为运行时完成。 The common solution is to use a weak symbol .常见的解决方案是使用弱符号 This is a symbol in a library that's used when there is no "normal" symbol to link against.这是库中的一个符号,当没有“正常”符号可链接时使用。 Your base library provides the weak symbols, your GUI library optionally provides replacements, and the static linker figures it out.您的基础库提供了弱符号,您的 GUI 库可以选择提供替换,static linker 可以解决这个问题。

You need a plugin loading system for this.为此,您需要一个插件加载系统。

  1. Plugin API library defines the interface (eg an abstract class defining some virtual functions you want to be able to call)插件 API 库定义了接口(例如,一个抽象的 class 定义了一些您希望能够调用的虚拟函数)

  2. Plugin implements the actual GUI code.插件实现了实际的 GUI 代码。 This is then a shared library that is loaded at runtime, with a hardcoded path and some form of configurable or fixed path/filename.这是一个在运行时加载的共享库,具有硬编码路径和某种形式的可配置或固定路径/文件名。 Other plugin, or application itself implements fallback implementation that is used if hardcoded plugin.其他插件或应用程序本身实现了硬编码插件时使用的后备实现。

  3. The application then tries to load the GUI plugin and calls a specific function from that dynamically loaded library to create the GUI specific plugin object.然后应用程序尝试加载 GUI 插件并从该动态加载的库中调用特定的 function 以创建 GUI 特定插件 object。 If the plugin cannot be loaded, it instantiates the fallback object.如果无法加载插件,它会实例化后备 object。

There are many helper libraries you can use to do this kind of thing.您可以使用许多帮助程序库来执行此类操作。 Qt has a QPluginLoader class but there are various other semi-generic wrappers to load dynamic libraries and instantiate plugin interfaces from them. Qt 有一个QPluginLoader class 但是还有其他各种半通用包装器来加载动态库并从中实例化插件接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Eclipse C++ 项目中包含静态链接库? - How do I include a statically linked library in my Eclipse C++ project? C++ 静态链接共享库 - C++ Statically linked shared library 如何创建与 c/c++ 标准库静态链接的 static 库? - How to create a static library that is statically linked against c/c++ standard libraries? 静态链接的非托管库和C ++ CLR - Statically linked unmanaged libs and C++ CLR 如何在OSX中将C ++库静态链接到C应用程序? - How can I statically link a C++ library to a C application in OSX? 将公共语言运行时 (/clr) .dll 链接到静态链接到 C/C++ 运行时库的静态库 - Linking a Common Language Runtime (/clr) .dll to static libraries statically linked to the C/C++ Runtime Library C / C ++:静态链接库和目标文件有什么区别? - C/C++: What is the difference between a statically-linked library and an object file? 如何在Android上将C ++运行时静态编译为NDK / JNI库? - How to statically compile C++ runtime into NDK/JNI library on Android? 如何从C ++项目静态链接golang的.a库? - How to statically link golang's .a library from C++ project? 具有1个功能的C ++ / CLI lib与本机C ++静态链接 - C++/CLI lib with 1 function to be statically linked with native C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM