NetCDF  4.9.3
building-with-cmake.md
1 Build Instructions for NetCDF-C using CMake {#netCDF-CMake}
2 ===========================================
3 
4 [TOC]
5 
6 # Overview {#cmake_overview}
7 
8 Starting with netCDF-C 4.3.0, we are happy to announce the inclusion
9 of CMake support. CMake will allow for building netCDF on a wider
10 range of platforms, include Microsoft Windows with Visual Studio.
11 CMake support also provides robust unit and regression testing tools.
12 We will also maintain the standard autotools-based build system in
13 parallel.
14 
15 In addition to providing new build options for netCDF-C, we will also
16 provide pre-built binary downloads for the shared versions of netCDF
17 for use with Visual Studio.
18 
19 
20 # Requirements {#cmake_requirements}
21 The following packages are required to build netCDF-C using CMake.
22 
23 * netCDF-C Source Code
24 * CMake version 2.8.12 or greater.
25 * Optional Requirements:
26  * HDF5 Libraries for netCDF4/HDF5 support.
27  * libcurl for DAP support.
28 
29 <center>
30 <img src="deptree.jpg" height="250px" />
31 </center>
32 
33 # The CMake Build Process {#cmake_build}
34 
35 There are four steps in the Build Process when using CMake
36 
37 1. Configuration: Before compiling, the software is configured based on the desired options.
38 2. Building: Once configuration is complete, the libraries are compiled.
39 3. Testing: Post-build, it is possible to run tests to ensure the functionality of the netCDF-C libraries.
40 4. Installation: If all tests pass, the libraries can be installed in the location specified during configuration.
41 
42 For users who prefer pre-built binaries, installation packages are available at \ref winbin
43 
44 ## Configuration {#cmake_configuration}
45 
46 The output of the configuration step is a project file based on the appropriate configurator specified. Common configurators include:
47 
48 * Unix Makefiles
49 * Visual Studio
50 * CodeBlocks
51 * ... and others
52 
53 ### Common CMake Options {#cmake_common_options}
54 
55 | **Option** | **Autotools** | **CMake** |
56 | :------- | :---- | :----- |
57 Specify Install Location | --prefix=PREFIX | -D"CMAKE\_INSTALL\_PREFIX=PREFIX"
58 Enable/Disable netCDF-4 | --enable-netcdf-4<br>--disable-netcdf-4 | -D"ENABLE\_NETCDF\_4=ON" <br> -D"ENABLE\_NETCDF\_4=OFF"
59 Enable/Disable DAP | --enable-dap <br> --disable-dap | -D"ENABLE\_DAP=ON" <br> -D"ENABLE\_DAP=OFF"
60 Enable/Disable Utilities | --enable-utilities <br> --disable-utilities | -D"BUILD\_UTILITIES=ON" <br> -D"BUILD\_UTILITIES=OFF"
61 Specify shared/Static Libraries | --enable-shared <br> --enable-static | -D"BUILD\_SHARED\_LIBS=ON" <br> -D"BUILD\_SHARED\_LIBS=OFF"
62 Enable/Disable Tests | --enable-testsets <br> --disable-testsets | -D"ENABLE\_TESTS=ON" <br> -D"ENABLE\_TESTS=OFF"
63 Specify a custom library location | Use *CFLAGS* and *LDFLAGS* | -D"CMAKE\_PREFIX\_PATH=/usr/custom_libs/"
64 
65 A full list of *basic* options can be found by invoking `cmake [Source Directory] -L`. To enable a list of *basic* and *advanced* options, one would invoke `cmake [Source Directory] -LA`.
66 
67 ### Configuring your build from the command line. {#cmake_command_line}
68 
69 The easiest configuration case would be one in which all of the dependent libraries are installed on the system path (in either Unix/Linux or Windows) and all the default options are desired. From the build directory (often, but not required to be located within the source directory):
70 
71 > $ cmake [Source Directory]
72 
73 If you have libraries installed in a custom directory, you may need to specify the **CMAKE\_PREFIX_PATH** variable to tell cmake where the libraries are installed. For example:
74 
75 > $ cmake [Source Directory] -DCMAKE\_PREFIX\_PATH=/usr/custom_libraries/
76 
77 #### Building with Parallel I/O. {#cmake_parallel_io}
78 
79 NetCDF will build with parallel I/O if the C compiler is an MPI
80 compiler, and HDF5 was built for parallel I/O. To build netcdf-c for
81 parallel I/O, first build HDF5 for parallel I/O, then build netcdf-c
82 like this:
83 
84 > $ CC=mpicc cmake [Source Directory] -DNETCDF\_ENABLE\_PARALLEL_TESTS=ON
85 
86 The parallel I/O tests will only run if the additional configure
87 option is used: NETCDF_ENABLE_PARALLEL_TESTS. Those tests run (by default)
88 with mpiexec, on 4, 16, or 32 processors. If mpiexec cannot be used on
89 your login-nodes, a different command can be used to launch the
90 parallel I/O tests. Used the NETCDF_MPIEXEC option to set a
91 different parallel I/O job launcher:
92 
93 > $ CC=mpicc cmake [Source Directory] -DNETCDF\_ENABLE\_PARALLEL_TESTS=ON -DNETCDF\_MPIEXEC='srun -A account'
94 
95 ## Building {#cmake_building}
96 
97 The compiler can be executed directly with 'make' or the appropriate command for the configurator which was used.
98 
99 > $ make
100 
101 Building can also be executed indirectly via cmake:
102 
103 > $ cmake --build [Build Directory]
104 
105 ## Testing {#cmake_testing}
106 
107 Testing can be executed several different ways:
108 
109 > $ make test
110 
111 or
112 
113 > $ ctest
114 
115 or
116 
117 > $ cmake --build [Build Directory] --target test
118 
119 ## Installation {#cmake_installation}
120 
121 Once netCDF has been built and tested, it may be installed using the following commands:
122 
123 > $ make install
124 
125 or
126 
127 > $ cmake --build [Build Directory] --target install
128 
129 # See Also {#cmake_see_also}
130 
131 For further information regarding NetCDF and CMake, see \ref cmake_faq