【发布时间】:2020-03-12 21:40:01
【问题描述】:
我有在 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
Example.cpp 文件调用 printSomething()
#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
【问题讨论】:
-
欢迎来到 Stackoverflow。为了鼓励其他用户帮助您解决问题,请考虑将您的程序转换为(最小示例)[stackoverflow.com/help/minimal-reproducible-example].
-
我用一个简单的例子更新了这个问题。
-
这个命令是错误的:
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) -
好的,我会回答的