Difference between revisions of "VTK/Tutorials/CMakeListsFile"

From KitwarePublic
< VTK‎ | Tutorials
Jump to navigationJump to search
(vtkHybrid --> ${VTK_LIBRARIES})
 
Line 4: Line 4:
 
<source lang=cmake>
 
<source lang=cmake>
 
cmake_minimum_required(VERSION 2.6)
 
cmake_minimum_required(VERSION 2.6)
PROJECT(Test)
+
project(Test)
 +
set(VTK_DIR "PATH/TO/VTK/BUILD/DIRECTORY")
 +
find_package(VTK REQUIRED)
 +
include(${VTK_USE_FILE})
 +
add_executable(Test Test.cxx)
 +
target_link_libraries(Test ${VTK_LIBRARIES})
  
FIND_PACKAGE(VTK REQUIRED)
+
</source>
INCLUDE(${VTK_USE_FILE})
 
  
ADD_EXECUTABLE(Test Test.cpp)
 
TARGET_LINK_LIBRARIES(Test vtkHybrid)
 
  
</source>
+
==Explanation==
  
 
Here is a line by line break down of what is going on:
 
Here is a line by line break down of what is going on:
Line 22: Line 24:
  
 
Name the project.
 
Name the project.
<source lang=cmake>
+
<source lang=cmake>project(Test)</source>
PROJECT(Test)
 
</source>
 
  
 
Tell cmake that this project needs to use VTK.
 
Tell cmake that this project needs to use VTK.
 
<source lang=cmake>
 
<source lang=cmake>
FIND_PACKAGE(VTK REQUIRED)
+
set(VTK_DIR "/PATH/TO/VTK/BUILD/DIRECTORY")
INCLUDE(${VTK_USE_FILE})
+
find_package(VTK REQUIRED)
 +
include(${VTK_USE_FILE})
 
</source>
 
</source>
  
 
Tell cmake we want to compile Test.cpp into a binary called Test.
 
Tell cmake we want to compile Test.cpp into a binary called Test.
 
<source lang=cmake>
 
<source lang=cmake>
ADD_EXECUTABLE(Test Test.cpp)
+
add_executable(Test Test.cxx)
 
</source>
 
</source>
  
Tell cmake we need to link Test against vtkHybrid. This is the "catch all" VTK library. If you don't know what to link to, most of the time vtkHybrid will have what you need.
+
Tell cmake we need to link Test against the VTK libraries.
 
<source lang=cmake>
 
<source lang=cmake>
TARGET_LINK_LIBRARIES(Test vtkHybrid)
+
target_link_libraries(Test ${VTK_LIBRARIES})
 
</source>
 
</source>

Latest revision as of 13:53, 14 August 2012

To use CMake to generate your project, this is what the CMakeLists.txt file should look like:

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
project(Test)
set(VTK_DIR "PATH/TO/VTK/BUILD/DIRECTORY")
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
add_executable(Test Test.cxx)
target_link_libraries(Test ${VTK_LIBRARIES})


Explanation

Here is a line by line break down of what is going on:

This will prevent annoying cmake warnings about "you must specifiy a minimum required version".

cmake_minimum_required(VERSION 2.6)

Name the project.

project(Test)

Tell cmake that this project needs to use VTK.

set(VTK_DIR "/PATH/TO/VTK/BUILD/DIRECTORY")
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})

Tell cmake we want to compile Test.cpp into a binary called Test.

add_executable(Test Test.cxx)

Tell cmake we need to link Test against the VTK libraries.

target_link_libraries(Test ${VTK_LIBRARIES})