Examples.html 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <html>
  3. <!--#include virtual="/CMake/HTML/Head.html"-->
  4. <body bgcolor="#FFFFFF" leftmargin=0 topmargin=0 text="black" link="#FFFFFF"
  5. vlink="#000000" alink="#000000">
  6. <!--#include virtual="/CMake/HTML/Table.html"-->
  7. <tr>
  8. <!--#include virtual="/CMake/HTML/SideBar.html"-->
  9. <td width="550" valign="top" bgcolor="#FFFFFF">
  10. <div align="left">
  11. <font size=5>The following example </font> demonstrates some key ideas
  12. of CMake. (You may wish to download this
  13. <a href="/CMake/HTML/cmakeExample.tar.gz">example code</a> and try it
  14. out for yourself.) Make sure that you have CMake installed prior to
  15. running this example (go <a href="/CMake/HTML/Install.html">here</a>
  16. for instructions).
  17. <p>
  18. There are three directories involved. The top level directory has two
  19. subdirectories called ./Demo and ./Hello. In the directory ./Hello, a
  20. library is built. In the directory ./Demo, an executable is built by
  21. linking to the library. A total of three CMakeList.txt files are
  22. created: one for each directory.
  23. <p>
  24. The first, top-level directory contains the following CMakeLists.txt file.
  25. <pre>
  26. # The name of our project is "HELLO". CMakeLists files in this project can
  27. # refer to the root source directory of the project as ${HELLO_SOURCE_DIR} and
  28. # to the root binary directory of the project as ${HELLO_BINARY_DIR}.
  29. PROJECT(HELLO)
  30. # Recurse into the "Hello" and "Demo" subdirectories. This does not actually
  31. # cause another cmake executable to run. The same process will walk through
  32. # the project's entire directory structure.
  33. SUBDIRS(Hello Demo)
  34. </pre>
  35. Then for each subdirectory listed in the SUBDIRS command, CMakeLists.txt
  36. files are created. In the ./Hello directory, the following CMakeLists.txt
  37. file is created:
  38. <pre>
  39. # Create a library called "Hello" which includes the source file "hello.cxx".
  40. # The extension is already found. Any number of sources could be listed here.
  41. ADD_LIBRARY(Hello hello)
  42. </pre>
  43. Finally, in the ./Demo directory, the third and final CMakeLists.txt file
  44. is created:
  45. <pre>
  46. # Make sure the compiler can find include files from our Hello library.
  47. INCLUDE_DIRECTORIES(${HELLO_SOURCE_DIR}/Hello)
  48. # Make sure the linker can find the Hello library once it is built.
  49. LINK_DIRECTORIES(${HELLO_BINARY_DIR}/Hello)
  50. # Add executable called "helloDemo" that is built from the source files
  51. # "demo.cxx" and "demo_b.cxx". The extensions are automatically found.
  52. ADD_EXECUTABLE(helloDemo demo demo_b)
  53. # Link the executable to the Hello library.
  54. TARGET_LINK_LIBRARIES(helloDemo Hello)
  55. </pre>
  56. CMake when executed in the top-level directory will process the
  57. CMakeLists.txt file and then descend into the listed subdirectories.
  58. Variables, include paths, library paths, etc. are inherited. Depending
  59. on the system, makefiles (Unix) or workspaces/projects (MSVC) will be
  60. built. These can then be used in the usual way to build the code.
  61. </div>
  62. </td>
  63. </tr>
  64. </table>
  65. </td>
  66. </tr>
  67. </table>
  68. </body>
  69. </html>