如何在Qt5的CMAKE中包含QGLVIEWER

时间:2019-01-14 21:48:51

标签: c++11 cmake qt5

我设置CMAKE为Qt5项目包括QGLViewer。但是,一旦我尝试编译项目,CMAKE就会因以下错误而停止:

CMake错误:此项目中使用了以下变量,但是将它们设置为NOTFOUND。 请设置它们或确保在CMake文件中对其进行了正确设置和测试: QGLVIEWER_LIBRARY 在目录/ home / emanuele / sfm中由目标“ sfm”链接

QGLViewer位于以下路径上:usr/include/QGLViewer,因为可以从此打印屏幕上看到:

Print Screen of the File Path This is the file path

这是我在CMAKE上设置路径的方法:

CMAKE File Path This is the file path in CMAKE

此外,我发现this source有助于理解如何声明lib。另外,this post可以帮助我指出正确的路线,但仍然无法正常工作。

导致问题的最重要的CMAKE部分下面:

cmake_minimum_required(VERSION 3.11)
project(sfm)

########### options ###############
SET(USE_QT TRUE CACHE BOOL)
########### END options ###########

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g")
set(CMAKE_INCLUDE_CURRENT_DIR ON)

########### OpenMP ################
find_package(OpenMP)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
########### END OpenMP ############

########### OpenCV ################
set (OpenCV_DIR /home/emanuele/opencv/build)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
list(APPEND LINK_LIBRARIES ${OpenCV_LIBS})
########### END OpenCV ############

########### Qt 5 ##################
IF (USE_QT)
  ADD_DEFINITIONS("-DUSE_QT")
  find_package(Qt5Widgets REQUIRED)
  find_package(Qt5Xml REQUIRED)
  find_package(Qt5OpenGL REQUIRED)

  include_directories(${Qt5Widgets_INCLUDE_DIRS}
      ${Qt5Xml_INCLUDE_DIRS}
      ${Qt5OpenGL_INCLUDE_DIRS}
  )
  list(APPEND LINK_LIBRARIES Qt5::Widgets Qt5::Xml Qt5::OpenGL)
  set(CMAKE_AUTOMOC ON)
  set(CMAKE_AUTOUIC ON)

  include_directories(src/ui/mainwindow src/ui/widgets)

  find_package(OpenGL REQUIRED)
  find_library(QGLVIEWER_LIBRARY
      NAMES qglviewer-qt5 qglviewer QGLViewer QGLViewer2)
  list(APPEND LINK_LIBRARIES ${QGLVIEWER_LIBRARY}  ${OPENGL_LIBRARIES})
ENDIF (USE_QT)
########### END Qt 5 ##############

########### sources ###############
file(GLOB_RECURSE SOURCE_FILES "src/*.cpp")
add_executable(sfm ${SOURCE_FILES})
########### END sources ###########

########### headers ###############
include_directories(src)
########### END headers ###########

########### link libraries ########
target_link_libraries(sfm ${LINK_LIBRARIES})
########### END link libraries ####

我希望CMAKE能够编译,但是实际结果显示在下面的打印屏幕中,这使我无法继续前进:

Print Screen of the Error This is the file path

1 个答案:

答案 0 :(得分:1)

您应该在CMAKE_PREFIX_PATH变量中设置qglviewer库的路径

或者您也可以使用qglviewer tar gz归档文件作为输入将其集成为新目标:

set(QGLVIEWER_FILES ${CMAKE_BINARY_DIR}/libQGLViewer-2.7.1/QGLViewer/qglviewer.h)

get_target_property (QT_QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)

add_custom_command(OUTPUT ${QGLVIEWER_FILES}
  COMMAND  tar xzf "${CMAKE_CURRENT_SOURCE_DIR}/libQGLViewer-2.7.1.tar.gz" --strip 1
  COMMAND ${QT_QMAKE_EXECUTABLE} -o QMakefile 
  COMMAND make -f QMakefile 
#  COMMAND ${CMAKE_COMMAND} -E touch ${LIBFOO_TAR_HEADERS}
  WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/libQGLViewer-2.7.1"
  DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/libQGLViewer-2.7.1.tar.gz"
  COMMENT "Unpacking libQGLViewer-2.7.1.tar.gz"
  VERBATIM
)

add_custom_target(qglviewer_untar DEPENDS ${QGLVIEWER_FILES})

add_library(qglviewer SHARED IMPORTED)
add_dependencies(qglviewer qglviewer_untar)

# link qglviewer
set_target_properties(qglviewer PROPERTIES
                      IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/libQGLViewer-2.7.1/QGLViewer/libQGLViewer-qt5.so"
                      INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_BINARY_DIR}/libQGLViewer-2.7.1")