pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

從 DLL 導出包含 `std::` 對象(向量、映射等)的類

Exporting classes containing `std::` objects (vector, map etc.) from a DLL(從 DLL 導出包含 `std::` 對象(向量、映射等)的類)
本文介紹了從 DLL 導出包含 `std::` 對象(向量、映射等)的類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試從包含 std::vectorsstd::strings 等對象的 DLL 中導出類 - 整個類被聲明為 DLL 導出通過:

class DLL_EXPORT FontManager {

問題是,對于復雜類型的成員,我收到此警告:

<塊引用>

warning C4251: 'FontManager::m__fonts' : class 'std::map<_Kty,_Ty>'需要有 dll 接口供FontManager"類的客戶端使用和[_Kty=std::string,_Ty=tFontInfoRef]

即使我沒有更改成員變量本身的類型,我也可以通過將以下前向類聲明放在它們之前來刪除一些警告:

模板類 DLL_EXPORT std::allocator;模板類 DLL_EXPORT std::vector>;std::vectorm_glyphProviders;

看起來像前向聲明injects"編譯成員時的 DLL_EXPORT 但它安全嗎?
當客戶端編譯這個頭文件并在他身邊使用 std:: 容器時,它真的改變了什么嗎?
它是否會在未來使用這種容器 DLL_EXPORT(并且可能不是內聯的)?
并且它真的解決了警告試圖警告的問題嗎?

這個警告是我應該擔心的,還是最好在這些結構的范圍內禁用它?
客戶端和 DLL 將始終使用相同的庫和編譯器集構建,而這些只是頭文件類...

我使用的是帶有標準 STD 庫的 Visual Studio 2003.


更新

我想更多地針對您,因為我看到答案很籠統,這里我們談論的是 std 容器和類型(例如 std::string)——也許問題真的很重要是:

我們能否通過相同的庫頭禁用對客戶端和 DLL 可用的標準容器和類型的警告,并像對待 int 或任何其他內置函數一樣對待它們?類型?(它似乎在我這邊工作正常)
如果是這樣,我們應該在什么條件下才能做到這一點?

或者是否應該禁止使用此類容器,或者至少要格外小心以確保不會將賦值運算符、復制構造函數等內聯到 DLL 客戶端中?

總的來說,我想知道您是否覺得設計具有此類對象的 DLL 接口(例如使用它們將內容作為返回值類型返回給客戶端)是否是一個好主意,以及為什么,我會喜歡有高水平"此功能的接口...
也許最好的解決方案是 Neil Butterworth 建議的 - 創建一個靜態庫?

解決方案

當您從客戶端訪問類中的成員時,您需要提供一個 DLL 接口.DLL 接口意味著編譯器在 DLL 本身中創建函數并使其可導入.

因為編譯器不知道 DLL_EXPORTED 類的客戶端使用哪些方法,所以它必須強制所有方法都是 dll 導出的.它必須強制客戶端可以訪問的所有成員也必須 dll 導出它們的函數.當編譯器警告您未導出的方法以及客戶端的鏈接器發送錯誤時,就會發生這種情況.

不是每個成員都必須用 dll-export 標記,例如客戶無法接觸的私人成員.在這里您可以忽略/禁用警告(注意編譯器生成的 dtor/ctors).

否則成員必須導出他們的方法.使用 DLL_EXPORT 向前聲明它們不會導出這些類的方法.您必須在其編譯單元中將相應的類標記為 DLL_EXPORT.

它歸結為...(對于非 dll 可導出成員)

  1. 如果您的成員不能/不能被客戶使用,請關閉警告.

  2. 如果您有必須由客戶端使用的成員,請創建一個 dll 導出包裝器或創建間接方法.

  3. 要減少外部可見成員的數量,請使用PIMPL idiom.

<小時>

模板類 DLL_EXPORT std::allocator;

這確實在當前編譯單元中創建了模板特化的實例化.所以這會在dll中創建std::allocator的方法并導出對應的方法.這不適用于具體類,因為這只是模板類的實例化.

I'm trying to export classes from a DLL that contain objects such as std::vectors and std::strings - the whole class is declared as DLL export through:

class DLL_EXPORT FontManager {

The problem is that for members of the complex types I get this warning:

warning C4251: 'FontManager::m__fonts' : class 'std::map<_Kty,_Ty>' needs to have dll-interface to be used by clients of class 'FontManager'
      with
      [
          _Kty=std::string,
          _Ty=tFontInfoRef
      ]

I'm able to remove some of the warnings by putting the following forward class declaration before them even though I'm not changing the type of the member variables themselves:

template class DLL_EXPORT std::allocator<tCharGlyphProviderRef>;
template class DLL_EXPORT std::vector<tCharGlyphProviderRef,std::allocator<tCharGlyphProviderRef> >;
std::vector<tCharGlyphProviderRef> m_glyphProviders;

Looks like the forward declaration "injects" the DLL_EXPORT for when the member is compiled but is it safe?
Does it really change anything when the client compiles this header and uses the std:: container on his side?
Will it make all future uses of such a container DLL_EXPORT (and possibly not inline)?
And does it really solve the problem that the warning tries to warn about?

Is this warning anything I should be worried about or would it be best to disable it in the scope of these constructs?
The clients and the DLL will always be built using the same set of libraries and compilers and those are header only classes...

I'm using Visual Studio 2003 with the standard STD library.


Update

I'd like to target you more though as I see the answers are general and here we're talking about std containers and types (such as std::string) - maybe the question really is:

Can we disable the warning for standard containers and types available to both the client and the DLL through the same library headers and treat them just as we'd treat an int or any other built-in type? (It does seem to work correctly on my side)
If so would should be the conditions under which we can do this?

Or should maybe using such containers be prohibited or at least ultra care taken to make sure no assignment operators, copy constructors etc will get inlined into the DLL client?

In general I'd like to know if you feel designing a DLL interface having such objects (and for example using them to return stuff to the client as return value types) is a good idea or not and why, I'd like to have a "high level" interface to this functionality...
Maybe the best solution is what Neil Butterworth suggested - creating a static library?

解決方案

When you touch a member in your class from the client, you need to provide a DLL-interface. A DLL-interface means, that the compiler creates the function in the DLL itself and makes it importable.

Because the compiler doesn't know which methods are used by the clients of a DLL_EXPORTED class it must enforce that all methods are dll-exported. It must enforce that all members which can be accessed by clients must dll-export their functions too. This happens when the compiler is warning you of methods not exported and the linker of the client sending errors.

Not every member must be marked with with dll-export, e.g. private members not touchable by clients. Here you can ignore/disable the warnings (beware of compiler generated dtor/ctors).

Otherwise the members must export their methods. Forward declaring them with DLL_EXPORT does not export the methods of these classes. You have to mark the according classes in their compilation-unit as DLL_EXPORT.

What it boils down to ... (for not dll-exportable members)

  1. If you have members which aren't/can't be used by clients, switch off the warning.

  2. If you have members which must be used by clients, create a dll-export wrapper or create indirection methods.

  3. To cut down the count of externally visible members, use approaches such as the PIMPL idiom.


template class DLL_EXPORT std::allocator<tCharGlyphProviderRef>;

This does create an instantiation of the template specialization in the current compilation unit. So this creates the methods of std::allocator in the dll and exports the corresponding methods. This does not work for concrete classes as this is only an instantiation of template classes.

這篇關于從 DLL 導出包含 `std::` 對象(向量、映射等)的類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How do I set the icon for my application in visual studio 2008?(如何在 Visual Studio 2008 中為我的應用程序設置圖標?)
Convert CString to const char*(將 CString 轉換為 const char*)
Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio(默認情況下,在 Visual Studio 中從項目中刪除安全警告 (_CRT_SECURE_NO_WARNINGS))
How do I start a new CUDA project in Visual Studio 2008?(如何在 Visual Studio 2008 中啟動新的 CUDA 項目?)
What are some reasons a Release build would run differently than a Debug build(發布版本與調試版本的運行方式不同的一些原因是什么)
How to set up Google C++ Testing Framework (gtest) with Visual Studio 2005(如何使用 Visual Studio 2005 設置 Google C++ 測試框架 (gtest))
主站蜘蛛池模板: 青岛侦探_青岛侦探事务所_青岛劝退小三_青岛调查出轨取证公司_青岛婚外情取证-青岛探真调查事务所 | 天然鹅卵石滤料厂家-锰砂滤料-石英砂滤料-巩义东枫净水 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 无锡网站建设_企业网站定制-网站制作公司-阿凡达网络 | 杭州货架订做_组合货架公司_货位式货架_贯通式_重型仓储_工厂货架_货架销售厂家_杭州永诚货架有限公司 | 非小号行情 - 专业的区块链、数字藏品行情APP、金色财经官网 | 聚合氯化铝厂家-聚合氯化铝铁价格-河南洁康环保科技 | 鑫铭东办公家具一站式定制采购-深圳办公家具厂家直销 | 河南卓美创业科技有限公司-河南卓美防雷公司-防雷接地-防雷工程-重庆避雷针-避雷器-防雷检测-避雷带-避雷针-避雷塔、机房防雷、古建筑防雷等-山西防雷公司 | 齿式联轴器-弹性联轴器-联轴器厂家-江苏诺兴传动联轴器制造有限公司 | 上海皓越真空设备有限公司官网-真空炉-真空热压烧结炉-sps放电等离子烧结炉 | MVE振动电机_MVE震动电机_MVE卧式振打电机-河南新乡德诚生产厂家 | 陕西安玻璃自动感应门-自动重叠门-磁悬浮平开门厂家【捷申达门业】 | 月嫂_保姆_育婴_催乳_母婴护理_产后康复_养老护理-吉祥到家家政 硫酸亚铁-聚合硫酸铁-除氟除磷剂-复合碳源-污水处理药剂厂家—长隆科技 | 飞扬动力官网-广告公司管理软件,广告公司管理系统,喷绘写真条幅制作管理软件,广告公司ERP系统 | 有福网(yofus.com)洗照片冲印,毕业聚会纪念册相册制作个性DIY平台 | 瑞典Blueair空气净化器租赁服务中心-专注新装修办公室除醛去异味服务! | 超高频感应加热设备_高频感应电源厂家_CCD视觉检测设备_振动盘视觉检测设备_深圳雨滴科技-深圳市雨滴科技有限公司 | 不锈钢酒柜|恒温酒柜|酒柜定制|酒窖定制-上海啸瑞实业有限公司 | 扬子叉车厂家_升降平台_电动搬运车|堆高车-扬子仓储叉车官网 | 银川美容培训-美睫美甲培训-彩妆纹绣培训-新娘化妆-学化妆-宁夏倍莱妮职业技能培训学校有限公司 临时厕所租赁_玻璃钢厕所租赁_蹲式|坐式厕所出租-北京慧海通 | 石栏杆_青石栏杆_汉白玉栏杆_花岗岩栏杆 - 【石雕之乡】点石石雕石材厂 | 隧道窑炉,隧道窑炉厂家-山东艾瑶国际贸易 | 东莞喷砂机-喷砂机-喷砂机配件-喷砂器材-喷砂加工-东莞市协帆喷砂机械设备有限公司 | 商用绞肉机-熟肉切片机-冻肉切丁机-猪肉开条机 - 广州市正盈机械设备有限公司 | 聚合氯化铝价格_聚合氯化铝厂家_pac絮凝剂-唐达净水官网 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 | 在线悬浮物浓度计-多参数水质在线检测仪-上海沃懋仪表科技有限公司 | 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 德州万泰装饰 - 万泰装饰装修设计软装家居馆 | 气动机械手-搬运机械手-气动助力机械手-山东精瑞自动化设备有限公司 | 扫地车厂家-山西洗地机-太原电动扫地车「大同朔州吕梁晋中忻州长治晋城洗地机」山西锦力环保科技有限公司 | 游动电流仪-流通式浊度分析仪-杰普仪器(上海)有限公司 | 无缝方管|无缝矩形管|无缝方矩管|无锡方管厂家 | 高柔性拖链电缆_卷筒电缆_耐磨耐折聚氨酯电缆-玖泰特种电缆 | 翅片管换热器「型号全」_厂家-淄博鑫科环保 | RFID电子标签厂家-上海尼太普电子有限公司 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 不锈钢散热器,冷却翅片管散热器厂家-无锡市烨晟化工装备科技有限公司 | 焊管生产线_焊管机组_轧辊模具_焊管设备_焊管设备厂家_石家庄翔昱机械 |