找回密码
 用户注册

QQ登录

只需一步,快速开始

查看: 5146|回复: 0

使用Cmake搭建Qt+VTK工程(顺便推荐超好Cmake入门教程一本)

[复制链接]
发表于 2012-2-19 22:30:26 | 显示全部楼层 |阅读模式
组内的项目要从MFC+OpenGL向Qt+VTK迁移。最近的任务是先把一个小模块进行尝试性迁移。

配置环境过程中发现Qt+VTK的项目配置起来十分麻烦,于是google之,发现使用CMake这个工具可以大大简化我们的配置过程。可惜我从没接触过Cmake,于是再次google之,发现搜索到的相关教程都很是简陋,不系统也不实用。后来在机缘巧合之下找到了一份《Cmake 实践》下载:(),真是一本很好的快速入门教程,在此感谢作者。

花几分钟快速过一下,基本就可以动手了。结合网上的代码片段,我终于写出了自己的第一个Cmake脚本——可以把Qt和VTK结合起来,同时也展示了如何使用Cmake引入第三方库。脚本贴在这里供大家参考:# This script help you to generate a project which combines VTK and Qt
  1. # Author: Wei Wang
  2. # Date: 2012/2/18
  3. cmake_minimum_required(VERSION 2.8)
  4. PROJECT(LayoutProto)
  5. FIND_PACKAGE(VTK)
  6. INCLUDE(${VTK_USE_FILE}) # include UseVTK.cmake
  7. FIND_PACKAGE(Qt4 REQUIRED)
  8. INCLUDE(${QT_USE_FILE})  # include UseQt4.cmake
  9. # support for out-of-source build
  10. INCLUDE_DIRECTORIES(
  11.   ${CMAKE_CURRENT_BINARY_DIR} #this is where ui_SimpleViewUI.h is generated
  12.   ${CMAKE_CURRENT_SOURCE_DIR}
  13. )
  14. # Set your files and resources here
  15. # 1. Set path for each kind of files
  16. SET(SourceFilesPath ${PROJECT_SOURCE_DIR}/src)
  17. SET(UIFilesPath ${PROJECT_SOURCE_DIR}/ui)
  18. SET(HeadersFilesPath ${PROJECT_SOURCE_DIR}/inc)
  19. # 2. Add items into current project
  20. SET(SourceFiles main.cpp mainwindow.cpp AutoNDE2DPanel.cpp rawdatapanel.cpp)
  21. SET(UIFiles mainwindow.ui)
  22. SET(HeadersFiles mainwindow.h AutoNDE2DPanel.h rawdatapanel.h)
  23. # 3. Concatenate items and their paths
  24. FOREACH(SrcFile ${SourceFiles})
  25.     SET(SimpleViewSrcs ${SimpleViewSrcs} ${SourceFilesPath}/${SrcFile})
  26. ENDFOREACH(SrcFile)
  27. #MESSAGE(STATUS "Files: " ${SimpleViewSrcs})
  28. FOREACH(UIFile ${UIFiles})
  29.     SET(SimpleViewUI ${SimpleViewUI} ${UIFilesPath}/${UIFile})
  30. ENDFOREACH(UIFile)
  31. #MESSAGE(STATUS "UIFlie: " ${SimpleViewUI})
  32. FOREACH(HeaderFile ${HeadersFiles})
  33.     SET(SimpleViewHeaders ${SimpleViewHeaders} ${HeadersFilesPath}/${HeaderFile})
  34. ENDFOREACH(HeaderFile)
  35. #MESSAGE(STATUS "Headers: " ${SimpleViewHeaders})
  36. # Wrap items to pass them to qmake
  37. QT4_WRAP_UI(UISrcs ${SimpleViewUI})
  38. QT4_WRAP_CPP(MOCSrcs ${SimpleViewHeaders} )
  39. SOURCE_GROUP("Resources" FILES
  40.   ${SimpleViewUI}
  41. )
  42. SOURCE_GROUP("Generated" FILES
  43.   ${UISrcs}
  44.   ${MOCSrcs}
  45. )
  46. # Construct an executable
  47. ADD_EXECUTABLE( LayoutProto ${SimpleViewSrcs} ${UISrcs} ${MOCSrcs})
  48. # Link VTK libs and Qt libs to the executable
  49. TARGET_LINK_LIBRARIES( LayoutProto QVTK )
  50. # Add some 3rd party libs
  51. SET(RDTiffUtilLibPath_Release ${PROJECT_SOURCE_DIR}/lib/release)
  52. SET(RDTiffUtilLibPath_Debug ${PROJECT_SOURCE_DIR}/lib/debug)
  53. SET(RDTiffUtilLib_Release recutil.lib rdtutil.lib rdtiff.lib)
  54. SET(RDTiffUtilLib_Debug recutil.lib rdtutil.lib rdtiff.lib)
  55. # For release mode
  56. FOREACH(LibName ${RDTiffUtilLib_Release})
  57.     TARGET_LINK_LIBRARIES( LayoutProto optimized ${RDTiffUtilLibPath_Release}/${LibName})
  58. ENDFOREACH(LibName)
  59. # For debug mode
  60. FOREACH(LibName ${RDTiffUtilLib_Debug})
  61.     TARGET_LINK_LIBRARIES( LayoutProto debug ${RDTiffUtilLibPath_Debug}/${LibName})
  62. ENDFOREACH(LibName)
复制代码
我在Cmake方面也还是菜鸟,以上代码仅仅是达到能用的地步而已,缺点肯定是很多了,请各位高手多多指点!
作者:cheng_tian 发表于2012-2-19 5:58:25 原文链接

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?用户注册

×
您需要登录后才可以回帖 登录 | 用户注册

本版积分规则

Archiver|手机版|小黑屋|ACE Developer ( 京ICP备06055248号 )

GMT+8, 2024-12-22 17:02 , Processed in 0.014177 second(s), 5 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表