Salut à tous !

Je dois lire des fichiers au format NetCDF en C++. Pour construire mon projet, j’utilise CMake. Cependant, j’ai quelques difficultés avec la bibliothèque NetCDF, de toute évidence je ne parviens pas à réaliser l’édition des liens. J’ai réalisé un exemple complet minimal pour montrer mon problème. Soit le contenu du fichier « main.cpp » suivant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <netcdf>
 
int main (int argc, char** argv) {
	if (argc != 2) {
		std::cerr << "Usage*: " << argv[0] << " nom_fichier\n";
		return -1;
	}
 
    netCDF::NcFile dataFile (argv[1], netCDF::NcFile::read);
 
    return 0;
}
Ainsi que le fichier « CMakeLists.txt » suivant :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
cmake_minimum_required(VERSION 3.0)
 
project(TestNetCDF CXX)
 
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
set(EXECUTABLE_NAME testnetcdf)
 
find_package(netCDF COMPONENTS CXX)
 
set(
    SOURCE_FILES
    main.cpp
)
 
link_directories(
    ${NETCDF_CXX_LIBRARIES}
)
include_directories(
	${CMAKE_CURRENT_BINARY_DIR}
    ${NETCDF_CXX_INCLUDE_DIRS}
)
 
add_executable(
    ${EXECUTABLE_NAME}
    ${SOURCE_FILES}
)
target_link_libraries(
    ${EXECUTABLE_NAME}
    ${NETCDF_CXX_LIBRARIES}
)
Comme il s’agit d’un exemple complet, certes, mais aussi minimal, j’ai tout mis dans le même répertoire. Après la génération des fichiers de constructions, voici ce que donne la compilation :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ make 
Scanning dependencies of target testnetcdf
[ 50%] Building CXX object CMakeFiles/testnetcdf.dir/main.cpp.o
[100%] Linking CXX executable bin/Debug/testnetcdf
CMakeFiles/testnetcdf.dir/main.cpp.o*: Dans la fonction «*main*»*:
/home/lebarsy/Codes/Cloud/essai/netcdf/main.cpp:47*: référence indéfinie vers «*netCDF::NcFile::NcFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, netCDF::NcFile::FileMode)*»
/home/lebarsy/Codes/Cloud/essai/netcdf/main.cpp:47*: référence indéfinie vers «*netCDF::NcFile::~NcFile()*»
collect2: error: ld returned 1 exit status
CMakeFiles/testnetcdf.dir/build.make:94: recipe for target 'bin/Debug/testnetcdf' failed
make[2]: *** [bin/Debug/testnetcdf] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/testnetcdf.dir/all' failed
make[1]: *** [CMakeFiles/testnetcdf.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Donc, clairement, quelque chose ne va pas au moment de l’édition des liens. Cependant, sur le net, je ne parviens pas à trouver d’où viens le problème. De toute évidence, je ne maîtrise pas le module CMake de NetCDF.

Pour information, voici ce qui est installé concernant la bibliothèque NetCDF sur ma machine :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$ aptitude search libnetcdf
i A libnetcdf-c++4                                                                       - legacy NetCDF C++ interface                                                                   
p   libnetcdf-c++4:i386                                                                  - legacy NetCDF C++ interface                                                                   
i A libnetcdf-c++4-1                                                                     - C++ interface for scientific data access to large binary data                                 
p   libnetcdf-c++4-1:i386                                                                - C++ interface for scientific data access to large binary data                                 
i   libnetcdf-c++4-dbg                                                                   - debugging symbols for NetCDF C++                                                              
p   libnetcdf-c++4-dbg:i386                                                              - debugging symbols for NetCDF C++                                                              
i   libnetcdf-c++4-dev                                                                   - creation, access, and sharing of scientific data in C++                                       
p   libnetcdf-c++4-dev:i386                                                              - creation, access, and sharing of scientific data in C++                                       
i   libnetcdf-c++4-doc                                                                   - NetCDF C++ API documentation                                                                  
p   libnetcdf-cxx-legacy-dbg                                                             - debugging symbols for legacy NetCDF C++ interface                                             
p   libnetcdf-cxx-legacy-dbg:i386                                                        - debugging symbols for legacy NetCDF C++ interface                                             
p   libnetcdf-cxx-legacy-dev                                                             - legacy NetCDF C++ interface - development files                                               
p   libnetcdf-cxx-legacy-dev:i386                                                        - legacy NetCDF C++ interface - development files                                               
i   libnetcdf-dev                                                                        - creation, access, and sharing of scientific data                                              
p   libnetcdf-dev:i386                                                                   - creation, access, and sharing of scientific data                                              
i A libnetcdf13                                                                          - Interface for scientific data access to large binary data                                     
p   libnetcdf13:i386                                                                     - Interface for scientific data access to large binary data                                     
i   libnetcdff-dbg                                                                       - debugging symbols for NetCDF Fortran                                                          
p   libnetcdff-dbg:i386                                                                  - debugging symbols for NetCDF Fortran                                                          
i   libnetcdff-dev                                                                       - creation, access, and sharing of scientific data in Fortran                                   
p   libnetcdff-dev:i386                                                                  - creation, access, and sharing of scientific data in Fortran                                   
p   libnetcdff-doc                                                                       - NetCDF Fortran documentation                                                                  
i A libnetcdff6                                                                          - Fortran interface for scientific data access to large binary data                             
p   libnetcdff6:i386                                                                     - Fortran interface for scientific data access to large binary data
Il me semble qu’il y a tout ce qu’il faut.

Donc, quelqu’un a-t-il déjà réussi à utiliser la bibliothèque NetCDF en C++ avec CMake ? Si oui, comment avez-vous fait ?

À bientôt.