Examples.html 3.0 KB

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