Netcdf体系结构x86_64的未定义符号

时间:2014-02-25 23:30:14

标签: linker netcdf

我在使用netcdf库时遇到了名人

Undefined symbols for architecture x86_64:
  "NcVar::get(double*, long const*) const", referenced from:
  Reader<itk::Image<double, 3u> >::Read() in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我使用自制程序安装了库:

$brew doctor
Your system is ready to brew.
$brew info netcdf
netcdf: stable 4.3.0
http://www.unidata.ucar.edu/software/netcdf
/usr/local/Cellar/netcdf/4.3.0 (53 files, 4.5M) *
Built from source with: --enable-cxx-compat
From: https://github.com/homebrew/homebrew-science/commits/master/netcdf.rb
==> Dependencies Required: hdf5 ✔

该库位于/ usr / local / lib(它应该在哪里)。我假设这个路径在库中包含路径。

$ls /usr/local/lib/|grep netcdf
libnetcdf.7.dylib
libnetcdf.a
libnetcdf.dylib
libnetcdf_c++.4.dylib
libnetcdf_c++.a
libnetcdf_c++.dylib
libnetcdf_c++4.1.dylib
libnetcdf_c++4.a
libnetcdf_c++4.dylib

我的代码用于链接,直到它没有。

#include <iostream>
#include <exception>

#include <netcdfcpp.h>

std::string _filename="blabla.nc", _varname="variableName";
int _dim;
long* _sizeArray;
long _sizeFlat;
double* _data;

// Read netCDF
void Read()
{
        // Open the file.
        NcFile file( _filename.c_str(), NcFile::ReadOnly, NULL, 0, NcFile::Offset64Bits);

        NcVar* var = file.get_var( _varname.c_str() );
        if( var->is_valid() )
        {
            _sizeFlat = var->num_vals();
            _sizeArray = var->edges();

            /*
             _dim = var->num_dims();

             std::cout<< _dim<<std::endl;
             for (int i=0; i<_dim; i++)
                 std::cout<<_sizeArray[i]<<"\t";
             std::cout<<"\t"<<_sizeFlat<<std::endl;
             // */

            _data = new PixelType[_sizeFlat];

            try
            {
                var->get(_data, _sizeArray);
            }
            catch(std::exception& a)
            {
                a.what();

                //free mem
                delete[] _data;
                file.close();
                _sizeArray = NULL;
                var = NULL;

                //quit
                std::cout<<"Could not read variable :"<<_varname<<std::endl;
                exit(EXIT_FAILURE);
            }

            //free mem
            file.close();

        }
        else
        {
            //free mem
            file.close();
            var = NULL;

            //quit
            std::cout<<"Could not find variable :"<<_varname<<std::endl;
            exit(EXIT_FAILURE);
        }

}

 int main(void)
{
   Read();
   return 0;
}

我知道我使用旧的遗留代码(http://www.unidata.ucar.edu/software/netcdf/examples/programs/),但我使用的方法是我在打开/usr/local/include/netcdfcpp.h时找到的方法

我正在使用Cmake进行编译,这里是CMakeLists.txt,如果它可以帮助

cmake_minimum_required ( VERSION 2.6 )

#set project name
set( Version_Major 3 )
set( Version_Minor 0 )
set( ProjectName supercellDetection_v${Version_Major}.${Version_Minor} )
project ( ${ProjectName} )

if( COMMAND CMAKE_POLICY )
  cmake_policy( SET CMP0012 NEW )
  cmake_policy( SET CMP0009 NEW )
endif()

#set output path
set( EXECUTABLE_OUTPUT_PATH /absolute/path )

#generate source files list
file(
GLOB_RECURSE
source_files
src/*pp
)
#create output executable
add_executable( ${ProjectName} ${source_files} )

#look for itk
find_package( ITK REQUIRED )
include( ${ITK_USE_FILE} )
include_directories( ${ITK_INCLUDE_DIRS} )

#try for vtk
if ( ITKVtkGlue_LOADED )
find_package( VTK REQUIRED )
include( ${VTK_USE_FILE} )
endif()

#linker settings
#target_link_libraries( ${ProjectName} ITKCommon ITKIO ITKBasicFilters ITKReview )
target_link_libraries( ${ProjectName} ${ITK_LIBRARIES} )

# compilation options
add_definitions( "-Wall -std=c++11 -g" )

################################################################


#Generate list of header files
include_directories( "/opt/X11/include/" )
include_directories( "/usr/local/include/" )

#add libraries
file(
GLOB_RECURSE
x11Lib_files
/opt/X11/lib/*.dylib
)

target_link_libraries( ${ProjectName} ${x11Lib_files} )

我不知道如何解决这个链接器问题。你能帮我吗?

0 个答案:

没有答案