所以我有一个小的Fortran库,为HDF5 Fortran调用提供了一些包装器。我还有一个小测试,调用库中定义的接口。我知道问题在于我连接HDF5的方式。我已经检查了我的HDF5版本(特别是1.8.15-patch1),确保它已正确安装到我的机器上,并且我的机器上没有备用版本。我正在构建两个项目,编译器设置为gcc-5,g ++ - 5和gfortran-5。
我正在使用CMake 3.2.2来生成makefile。
项目的布局是这样的:
CMakeLists.txt
test
|_____ CMakeLists.txt
|_____ dump_test.f90
src
|_____ data
|_____ CMakeLists.txt
|_____ dump_data.f90
|_____ <other libraries>
在主CMakeLists.txt中,我有
project(testlibs)
ENABLE_LANGUAGE(Fortran)
SET(HDF5_USE_STATIC_LIBRARIES ON)
find_package(HDF5 COMPONENTS C CXX Fortran REQUIRED)
SET(CMAKE_Fortran_FLAGS_DEBUG "-g -O0 -DDEBUG")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/src/data)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/tests)
在数据库CMakeLists.txt中,我有
include_directories(${HDF5_INCLUDE_DIRS})
add_library(dumpData STATIC ${CMAKE_CURRENT_LIST_DIR}/dump_data.f90)
target_link_libraries(dumpData hdf5 hdf5_fortran )
install(TARGETS dumpData DESTINATION ${CMAKE_INSTALL_RPATH})
在测试CMakeLists.txt中,我有
include_directories(${HDF5_INCLUDE_DIRS})
include_directories("${PROJECT_BINARY_DIR}/src/data")
add_executable(dumptest EXCLUDE_FROM_ALL dump_test.f90)
target_link_libraries(dumptest dumpData)
我已经看到了将图书馆与dl联系起来的建议,但这似乎并没有影响任何事情。
构建库时没有错误但是当我构建示例时,我得到了一堆未定义的引用
//usr/local/lib/libhdf5_fortran.a(H5_ff.f90.o): In function `__h5lib_MOD_h5dont_atexit_f':
H5_ff.f90:(.text+0xed): undefined reference to `h5dont_atexit_c_'
//usr/local/lib/libhdf5_fortran.a(H5_ff.f90.o): In function `__h5lib_MOD_h5garbage_collect_f':
H5_ff.f90:(.text+0x109): undefined reference to `h5garbage_collect_c_'
//usr/local/lib/libhdf5_fortran.a(H5_ff.f90.o): In function `__h5lib_MOD_h5check_version_f':
H5_ff.f90:(.text+0x143): undefined reference to `h5check_version_c_'
//usr/local/lib/libhdf5_fortran.a(H5_ff.f90.o): In function `__h5lib_MOD_h5get_libversion_f':
H5_ff.f90:(.text+0x17d): undefined reference to `h5get_libversion_c_'
//usr/local/lib/libhdf5_fortran.a(H5_ff.f90.o): In function `__h5lib_MOD_h5close_f':
H5_ff.f90:(.text+0x1cc): undefined reference to `h5close_types_c_'
//usr/local/lib/libhdf5_fortran.a(H5_ff.f90.o): In function `__h5lib_MOD_h5open_f':
H5_ff.f90:(.text+0x20a): undefined reference to `h5init_types_c_'
H5_ff.f90:(.text+0x2ba): undefined reference to `h5init_flags_c_'
H5_ff.f90:(.text+0x2d0): undefined reference to `h5init1_flags_c_'
我在CMake文件中遗漏了什么?我发现它是HDF5 Fortran函数无法找到底层C函数的问题。
答案 0 :(得分:0)
我正在做类似的事情,并且有相同的错误消息(请参见下面的小摘录)。我在主文件夹中有一个CMakeLists.txt,它用add_subdirectory( src )
来构建用于多个可执行程序的静态和共享库,并用add_subdirectory( apps )
来构建和链接可执行程序。这是错误消息的示例:
Faults/src/HDMod.f90:90: undefined reference to `__h5global_MOD_h5f_acc_trunc_f'
Faults/src/HDMod.f90:90: undefined reference to `__h5f_MOD_h5fcreate_f'
Faults/src/HDMod.f90:90: undefined reference to `__h5global_MOD_h5f_acc_rdwr_f'
Faults/src/HDMod.f90:90: undefined reference to `__h5f_MOD_h5fopen_f'
../lib/libFaultsStatic.a(HDMod.f90.o): In function `__hdmod_MOD_hd_open':
主要的CMakeLists.txt文件包含以下几行(仅摘录,因为未显示更多行):
cmake_minimum_required (VERSION 3.12 FATAL_ERROR)
project( Faults_Project Fortran C )
set( HDF5_USE_STATIC_LIBRARIES ON )
find_package( MPI REQUIRED )
find_package( HDF5 COMPONENTS C Fortran REQUIRED )
include_directories( SYSTEM ${HDF5_INCLUDE_DIRS} )
set( CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${OpenMP_Fortran_FLAGS}" )
add_subdirectory( src )
add_subdirectory( apps )
src
文件夹的CmakeLists.txt从源创建静态库和共享库:
add_library( FaultsShared SHARED ${Faults_Src_Code} )
set_target_properties( FaultsShared PROPERTIES LINKER_LANGUAGE Fortran )
set_target_properties( FaultsShared PROPERTIES LINK_FLAGS "${MPI_LINK_FLAGS}" )
set_target_properties( FaultsShared PROPERTIES Fortran_FORMAT "FREE" )
install( TARGETS FaultsShared RUNTIME DESTINATION ../bin/ LIBRARY DESTINATION ../lib/ ARCHIVE DESTINATION ../lib/ )
,对于STATIC
库也是如此。然后在apps
文件夹的CMakeLists.txt中:
add_executable( artsRun artsRun.f90 )
target_link_libraries( artsRun FaultsStatic ${HDF5_Fortran_LIBRARIES} )
对于其他可执行程序,依此类推。诀窍是在${HDF5_Fortran_LIBRARIES}
行中包含target_link_libraries
。
我将其发布在这里是因为看起来很多人都看过这个问题,并且3年后没有发布任何答案。希望这可以帮助其他人寻找相同的东西。