Qt5 C++中出现意外"Multiple definition"错误



我有使用C++中GLFW的工作代码。现在我想把它和Qt一起使用。我使用的是一个继承QOpenGLWidget的类。问题是,在我的utils.h和main.cpp文件中,所有函数都有多个定义错误,而它们只定义了一次。

生成日志:

Makefile:597: warning: overriding recipe for target 'main.o'
Makefile:432: warning: ignoring old recipe for target 'main.o'
g++ -Wl,-O1 -o Rachaita3DFrontend main.o mainwindow.o oglwidget.o main.o moc_mainwindow.o   -lGLEW -lglfw -lGLX -lSOIL -lstdc++ -lQt5Widgets -lQt5Gui -lQt5Core /usr/lib/x86_64-linux-gnu/libGL.so -lpthread   
/usr/bin/ld: main.o: in function `ToCompleteEntity(DataStructs::Type*, DataStructs::Position*, DataStructs::Rotation*, DataStructs::Scale*, DataStructs::Mesh*, DataStructs::Shader*)':
main.cpp:(.text+0x0): multiple definition of `ToCompleteEntity(DataStructs::Type*, DataStructs::Position*, DataStructs::Rotation*, DataStructs::Scale*, DataStructs::Mesh*, DataStructs::Shader*)'; main.o:main.cpp:(.text+0x0): first defined here
/usr/bin/ld: main.o: in function `LoadTexture(char const*, int, int, MeshInstance)':
main.cpp:(.text+0x130): multiple definition of `LoadTexture(char const*, int, int, MeshInstance)'; main.o:main.cpp:(.text+0x130): first defined here
/usr/bin/ld: main.o: in function `key_callback(GLFWwindow*, int, int, int, int)':
main.cpp:(.text+0x270): multiple definition of `key_callback(GLFWwindow*, int, int, int, int)'; main.o:main.cpp:(.text+0x270): first defined here
/usr/bin/ld: main.o: in function `Update()':
main.cpp:(.text+0x290): multiple definition of `Update()'; main.o:main.cpp:(.text+0x290): first defined here
/usr/bin/ld: main.o: in function `gl()':
main.cpp:(.text+0x2a0): multiple definition of `gl()'; main.o:main.cpp:(.text+0x2a0): first defined here
/usr/bin/ld: main.o:(.bss+0x10): multiple definition of `window'; main.o:(.bss+0x10): first defined here
/usr/bin/ld: main.o:(.bss+0x8): multiple definition of `shader'; main.o:(.bss+0x8): first defined here
/usr/bin/ld: main.o:(.bss+0x0): multiple definition of `mesh'; main.o:(.bss+0x0): first defined here
/usr/bin/ld: main.o: in function `resize(int, int)':
main.cpp:(.text+0x390): multiple definition of `resize(int, int)'; main.o:main.cpp:(.text+0x390): first defined here
/usr/bin/ld: main.o: in function `LoadToVAO(std::vector<float, std::allocator<float> >, std::vector<unsigned int, std::allocator<unsigned int> >, std::vector<float, std::allocator<float> >, char const*, int, int)':
main.cpp:(.text+0x3a0): multiple definition of `LoadToVAO(std::vector<float, std::allocator<float> >, std::vector<unsigned int, std::allocator<unsigned int> >, std::vector<float, std::allocator<float> >, char const*, int, int)'; main.o:main.cpp:(.text+0x3a0): first defined here
/usr/bin/ld: main.o: in function `vecTofloat(std::vector<glm::vec<2, float, (glm::qualifier)0>, std::allocator<glm::vec<2, float, (glm::qualifier)0> > >)':
main.cpp:(.text+0xba0): multiple definition of `vecTofloat(std::vector<glm::vec<2, float, (glm::qualifier)0>, std::allocator<glm::vec<2, float, (glm::qualifier)0> > >)'; main.o:main.cpp:(.text+0xba0): first defined here
/usr/bin/ld: main.o: in function `vecTofloat(std::vector<glm::vec<3, float, (glm::qualifier)0>, std::allocator<glm::vec<3, float, (glm::qualifier)0> > >)':
main.cpp:(.text+0xca0): multiple definition of `vecTofloat(std::vector<glm::vec<3, float, (glm::qualifier)0>, std::allocator<glm::vec<3, float, (glm::qualifier)0> > >)'; main.o:main.cpp:(.text+0xca0): first defined here
/usr/bin/ld: main.o: in function `loadOBJ(char const*, char const*, int, int)':
main.cpp:(.text+0xe20): multiple definition of `loadOBJ(char const*, char const*, int, int)'; main.o:main.cpp:(.text+0xe20): first defined here
/usr/bin/ld: main.o: in function `initGL()':
main.cpp:(.text+0x1b30): multiple definition of `initGL()'; main.o:main.cpp:(.text+0x1b30): first defined here
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x20): undefined reference to `main'
/usr/bin/ld: oglwidget.o: in function `OGLWidget::initializeGL()':
oglwidget.cpp:(.text+0x61): undefined reference to `GL::initGL()'
/usr/bin/ld: oglwidget.o: in function `OGLWidget::paintGL()':
oglwidget.cpp:(.text+0x71): undefined reference to `GL::gl()'
/usr/bin/ld: oglwidget.o: in function `OGLWidget::resizeGL(int, int)':
oglwidget.cpp:(.text+0x85): undefined reference to `GL::resize(int, int)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:167: Rachaita3DFrontend] Error 1

这里有一个简单的例子。

main1.cpp:

#include "utils1.h"
#include "main1.h"
void printSomething2{
printSomething();
}

main1.h:

#ifndef MAIN1_H_INCLUDED
#define MAIN1_H_INCLUDED
class foo{
public:
static void printSomething();
};
#endif

utils1.h:

#ifndef UTILS1_H_INCLUDED
#define UTILS1_H_INCLUDED
#include <iostream>
void printSomething(){
std::cout<<"something.";
}
#endif

调用printSomething((的.cpp文件示例

#include "main1.h"
int main(){
foo::printSomething();
return 0;
}

编译Example.cpp:后的GCC日志

/usr/bin/ld: /tmp/cc4ft1pF.o: in function `main':
Ogl1.cpp:(.text+0x5): undefined reference to `foo::printSomething()'
collect2: error: ld returned 1 exit status

此命令错误:

g++ -Wl,-O1 -o Rachaita3DFrontend main.o mainwindow.o oglwidget.o main.o moc_mainwindow.o -lGLEW -lglfw -lGLX -lSOIL -lstdc++ -lQt5Widgets -lQt5Gui -lQt5Core /usr/lib/x86_64-linux-gnu/libGL.so -lpthread

它不应该有两次main.o。您在构建配置中的某个地方(而不是源代码中(犯了一个错误。警告表明您的Makefile中有两个不同的main.o条目。

最后的"简单示例"中的问题是,您从未定义foo::printSomething。这是与printSomething(不合格(不同的函数。

相关内容

  • 没有找到相关文章

最新更新