CMake入门——CMakeLists.txt执行

CMakeLists.txt执行,有两种方法。第一种使用cmake-gui.exe,可视化界面,简单易懂。第二种使用*.bat脚本,批量传参,高效快速。

准备:新建目录E:\vtk_Exercise,在该目录下新建3个文件,分别是CMakeLists.txt,RenderCylinder.h,RenderCylinder.cpp,RunCmakeX64.bat

第一种使用步骤

step1:编写CMakeLists.txt,示例如下

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
MARK_AS_ADVANCED(CMAKE_BACKWARDS_COMPATIBILITY)
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
SET(VTK_DIR E:/Thirdparty/x64/VTK)
 
project(RenderCylinder)
 
INCLUDE_DIRECTORIES(
    ${VTK_DIR}/include/vtk-6.0
)
LINK_DIRECTORIES(
    ${VTK_DIR}/lib/${ConfigurationName}
)
add_executable(${PROJECT_NAME} RenderCylinder.cpp RenderCylinder.h)
 
target_link_libraries(${PROJECT_NAME}
	vtkFiltersGeometry-6.0
	vtkIOGeometry-6.0
	vtkIOCore-6.0
	vtkCommonCore-6.0
	vtkInteractionStyle-6.0
	vtkRenderingOpenGL-6.0
	vtkCommonDataModel-6.0
	vtkfilterscore-6.0
	vtkRenderingCore-6.0
	vtkCommonExecutionModel-6.0
	vtkFiltersSources-6.0
)

step2:打开cmake-gui.exe,如下图。

1是选择最外层的CMakeLists.txt所在目录(本例中只有一层),点击Browse Source浏览目录,

2是设置生成的工程目录,可任意设置,为方便管理,设置成E:\vtk_Exercise\bin

点击3后,选择编译环境,本人是Visual Studio 11 win64,然后单击Finish,配置完成

点击4后,生成工程(该步骤即执行CMakeLists.txt

CMake入门——CMakeLists.txt执行_第1张图片

第二种使用步骤

step1:编写脚本RunCmakeX64.bat,示例如下

set rootdir=%cd%
mkdir "Build\x64"
cd "Build\x64"
"CMake.exe" -G "Visual Studio 11 Win64"  -D QT_DIR:PATH="c:\Qt\Qt5.1.1\5.1.1\msvc2012_64\lib\cmake"  -D VTK_DIR_ROOT:PATH="%rootdir%\Temp\x64\VTK"    TEMPLATE_RC_FILE:PATH="%rootdir%\TemplateFile\Template.rc"  %rootdir%
if errorlevel 1 pause
cd.. 
cd..

step2:编写CMakeLists.txt,示例如下。命令SET(VTK_DIR "${VTK_DIR_ROOT}"),建立了CMakeLists.txt和RunCmakeX64.bat的联系,即RunCmakeX64.bat传参数给CMakeLists.txt

CMAKE_MINIMUM_REQUIRED(VERSION 2.8.11)
MARK_AS_ADVANCED(CMAKE_BACKWARDS_COMPATIBILITY)
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
SET(VTK_DIR "${VTK_DIR_ROOT}")

project(RenderCylinder)

INCLUDE_DIRECTORIES(
    ${VTK_DIR}/include/vtk-6.0
)
LINK_DIRECTORIES(
    ${VTK_DIR}/lib/${ConfigurationName}
)
add_executable(${PROJECT_NAME} RenderCylinder.cpp RenderCylinder.h)

target_link_libraries(${PROJECT_NAME}
	vtkFiltersGeometry-6.0
	vtkIOGeometry-6.0
	vtkIOCore-6.0
	vtkCommonCore-6.0
	vtkInteractionStyle-6.0
	vtkRenderingOpenGL-6.0
	vtkCommonDataModel-6.0
	vtkfilterscore-6.0
	vtkRenderingCore-6.0
	vtkCommonExecutionModel-6.0
	vtkFiltersSources-6.0
)


step3:双击RunCmakeX64.bat即可。如果报错权限不够,右键管理员权限

你可能感兴趣的:(CMake)