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

    <bdo id='m3azu'></bdo><ul id='m3azu'></ul>

        <small id='m3azu'></small><noframes id='m3azu'>

        <i id='m3azu'><tr id='m3azu'><dt id='m3azu'><q id='m3azu'><span id='m3azu'><b id='m3azu'><form id='m3azu'><ins id='m3azu'></ins><ul id='m3azu'></ul><sub id='m3azu'></sub></form><legend id='m3azu'></legend><bdo id='m3azu'><pre id='m3azu'><center id='m3azu'></center></pre></bdo></b><th id='m3azu'></th></span></q></dt></tr></i><div class="tjjbv5h" id='m3azu'><tfoot id='m3azu'></tfoot><dl id='m3azu'><fieldset id='m3azu'></fieldset></dl></div>
        <tfoot id='m3azu'></tfoot>

      1. <legend id='m3azu'><style id='m3azu'><dir id='m3azu'><q id='m3azu'></q></dir></style></legend>

        帶有模板的 C++ 共享庫:未定義符號(hào)錯(cuò)誤

        C++ Shared Library with Templates: Undefined symbols error(帶有模板的 C++ 共享庫:未定義符號(hào)錯(cuò)誤)

      2. <tfoot id='g7KQU'></tfoot>
      3. <i id='g7KQU'><tr id='g7KQU'><dt id='g7KQU'><q id='g7KQU'><span id='g7KQU'><b id='g7KQU'><form id='g7KQU'><ins id='g7KQU'></ins><ul id='g7KQU'></ul><sub id='g7KQU'></sub></form><legend id='g7KQU'></legend><bdo id='g7KQU'><pre id='g7KQU'><center id='g7KQU'></center></pre></bdo></b><th id='g7KQU'></th></span></q></dt></tr></i><div class="tvldt77" id='g7KQU'><tfoot id='g7KQU'></tfoot><dl id='g7KQU'><fieldset id='g7KQU'></fieldset></dl></div>
        <legend id='g7KQU'><style id='g7KQU'><dir id='g7KQU'><q id='g7KQU'></q></dir></style></legend>
                <tbody id='g7KQU'></tbody>

              <small id='g7KQU'></small><noframes id='g7KQU'>

                • <bdo id='g7KQU'></bdo><ul id='g7KQU'></ul>
                  本文介紹了帶有模板的 C++ 共享庫:未定義符號(hào)錯(cuò)誤的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時(shí)送ChatGPT賬號(hào)..

                  我正在嘗試使用模板類鏈接到共享庫,但它給了我未定義符號(hào)"錯(cuò)誤.我已將問題濃縮為大約 20 行代碼.

                  I'm trying to link to a shared library with a template class, but it is giving me "undefined symbols" errors. I've condensed the problem to about 20 lines of code.

                  shared.h

                  template <class Type> class myclass {
                    Type x;
                  public:
                    myclass() { x=0; }
                    void setx(Type y);
                    Type  getx();
                  };
                  

                  shared.cpp

                  #include "shared.h"
                  template <class Type> void myclass<Type>::setx(Type y) { x = y; }
                  template <class Type> Type myclass<Type>::getx() { return x; }
                  

                  ma??in.cpp

                  #include <iostream>
                  #include "shared.h"
                  using namespace std;
                  
                  int main(int argc, char *argv[]) {
                     myclass<int> m;
                     cout << m.getx() << endl;
                     m.setx(10);
                     cout << m.getx() << endl;
                     return 0;
                  }
                  

                  這是我編譯庫的方式:

                  g++ -fPIC -c shared.cpp -o shared.o
                  g++ -dynamiclib -Wl,-dylib_install_name -Wl,libshared.dylib -o libshared.dylib shared.o
                  

                  和主程序:

                  g++ -c main.cpp
                  g++ -o main  main.o -L. -lshared
                  

                  只得到以下錯(cuò)誤:

                  Undefined symbols:
                  "myclass<int>::getx()", referenced from:
                    _main in main.o
                    _main in main.o
                  "myclass<int>::setx(int)", referenced from:
                    _main in main.o
                  

                  如果我刪除 shared.h/cpp 中的模板"內(nèi)容,并將它們替換為int",則一切正常.此外,如果我只是將模板類代碼直接復(fù)制并粘貼到 main.cpp 中,并且不鏈接到共享庫,則一切正常.

                  If I remove the 'template' stuff in shared.h/cpp, and replace them with just 'int', everything works fine. Also, if I just copy&paste the template class code right into main.cpp, and don't link to the shared library, everything works as well.

                  如何讓這樣的模板類通過共享庫工作?

                  How can I get a template class like this to work through a shared library?

                  我使用的是 MacOS 10.5 和 GCC 4.0.1.

                  I'm using MacOS 10.5 with GCC 4.0.1.

                  推薦答案

                  除了其他答案之外,您還可以顯式實(shí)例化模板類.這僅在您事先知道模板參數(shù)可能采用的類型時(shí)才有用.您使用庫中的所有這些類型實(shí)例化模板.

                  In addition to the other answers, you can explicitly instantiate template classes. This is only useful if you know beforehand what types the template parameters may assume. You instantiate the template with all these types in the library.

                  為了編譯您的示例,只需將以下內(nèi)容添加到 shared.cpp 的末尾:

                  For your example to compile, just add the following to the end of shared.cpp:

                  // Instantiate myclass for the supported template type parameters
                  template class myclass<int>;
                  template class myclass<long>;
                  

                  這會(huì)使用 Type=int 實(shí)例化模板并將實(shí)例化的代碼放在共享庫中.根據(jù)需要為所有類型添加盡可能多的顯式實(shí)例.

                  This instantiates the template with Type=int and places the instantiated code in the shared library. Add as many explicit instantiations as you need, for all the types you need.

                  同樣,如果您希望能夠使用任意類型參數(shù)實(shí)例化模板,那么您必須將定義添加到頭文件中,以便編譯器知道模板的源代碼在其他編譯單元中實(shí)例化時(shí).

                  Again, if you want to be able to instantiate the template with any arbitrary Type parameter, then you must add the definitions to the header file, so that the compiler knows the source code of the template when instantiating it in other compilation units.

                  這篇關(guān)于帶有模板的 C++ 共享庫:未定義符號(hào)錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  Why do two functions have the same address?(為什么兩個(gè)函數(shù)的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復(fù)制構(gòu)造的?)
                  mixing templates with polymorphism(混合模板與多態(tài)性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時(shí)候應(yīng)該使用關(guān)鍵字“typename?使用模板時(shí))
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標(biāo)準(zhǔn)庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數(shù)模板,而 clang 不能)
                • <i id='11hxD'><tr id='11hxD'><dt id='11hxD'><q id='11hxD'><span id='11hxD'><b id='11hxD'><form id='11hxD'><ins id='11hxD'></ins><ul id='11hxD'></ul><sub id='11hxD'></sub></form><legend id='11hxD'></legend><bdo id='11hxD'><pre id='11hxD'><center id='11hxD'></center></pre></bdo></b><th id='11hxD'></th></span></q></dt></tr></i><div class="ntttpnn" id='11hxD'><tfoot id='11hxD'></tfoot><dl id='11hxD'><fieldset id='11hxD'></fieldset></dl></div>

                  <small id='11hxD'></small><noframes id='11hxD'>

                      <tfoot id='11hxD'></tfoot>
                        <bdo id='11hxD'></bdo><ul id='11hxD'></ul>

                          <tbody id='11hxD'></tbody>
                        1. <legend id='11hxD'><style id='11hxD'><dir id='11hxD'><q id='11hxD'></q></dir></style></legend>
                          • 主站蜘蛛池模板: 都江堰招聘网-都江堰人才网 都江堰人事人才网 都江堰人才招聘网 邢台人才网_邢台招聘网_邢台123招聘【智达人才网】 | 挤出熔体泵_高温熔体泵_熔体出料泵_郑州海科熔体泵有限公司 | 365文案网_全网创意文案句子素材站 | 山东风淋室_201/304不锈钢风淋室净化设备厂家-盛之源风淋室厂家 翻斗式矿车|固定式矿车|曲轨侧卸式矿车|梭式矿车|矿车配件-山东卓力矿车生产厂家 | 逗网红-抖音网红-快手网红-各大平台网红物品导航 | 横河变送器-横河压力变送器-EJA变送器-EJA压力变送器-「泉蕴仪表」 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 红外光谱仪维修_二手红外光谱仪_红外压片机_红外附件-天津博精仪器 | 涡轮流量计_LWGY智能气体液体电池供电计量表-金湖凯铭仪表有限公司 | lcd条形屏-液晶长条屏-户外广告屏-条形智能显示屏-深圳市条形智能电子有限公司 | 天津市能谱科技有限公司-专业的红外光谱仪_红外测油仪_紫外测油仪_红外制样附件_傅里叶红外光谱技术生产服务厂商 | 中图网(原中国图书网):网上书店,尾货特色书店,30万种特价书低至2折! | 招商帮-一站式网络营销服务|互联网整合营销|网络推广代运营|信息流推广|招商帮企业招商好帮手|搜索营销推广|短视视频营销推广 | 三效蒸发器_多效蒸发器价格_四效三效蒸发器厂家-青岛康景辉 | 刮板输送机,粉尘加湿搅拌机,螺旋输送机,布袋除尘器 | 回转支承-转盘轴承-回转驱动生产厂家-洛阳隆达轴承有限公司 | 青岛球场围网,青岛车间隔离网,青岛机器人围栏,青岛水源地围网,青岛围网,青岛隔离栅-青岛晟腾金属制品有限公司 | 北京发电机出租_发电机租赁_北京发电机维修 - 河北腾伦发电机出租 | 宠物店加盟_宠物连锁店_开宠物店-【派多格宠物】 | 郑州外墙清洗_郑州玻璃幕墙清洗_郑州开荒保洁-河南三恒清洗服务有限公司 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 生产自动包装秤_颗粒包装秤_肥料包装秤等包装机械-郑州鑫晟重工科技有限公司 | 粤丰硕水性环氧地坪漆-防静电自流平厂家-环保地坪涂料代理 | 昆明挖掘机修理厂_挖掘机翻新再制造-昆明聚力工程机械维修有限公司 | 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 永嘉县奥阳陶瓷阀门有限公司| 污水处理设备维修_污水处理工程改造_机械格栅_过滤设备_气浮设备_刮吸泥机_污泥浓缩罐_污水处理设备_污水处理工程-北京龙泉新禹科技有限公司 | 档案密集柜_手动密集柜_智能密集柜_内蒙古档案密集柜-盛隆柜业内蒙古密集柜直销中心 | 培训无忧网-教育培训咨询招生第三方平台 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 写方案网_方案策划方案模板下载| 世界箱包品牌十大排名,女包小众轻奢品牌推荐200元左右,男包十大奢侈品牌排行榜双肩,学生拉杆箱什么品牌好质量好 - Gouwu3.com | 变色龙云 - 打包app_原生app_在线制作平台_短链接_ip查询 | 东莞韩创-专业绝缘骨架|马达塑胶零件|塑胶电机配件|塑封电机骨架厂家 | 二维运动混料机,加热型混料机,干粉混料机-南京腾阳干燥设备厂 | 外贸网站建设-外贸网站设计制作开发公司-外贸独立站建设【企术】 | 12cr1mov无缝钢管切割-15crmog无缝钢管切割-40cr无缝钢管切割-42crmo无缝钢管切割-Q345B无缝钢管切割-45#无缝钢管切割 - 聊城宽达钢管有限公司 | 踏板力计,制动仪,非接触多功能速度仪,逆反射系数测试仪-创宇 | 蔡司三坐标-影像测量机-3D扫描仪-蔡司显微镜-扫描电镜-工业CT-ZEISS授权代理商三本工业测量 | 球形钽粉_球形钨粉_纳米粉末_难熔金属粉末-广东银纳官网 | 口臭的治疗方法,口臭怎么办,怎么除口臭,口臭的原因-口臭治疗网 |