netCDF - undefined reference

This is a discussion on netCDF - undefined reference within the C++ Programming forums, part of the General Programming Boards category; First let me start off by saying that it has been many years since I've worked with C++. I used ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    netCDF - undefined reference

    First let me start off by saying that it has been many years since I've worked with C++. I used to be a comp sci major but bailed on it for mechanical engineering. I've written a program in R to extract data from a netCDF file as well as do other things with said data. The program as a whole works fine, but it is so slow. I want to re-write it in C++. I downloaded the latest stable release of the netCDF distribution (4.0.1). I pointed my compiler (Bloodshed Dev C++ 4.9.9.2) to the necessary libraries. I tried compiling one of their example C++ files
    (http://www.unidata.ucar.edu/software...mple_xy_wr.cpp)
    however, the compiler gave the following error msgs:

    [Linker error] undefined reference to `netCDF::NcFile::NcFile(std::string, netCDF::NcFile::FileMode)'
    [Linker error] undefined reference to `netCDF::NcFile::addDim(std::string, long)'
    [Linker error] undefined reference to `netCDF::NcFile::addDim(std::string, long)'

    etc..

    What could be causing this error and how can it be solved?
    I'm running Win XP.

    Thank you in advance for any help that can be offered.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You're either missing object/library files in the linker directives, or you haven't compiled all of the .cpp files. Did you compile following the install instructions?

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by Sebastiani View Post
    You're either missing object/library files in the linker directives, or you haven't compiled all of the .cpp files. Did you compile following the install instructions?
    There was some installation instructions for building a dll. But the function requires headers netcdfcpp.h, ncvalues.h and netcdf.h. I've used the ncdump function executable which requires the dll file, but I don't see how the compiler would need it. And if it did, I don't know where to put it.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You can probably just use the pre-built DLL, anyway. Just navigate to the compiler's project settings and add the DLL to the the linker options (you don't have to move the DLL, just find out where it is and give the full path to the linker). For most versions of Dev-C++, the steps would be Project->Project Options->Parameters and under the Linker field click Add Object or Library.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    7

    Unhappy

    Quote Originally Posted by Sebastiani View Post
    You can probably just use the pre-built DLL, anyway. Just navigate to the compiler's project settings and add the DLL to the the linker options (you don't have to move the DLL, just find out where it is and give the full path to the linker). For most versions of Dev-C++, the steps would be Project->Project Options->Parameters and under the Linker field click Add Object or Library.
    I added the dll file under the linker field. However, I'm still getting the undefined reference error.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Show your complete log -- you may need to give a path, unless you put the .dll somewhere special.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by tabstop View Post
    Show your complete log -- you may need to give a path, unless you put the .dll somewhere special.
    Here it is.

    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\Dev-Cpp\netcdf-4.0\examples\CXX\simple_xy_wr.cpp" -o "C:\Dev-Cpp\netcdf-4.0\examples\CXX\simple_xy_wr.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x217):simple_xy_wr.cpp: undefined reference to `NcFile::NcFile(char const*, NcFile::FileMode, unsigned int*, unsigned int, NcFile::FileFormat)'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x22f):simple_xy_wr.cpp: undefined reference to `NcFile::is_valid() const'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x25f):simple_xy_wr.cpp: undefined reference to `NcFile::~NcFile()'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x296):simple_xy_wr.cpp: undefined reference to `NcFile::add_dim(char const*, long)'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x2ba):simple_xy_wr.cpp: undefined reference to `NcFile::add_dim(char const*, long)'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x30a):simple_xy_wr.cpp: undefined reference to `NcFile::add_var(char const*, NcType, NcDim const*, NcDim const*, NcDim const*, NcDim const*, NcDim const*)'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x350):simple_xy_wr.cpp: undefined reference to `NcVar:ut(int const*, long, long, long, long, long)'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x38c):simple_xy_wr.cpp: undefined reference to `NcFile::~NcFile()'
    C:\DOCUME~1\Owner\LOCALS~1\Temp/cc67baaa.o(.text+0x3cb):simple_xy_wr.cpp: undefined reference to `NcFile::~NcFile()'
    collect2: ld returned 1 exit status

    Execution terminated

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    One more thing: I copied the netcdfcpp.h, ncvalues.h and netcdf.h files into C:\Dev-Cpp\include so the compiler would definitely see them.

    Here's the log that's generated when I point the linker to the netcdf.lib file that came with the pre-built dll. I've also pointed the linker to the dll file itself even though the dialog box was looking for .lib and .obj files. The log is virtually identical.

    Compiler: Default compiler
    Building Makefile: "C:\Dev-Cpp\Makefile.win"
    Executing make...
    make.exe -f "C:\Dev-Cpp\Makefile.win" all
    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"

    g++.exe main.o -o "Project1.exe" -L"C:/Dev-Cpp/lib" netcdf-3.6.1-beta1/netcdf.lib

    main.o(.text+0x217):main.cpp: undefined reference to `NcFile::NcFile(char const*, NcFile::FileMode, unsigned int*, unsigned int, NcFile::FileFormat)'
    main.o(.text+0x22f):main.cpp: undefined reference to `NcFile::is_valid() const'
    main.o(.text+0x25f):main.cpp: undefined reference to `NcFile::~NcFile()'
    main.o(.text+0x296):main.cpp: undefined reference to `NcFile::add_dim(char const*, long)'
    main.o(.text+0x2ba):main.cpp: undefined reference to `NcFile::add_dim(char const*, long)'
    main.o(.text+0x30a):main.cpp: undefined reference to `NcFile::add_var(char const*, NcType, NcDim const*, NcDim const*, NcDim const*, NcDim const*, NcDim const*)'
    main.o(.text+0x350):main.cpp: undefined reference to `NcVar:ut(int const*, long, long, long, long, long)'
    main.o(.text+0x38c):main.cpp: undefined reference to `NcFile::~NcFile()'
    main.o(.text+0x3cb):main.cpp: undefined reference to `NcFile::~NcFile()'
    collect2: ld returned 1 exit status

    make.exe: *** [Project1.exe] Error 1

    Execution terminated

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Is your library at c:\Dev-Cpp\lib\something.dll? 'Cause that's where you've got it looking. If it's not, you need to add the correct path to the library path list (the part with -L).

  10. #10
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by tabstop View Post
    Is your library at c:\Dev-Cpp\lib\something.dll? 'Cause that's where you've got it looking. If it's not, you need to add the correct path to the library path list (the part with -L).
    All the files are in folders visible to the compiler.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CshNJ View Post
    All the files are in folders visible to the compiler.
    Then problem solved! Well done.

    Edit: And please tell me you didn't get the prebuilt dlls that say:
    (Note: we don't have a C++ interface here).
    Last edited by tabstop; 09-05-2009 at 06:52 PM.

  12. #12
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> netcdf-3.6.1-beta1/netcdf.lib

    Just reiterating what tabstop said, that's not a full path so the compiler is going to look in the DEV-C++ lib directory (and I'm pretty sure it won't find it there).

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    Quote Originally Posted by tabstop View Post
    Then problem solved! Well done.

    Edit: And please tell me you didn't get the prebuilt dlls that say:

    You know, I did see that when I was looking at the documentation last night. I probably convinced myself that somehow didn't apply to what I was doing. Guess this means that I need to build the dll myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21