久久久久久久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++ 共享庫:未定義符號錯誤

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

      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++ 共享庫:未定義符號錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  限時送ChatGPT賬號..

                  我正在嘗試使用模板類鏈接到共享庫,但它給了我未定義符號"錯誤.我已將問題濃縮為大約 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
                  

                  只得到以下錯誤:

                  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 中的模板"內容,并將它們替換為int",則一切正常.此外,如果我只是將模板類代碼直接復制并粘貼到 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.

                  推薦答案

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

                  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.

                  為了編譯您的示例,只需將以下內容添加到 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 實例化模板并將實例化的代碼放在共享庫中.根據需要為所有類型添加盡可能多的顯式實例.

                  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.

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

                  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.

                  這篇關于帶有模板的 C++ 共享庫:未定義符號錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  Why do two functions have the same address?(為什么兩個函數的地址相同?)
                  Why the initializer of std::function has to be CopyConstructible?(為什么 std::function 的初始化程序必須是可復制構造的?)
                  mixing templates with polymorphism(混合模板與多態性)
                  When should I use the keyword quot;typenamequot; when using templates(我什么時候應該使用關鍵字“typename?使用模板時)
                  Dependent name resolution amp; namespace std / Standard Library(依賴名稱解析命名空間 std/標準庫)
                  gcc can compile a variadic template while clang cannot(gcc 可以編譯可變參數模板,而 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>
                          • 主站蜘蛛池模板: 欧美自拍一区 | 福利片在线 | 亚洲123区 | 亚洲一二区 | 欧美精品二区三区四区免费看视频 | 黄色免费网站 | 看片地址| 欧美日韩国产三级 | 日韩中文字幕在线观看 | 三年中文在线看免费观看 | 日日夜夜狠狠操 | 日本一区二区三区免费观看 | 日韩精品福利 | 亚洲一区二区三区视频 | 中文字幕免费在线观看 | 久久国产精品一区二区三区 | 国产精品免费一区 | 91美女视频 | 亚洲精品1区2区 | 福利一区福利二区 | 日韩中文一区 | 成人黄色在线观看 | 国产成人在线免费视频 | 亚洲精品99| 福利在线看 | 99精品视频在线 | 日本在线视频一区 | 97国产精品人人爽人人做 | 国产乱码一区二区三区 | 亚洲三区在线 | 久久机热| 99re视频| 国产午夜精品一区二区三区视频 | 在线视频一区二区三区 | 欧美日韩国产激情 | 国产小视频在线观看 | 国产精品一区在线观看 | 国产日韩精品一区二区 | 久久久久久久97 | 日韩一级片在线观看 | 久久精品福利视频 |