Ubuntu下自己编译了QVTKWidget,在使用的过程中报错
错误信息:
: error while loading shared libraries: libQt5X11Extras.so.5: cannot open shared object file: No such file or directory
或显示如下错误:
问题原因:
我们将QT安装到了opt目录下面,导致在系统目录下找不到对应的QT库文件
解决方式一
第一步:修改cmakelists.txt文件,指定QT目录为我们安装的QT目录
在find_package之前添加QT的cmake路径
set(CMAKE_PREFIX_PATH “/home/ueg-pc1/Qt5.12.5/gcc_64/lib/cmake”)
find_package (Qt5 COMPONENTS Core Widgets REQUIRED)
或在qt界面上配置:CMAKE_PREFIX_PATH /home/ueg-pc1/Qt5.12.5/gcc_64/lib/cmake
第二步:清除CMake在build目录下面的缓存文件,清空build目录即可一定要做这一步
第三步:运行报错后,将QT库目录添加到LD_LIBRARY_PATH中,运行成功
设置环境变量
cmd: sudo gedit ~/.bashrc
//****************在bashrc文件末尾添加
export QTDIR=/home/ueg-pc1/Qt5.12.5/5.12.5/gcc_64
export PATH=$QTDIR/bin:$PATH
export MANPATH=$QTDIR/man:$MANPATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
//****************end
cmd: source ~/.bashrc
尝试的其它无效的方法,在此记录一下,主要是设置环境变量:
cmd:sudo gedit /etc/profile
//****************在文件末尾添加
export PATH=/home/ueg-pc1/Qt5.12.5/Tools/QtCreator/bin:$PATH
export QTDIR=/home/ueg-pc1/Qt5.12.5/5.12.5/gcc_64
export PATH=$QTDIR/bin:$PATH
export LD_LIBRARY_PATH=$QTDIR/lib:$LD_LIBRARY_PATH
//****************end
cmd: source /etc/profile
cmd :sudo gedit /etc/ld.so.conf
cmd:sudo ldconfig
此部分只需要设置红色部分,其它颜色不用设置。
第四,此时可以在终端运行程序,可以找到链接库,但是在qt界面上的编译运行,仍然存在找不到库的问题。
在终端运行:正常
cmake untitled2
make
./untitled22
第五,在qt运行出现问题设置,手动添加LD_LIBRARY_PATH环境变量/home/ueg-pc1/Qt5.12.5/5.12.5/gcc_64/lib,系统添加了,但是qt上run没有添加,这就是问题所在。
CMAKE:
cmake_minimum_required(VERSION 3.10)
project(VTKTest)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_PREFIX_PATH "/home/ueg-pc1/Qt5.12.5/5.12.5/gcc_64/lib/cmake")
find_package (Qt5 COMPONENTS Core Widgets REQUIRED)
FIND_PACKAGE(VTK REQUIRED)
IF (VTK_FOUND)
MESSAGE(STATUS "VTK found.")
INCLUDE(${VTK_USE_FILE})
ENDIF()
IF(Qt5_FOUND)
MESSAGE(STATUS "Qt5 found.")
ENDIF()
add_executable(VTKTest main.cpp)
target_link_libraries(VTKTest
${VTK_LIBRARIES})
SOURCE:
#include <iostream>
//VTK includes
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCylinderSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <QVTKOpenGLWidget.h>
#include <QApplication>
#include <QVBoxLayout>
#include <QSurfaceFormat>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget w;// = new QWidget(0);
QVBoxLayout layout;// = new QVBoxLayout(0);
QVTKOpenGLWidget v;// = new QVTKOpenGLWidget();
v.setFixedSize(300, 300);
layout.addWidget(&v);
w.setLayout(&layout);
auto cylinder = vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetHeight(3.0);
cylinder->SetRadius(1.0);
cylinder->SetResolution(10);
auto cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
auto cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
auto renderer = vtkSmartPointer<vtkRenderer>::New();
renderer->AddActor(cylinderActor);
renderer->SetBackground(0.1, 0.2, 0.4);
auto renWin = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
renWin->AddRenderer(renderer);
v.SetRenderWindow(renWin);
w.show();
return a.exec();
}
测试后发现,第一步的环境变量可以不设置。
参考资料:
VTKwidget
https://www.jianshu.com/p/c9d1c623878f