NetCDF C++ interface under Visual C++ on Windows - problem and workaround

 
 
Hello, list!
 
As S.G on November 7, I am also having trouble with building a DLL from the C++ 
interface under Windows, since I recently switched to MSVC 8 (the 2005 
edition). However, I did succeed when I was using the VC 6.0 version, and I 
actually wrote down a recipe (below). There is a hack, though, related to the 
off_t __int64 definition, which I think invalidates the large file support.

With the newest MS compiler, I thought I could get the off_t to work properly, 
that is, skip the ugly hack. So far no success, so I think I'll just use my 
former solution unless someone can guide me around the problem.

Which is that netcdf.h states:
#define off_t __int64
 
whereas in the file wchar.h, which appears to be included deep down in some 
dependencies (haven't explored the #include chain) the definition goes:

#ifndef _OFF_T_DEFINED
typedef long _off_t;                /* file offset value */
#if     !__STDC__
/* Non-ANSI name for compatibility */
typedef long off_t;
#endif
#define _OFF_T_DEFINED
#endif

...Which generates the following errors:
 
1>c:\program files\microsoft visual studio 8\vc\include\wchar.h(485) : error 
C2632: 'long' followed by '__int64' is illegal

1>c:\program files\microsoft visual studio 8\vc\include\wchar.h(485) : warning 
C4091: 'typedef ' : ignored on left of 'long' when no variable is declared

...occuring under compilation of netcdf.cpp (ncvalues.cpp compiles OK).

Obviously, the definitions crash. I've tried to switch places of some 
#include's and also tried to #undef off_t, but preprocessor directives are not 
what I know best.

 
Here is what worked in VC 6.0. It also compiles under MSVC 8.0 (2005), but I 
haven't tested the functionality. The oold one worked fine. I have not tried 
MSVC 7.0 (2003).

(Most of the text below was written a year ago, for VC60 and NetCDF 3.6.1.beta3)
 

Being a slave of MS and GUI, I managed to get the netCDF C++ interface to
compile under Windows, from "within" MS Visual Studio 6.0.
(That is, using the GUI and wizards, no makefiles or configure tricks.)
 
I have not compiled the whole C library, but using the pre-built netCDF works 
fine.
 
I do not know if this is the only or even best solution, but it works. I had to 
do 
a modification which I guess disqualifies me from using the >2GiB files, and
I have no idea of what happens if I try reading a larger file.
 

1: Get the 3.6.1 prebuilt binaries from the 
 http://www.unidata.ucar.edu/software/netcdf/ 
<http://www.unidata.ucar.edu/software/netcdf/>  WEB site.
This does not include the C++ interface.
 
2: Copy the .exe, .dll and .lib files to the directory 
where you put your compiled applications. 
 
3: Find, on the same site, the full source distribution
This contains both the netCDF C code (in \libsrc) and the C++ interface code 
(in \cxx).
 
4: Build a new, empty C++ dll project using the Visual Studio wizard. (I call 
this netCDFsh)
 
5: Copy the files 
 
\netcdf-3.6.1\src\cxx\ncvalues.cpp
\netcdf-3.6.1\src\cxx\ncvalues.h
\netcdf-3.6.1\src\cxx\netcdf.cpp
\netcdf-3.6.1\src\cxx\netcdfcpp.h
and 
\netcdf-3.6.1\libsrc\netcdf.h
 
to your \netCDFsh project directory, and include them in your project.
 
6: Be sure to include the definition DLL_NETCDF in the Project Settings, tab 
C/C++, 
Category Preprocessor.
7: Still in Project Settings, tab Link, category Input, add netcdf.lib under 
Object/library modules, and the path where you put it, under Additional Library 
Path.
8: Still in Project Settings, specify your output path to the directory where 
you
keep your binary executables (and where you put the pre-built netcdf files).
 
9: In the netcdf.h, comment out the 
 #define off_t __int64
on (or around) line 212, and add a comment at the file top that you did so.
This is because VC60 doesn't react well to 64 bit integers, and means that you 
will
not be able to use this extended functionality of netCDF.
 
NB! Beware that you now change the code behind the pre-built C netCDF.dll, so 
keep a
backup of the original file in case you want to try compiling this library 
yourself.
 
NB2: I have absolutely no idea of which functions in the C++ interface that 
will not
work without this declaration of off_t.
 

10: In the netcdfcpp.h file, add the declaration 
 __declspec(dllexport)
to the class NcFile declaration, so that it reads:
 class __declspec(dllexport) NcFile
Repeat this for all the other classes in this file. This is because you compile 
the C++ interface 
into a new dll, and need the compiler to export these classes to a .lib file.
 
11: Done. Build the netCDFsh dll, and verify that netCDF.dll and netCDF.lib 
appears
in your executable directory.
 
12: Code your application.
 Link in both netCDF.lib and netDCFsh.lib, 
 #include "netCDFsh\netCDFcpp.h"
and you should be able to construct NcFile objects and extract information from 
the
files just by ordinary class member notation.
 
I guess it is possible to take all the four C++ files directly into your 
application, so 
you can do without the __declspec's and the netCDFsh dll, but I prefer to 
separate 
my own from other people's code.

 
Good luck!

Sjur  :-)
 
 
On Tue, 07 Nov 2006 07:27:41 -0700, Ed Hartnett wrote:
>
>Howdy!
>
>The challenge is, no one has ever ported the C++ API to the windows
>programming environment.
>
>As you noted, I did port the C API to MS Visual Studio, and that
>works.
>
>But I have never had the time or the strong need to put the C++ API
>under Visual Studio. I don't know how hard it would be to get it
>working, perhaps easy, perhaps not, however I am sure that I will not
>be able to try to tackle that problem any time soon.
>
>You could just use the C library from C++ on widows, or you can try to
>get a visual studio solution together for the C++ API, which would
>then produce a C++ library DLL. If you do this, please share your
>results with me, and I can make it available in future distributions.
>
>Thanks!
>
>Ed
>



Sjur Kolberg
SINTEF Energiforskning A/S
Sem Sælands vei 11
7465 Trondheim
tlf. 73 59 72 78
fax  73 59 72 50 


==============================================================================
To unsubscribe netcdfgroup, visit:
http://www.unidata.ucar.edu/mailing-list-delete-form.html
==============================================================================