首页 > 其他分享 >vs+qt中使用opengl及关键报错“无法打开包括文件: no such file or directory”与“error LNK2001: 无法解析的外部符号 "public: vir

vs+qt中使用opengl及关键报错“无法打开包括文件: no such file or directory”与“error LNK2001: 无法解析的外部符号 "public: vir

时间:2024-01-30 16:13:17浏览次数:22  
标签:qt opengl LNK2001 void OpenGLWidget openglwidget vs 报错 GL

参考链接
https://blog.csdn.net/qq_22533607/article/details/79792083
http://t.csdnimg.cn/T8II5
http://t.csdnimg.cn/JP8k7

基础准备:vs中配置qt插件(略)

关键步骤:

  1. 创建Qt Widget Application项目
    image
  2. 将Base Class修改成QWidget,方框中的内容可以不勾,个人习惯
    image
  3. ui文件中添加opengl控件并提升为OpenGLWidget(可以另外修改成其他的名字,与之后在文件中新建的类名一致即可),控件具体名称可以不管
    image
    image
    image
  4. 添加新类,名称openglwidget(与刚才提升的类名一致,但小写)
    image
  5. main.cpp, invisual.cpp, invisual.h文件全保持不变,对openglwidget.cpp和openglwidget.h进行修改(注意:根据功能变化main.cpp, invisual.cpp, invisual.h或许可以添加与opengl有关的代码,此处只是为了说明opengl在qt中不需要特别包含特别调用,直接重写一个新类也可以运行)
// openglwidget.cpp
#include "openglwidget.h"

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"}\n\0";

OpenGLWidget::OpenGLWidget(QWidget* parent)
	: QOpenGLWidget(parent) {}

OpenGLWidget::~OpenGLWidget() {
	makeCurrent();
	glDeleteBuffers(1, &VBO);
	glDeleteVertexArrays(1, &VAO);
	shaderProgram.release();
	doneCurrent();
}

void OpenGLWidget::initializeGL() {
	initializeOpenGLFunctions();
	// link shaders
	shaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
	shaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
	shaderProgram.link();
	float vertices[] = {
		-0.5f, -0.5f, 0.0f, // left  
		 0.5f, -0.5f, 0.0f, // right 
		 0.0f,  0.5f, 0.0f  // top  
	};
	glGenVertexArrays(1, &VAO);
	glGenBuffers(1, &VBO);
	glBindVertexArray(VAO);
	glBindBuffer(GL_ARRAY_BUFFER, VBO);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
	// position attribute
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
}

void OpenGLWidget::paintGL() {
	glClearColor(0.2f, 0.3f, 0.2f, 1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	shaderProgram.bind();
	glBindVertexArray(VAO);
	glDrawArrays(GL_TRIANGLES, 0, 3);
}

void OpenGLWidget::resizeGL(int w, int h) {
	glViewport(0, 0, w, h);
}


//openglwidget.h
#pragma once
#include <QOpenGLWidget>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLShaderProgram>
#include <iostream>

class OpenGLWidget : public QOpenGLWidget, QOpenGLFunctions_3_3_Core {
	Q_OBJECT
public:
	OpenGLWidget(QWidget* parent);
	~OpenGLWidget();
protected:
	void initializeGL();
	void resizeGL(int w, int h);
	void paintGL();
private:
	QOpenGLShaderProgram shaderProgram;
	unsigned int VBO;
	unsigned int VAO;
};

5.编译运行错误及解决办法:
错误1:报错“无法打开包括文件...”
image
解决办法:在项目属性中进行修改,包含目录中添加$(ProjectDir)(或直接填写./),表示当前工作目录,即“.vcxproj”及其类似文件所在目录
image
image

错误2:报错“无法解析的外部符号...”

image
解决办法:在头文件属性(此处是openglwidget.h)中进行修改,项类型中修改为Qt Meta-Object Compiler (moc)
image
image

  1. 编译成功
    image

注意:

  1. 本次项目属性中并没有勾选OpenGL或OpenGL Extensions,可见vs+qt中使用opengl勾选这两个模块并不是必须的
    image
  2. 上述两个报错在涉及vs与qt的项目中有类似错误可以继续参考。

标签:qt,opengl,LNK2001,void,OpenGLWidget,openglwidget,vs,报错,GL
From: https://www.cnblogs.com/Yami-Wa/p/17997328

相关文章

  • three学习-vscode-创建项目01
    安装node之后,使用vscode打开自己创建的一个文件夹1、创建我们的项目目录存放的文件夹study01再vscode中打开创建的文件夹2、创建vite支持环境npminitvite@latest输入y输入项目名称:studyapp01框架选择 3、进入项目,安装依赖,运行项目cdstudyapp01npminstallnpm......
  • kubeadm init过程报错解决
    1、问题kubeadminit初始化kubesphere集群的时候遇到如下错误:1)错误1:W061315:49:11.5500292140images.go:80]couldnotfindofficiallysupportedversionofetcdforKubernetesv1.27.2,fallingbacktothenearestetcdversion(3.5.7-0)[wait-control-plane]......
  • CVSD和mSBC的区别
    CVSD和mSBC的区别CVSD(ContinuousVariableSlopeDeltamodulation)和mSBC(ModifiedSBC,ModifiedSubBandCoding)是两种不同的音频编解码器,它们在蓝牙音频传输中用于不同的目的。在蓝牙的Hands-FreeProfile(HFP)中,这两种编解码器常被用于语音传输。CVSD介绍:CV......
  • window下VScode 使用虚拟环境virtualenv
    前提:搭建python环境,这里以python3X为例一经典virtualenv1-安装virtualenvpipinstallvirtualenv2-创建虚拟环境virtualenvmyenv 3-激活虚拟环境./myenv/Script/activate4-退出虚拟环境cd./myenv/Scriptdeactivate -----以下选操作---前提条件:已安装virtualen......
  • MySQL建索引报错:BLOB/TEXT column used in key specification without a key length
    MySQL建索引报错:BLOB/TEXTcolumnusedinkeyspecificationwithoutakeylength因为text类型的字段值太长,没办法为全部内容建立索引,只能指定前多少位字符建立索引;就像这样createindex`索引名`on表名(字段名(600));所以能用varchar能放下的尽量使用varchar吧......
  • pyinstaller -noconsole报错win32ctypes.pywin32.pywintypes.error: (225, '', '无法
    将pyinstaller6.3.0,卸载后,安装6.2.0重新打包即可https://www.cnblogs.com/uoky/p/17916300.html但是使用-w或者--noconsole就会报错win32ctypes.pywin32.pywintypes.error:(225,'','无法成功完成操作,因为文件包含bingdu或潜在的垃圾软件。')......
  • IDEA编译报错:Error:Kotlin: Module was compiled with an incompatible version of Ko
    问题Error:Kotlin:ModulewascompiledwithanincompatibleversionofKotlin.Thebinaryversionofitsmetadatais1.6.0,expectedversionis1.1.13.Warning:Kotlin:RuntimeJARfilesintheclasspathshouldhavethesameversion.Thesefileswerefoundi......
  • 文心一言 VS 讯飞星火 VS chatgpt (189)-- 算法导论14.1 6题
    六、用go语言,在OS-SELECT或OS-RANK中,注意到无论什么时候引用结点的size属性都是为了计算一个秩。相应地,假设每个结点都存储它在以自己为根的子树中的秩。试说明在插入和删除时,如何维护这个信息。(注意,这两种操作都可能引起旋转。)文心一言:在OS-SELECT和OS-RANK中,我们维护一个树形结......
  • vue安装node-sass sass-loader 报错
    vue安装node-sass  sass-loader报错 主要的原因是因为node的版本 和  node-sass  sass-loader 不匹配导致的报错node-sass版本和sass-loader版本对应 具体查看这个链接   https://www.npmjs.com/package/node-sass拿我当前项目举例  我node版本是16......
  • 关于ufw 报错ip6tables v1.6.1: can't initialize ip6tables table `filter': Table d
    背景在ubuntuarm版本上安装ufw,设置规则时报错发现报错ip6tablesv1.6.1:can'tinitializeip6tablestable`filter':Tabledoesnotexist(doyouneedtoinsmod?)Perhapsip6tablesoryourkernelneedstobeupgraded.解决办法一.升级ip6tables二.禁用i......