我正在处理一个 DLL 项目并且我正在使用 BOOST 库,我还使用了一个模块定义文件来避免编译器的名称修改,以便可以轻松地从这个 DLL 调用我的函数。但我的问题是很多 BOOST 功能区域也被导出。我正在使用 DLL 查看器查看我的 DLL 公开的函数,令我惊讶的是,那里有很多 BOOST。(我想导出的功能也在那里,所以没有问题)知道为什么会这样吗?(我没有足够的声誉来发布图片,有什么建议我可以这样做吗?pastebin 或其他什么?)
`GetNameOfUsbIf Exported Function
GetNumberOfUsbIfs Exported Function
Initialize Exported Function
int __stdcall SET_SYSTEM_NOTIFY_CALLBACK(void (__cdecl*)(enum SYSTEM_EVENT_MSP)) Exported Function
private: static class boost::archive::detail::extra_detail::map<class boost::archive::binary_iarchive> & __cdecl boost::serialization::singleton<class boost::archive::detail::extra_detail::map<class boost::archive::binary_iarchive> >::get_instance(void) Exported Function
private: static class boost::archive::detail::extra_detail::map<class boost::archive::binary_iarchive> & boost::serialization::singleton<class boost::archive::detail::extra_detail::map<class boost::archive::binary_iarchive> >::instance Exported Function`
Run Code Online (Sandbox Code Playgroud)
我正在使用__declspec(dllexport)我希望导出的函数的前缀,该前缀由编译器标志控制。以上是 dllViewer 文本的复制粘贴 我缺少什么建议?
这是一个老问题,但问题似乎是当前的,因为我遇到了它。所以我提供我的发现。
A DLL of mine which uses a static lib which in turn uses Boost.Serialization exports these symbols in addition to the desired ones:
public: static void __cdecl boost::serialization::singleton_module::unlock(void)
private: static bool & __ptr64 __cdecl boost::serialization::singleton_module::get_lock(void)
public: static bool __cdecl boost::serialization::singleton_module::is_locked(void)
bool `private: static bool & __ptr64 __cdecl boost::serialization::singleton_module::get_lock(void)'::`2'::lock
public: static void __cdecl boost::serialization::singleton_module::lock(void)
Run Code Online (Sandbox Code Playgroud)
I don't have boost::serialization::singleton there, but that may be due to the fact that I'm not actually using Boost.Serialization, just including some of its headers for historic reasons.
After some inspection in the Boost source code (1.67.0 in my case) I found this in boost/serialization/singleton.hpp:
BOOST_DLLEXPORT static void lock(){
Run Code Online (Sandbox Code Playgroud)
And you may have already guess it, that macro is defined as __declspec(dllexport).
So because I'm (transitively) including this header in my sources these symbols get exported automatically. (see Exporting from a DLL Using __declspec(dllexport) for more on this)
So how can this be fixed?
Personally I'm in the lucky situation that I could remove all the Boost.Serialization headers in my project since they were not used. After a rebuild the symbols were gone.
If one is not so lucky, life is becoming hard. It seems that there is no opt-out macro. (well, usually one would not want to NOT export symbols marked as BOOST_DLLEXPORT). But the code contains this information in the comments:
// note usage of BOOST_DLLEXPORT. These functions are in danger of
// being eliminated by the optimizer when building an application in
// release mode. Usage of the macro is meant to signal the compiler/linker
// to avoid dropping these functions which seem to be unreferenced.
// This usage is not related to autolinking.
Run Code Online (Sandbox Code Playgroud)
So if you really need to remove these symbols you can try to modify the Boost headers and remove the BOOST_DLLEXPORT from the affected symbols in this one header, this will definitely remove those symbols. Then test if everything works, if so party hard.
Otherwise since you are on MSVC you could try some tricks to stop the optimizer, e.g., as decribed here: Preventing the optimizer from optimizing a variable away in visual studio. If this also does not help you are pretty much out of luck.
This does not feel right, but it is the only solution I can think of. (apart from filing a bug with Boost which may not help anyway)