久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <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="qwawimqqmiuu" 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++ 共享庫:未定義符號錯(cuò)誤

        C++ Shared Library with Templates: Undefined symbols error(帶有模板的 C++ 共享庫:未定義符號錯(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="qwawimqqmiuu" 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++ 共享庫:未定義符號錯(cuò)誤的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  限時(shí)送ChatGPT賬號..

                  我正在嘗試使用模板類鏈接到共享庫,但它給了我未定義符號"錯(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>;
                  

                  這會使用 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++ 共享庫:未定義符號錯(cuò)誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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="qwawimqqmiuu" 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>
                          • 主站蜘蛛池模板: 精品国产一区二区三区久久影院 | www.日本国产 | 中文字幕免费视频 | 日本福利视频免费观看 | 亚洲午夜精品 | 久久一区精品 | 久久成人免费 | 国产成人91 | 中文字幕一页二页 | 国产精品久久久久久久久久免费看 | 男人的天堂久久 | 99精品免费久久久久久日本 | 中文字幕动漫成人 | 中文字幕精品一区二区三区精品 | 亚洲高清在线观看 | 国产精品一区二区久久 | 狠狠插狠狠操 | 99福利视频导航 | 成人av电影在线 | 久久国产精品-久久精品 | 波多野结衣电影一区 | 久久久精品网站 | 欧美片网站免费 | 91精品久久久久久久久久 | 狠狠操电影 | 国产欧美在线观看 | www久久久| 欧美日韩在线观看一区 | 五月天婷婷久久 | 中文字幕视频在线观看免费 | 国产精品久久久 | 国产aa | 日韩欧美中文字幕在线视频 | 成年网站在线观看 | 久久这里只有精品首页 | 日本视频中文字幕 | 九九视频在线观看 | 最新一级毛片 | www精品美女久久久tv | 欧美一级欧美三级在线观看 | 午夜91|