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

    • <bdo id='zToFt'></bdo><ul id='zToFt'></ul>
    <legend id='zToFt'><style id='zToFt'><dir id='zToFt'><q id='zToFt'></q></dir></style></legend>

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

  1. <tfoot id='zToFt'></tfoot>
    1. 使用静态成员时的链接器错误

      Linker error when using static members(使用静态成员时的链接器错误)

        1. <i id='5uytw'><tr id='5uytw'><dt id='5uytw'><q id='5uytw'><span id='5uytw'><b id='5uytw'><form id='5uytw'><ins id='5uytw'></ins><ul id='5uytw'></ul><sub id='5uytw'></sub></form><legend id='5uytw'></legend><bdo id='5uytw'><pre id='5uytw'><center id='5uytw'></center></pre></bdo></b><th id='5uytw'></th></span></q></dt></tr></i><div id='5uytw'><tfoot id='5uytw'></tfoot><dl id='5uytw'><fieldset id='5uytw'></fieldset></dl></div>

          <small id='5uytw'></small><noframes id='5uytw'>

            <bdo id='5uytw'></bdo><ul id='5uytw'></ul>

            <legend id='5uytw'><style id='5uytw'><dir id='5uytw'><q id='5uytw'></q></dir></style></legend>
                <tfoot id='5uytw'></tfoot>

                  <tbody id='5uytw'></tbody>
                本文介绍了使用静态成员时的链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 Mac OS X 上使用 Qt 4.7 和 Cmake 2.8.3 以及 g++ 4.2.1.

                I'm using Qt 4.7 and Cmake 2.8.3 with g++ 4.2.1 on Mac OS X.

                在我的一个文件中使用静态或全局变量时,我收到了一个奇怪的链接器错误.这是错误:

                I'm getting a bizarre linker error when using static or global variables in one of my files. Here's the error:

                ld: duplicate symbol ColorTrail::calculateColorUniformLocation        in CMakeFiles/GLBall.dir/src/DesktopMain.cpp.o and CMakeFiles/GLBall.dir/src/ColorTrail.cpp.o
                collect2: ld returned 1 exit status
                

                calculateColorUniformLocation 是类 ColorTrail 的静态成员...但它甚至根本没有在 DesktopMain.cpp 中使用!

                calculateColorUniformLocation is a static member of class ColorTrail... but its not even used in DesktopMain.cpp at all!

                这是我尝试过的:重命名变量并不能解决问题.将变量移出类并使其成为普通的全局变量也不能修复它

                Here's what I've tried: Renaming the variable doesn't fix the problem. Moving the variable out of the class and just making it a plain global variable also doesn't fix it

                文件 ColorTrail.h:

                The file ColorTrail.h:

                #ifndef COLORTRAIL
                #define COLORTRAIL 9
                
                #include "GlobalConstants.h"
                #include <vector>
                using namespace std;
                
                
                class ColorTrail
                {
                private:
                    //note that this is NOT a Q_OBJECT
                
                    static GLint calculateColorUniformLocation;
                
                    //omitted for brevity
                };
                
                GLint ColorTrail::calculateColorUniformLocation;
                
                
                #endif
                

                DesktopMain.cpp 使用类 ColorTrail,但不是静态的,也从不引用变量.

                DesktopMain.cpp uses class ColorTrail, but not statically and never references the variable.

                有谁知道 Qt 有什么问题/有类似的问题吗?

                Anyone have any idea what could be wrong/had a similar problem with Qt?

                推荐答案

                您需要在 cpp 文件中而不是在头文件中定义静态变量.如果你在头文件中定义它,每个包含这个头的 cpp 文件都会得到它自己的副本,因此链接器会抱怨重复的符号.

                You need to define the static variable in cpp file and not in header file. If you define it in header file, every cpp file which includes this header will get its own copy hence linker complains about duplicate symbols.

                这篇关于使用静态成员时的链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)

                    <legend id='OsK2Q'><style id='OsK2Q'><dir id='OsK2Q'><q id='OsK2Q'></q></dir></style></legend>

                      <i id='OsK2Q'><tr id='OsK2Q'><dt id='OsK2Q'><q id='OsK2Q'><span id='OsK2Q'><b id='OsK2Q'><form id='OsK2Q'><ins id='OsK2Q'></ins><ul id='OsK2Q'></ul><sub id='OsK2Q'></sub></form><legend id='OsK2Q'></legend><bdo id='OsK2Q'><pre id='OsK2Q'><center id='OsK2Q'></center></pre></bdo></b><th id='OsK2Q'></th></span></q></dt></tr></i><div id='OsK2Q'><tfoot id='OsK2Q'></tfoot><dl id='OsK2Q'><fieldset id='OsK2Q'></fieldset></dl></div>
                      • <bdo id='OsK2Q'></bdo><ul id='OsK2Q'></ul>
                        1. <tfoot id='OsK2Q'></tfoot>
                        2. <small id='OsK2Q'></small><noframes id='OsK2Q'>

                            <tbody id='OsK2Q'></tbody>