geospace-code / h5fortran

Lightweight HDF5 polymorphic Fortran: h5write() h5read()
https://geospace-code.github.io/h5fortran
BSD 3-Clause "New" or "Revised" License
99 stars 24 forks source link

LNK2019: unresolved external symbol, H5FORTRAN_mp_LT1WRITE #6

Closed nyckmaia closed 4 years ago

nyckmaia commented 4 years ago

I wrote a simple Fortran static library using thi single *.f90 file:

module mod

    use h5fortran
    use ISO_C_BINDING

    contains
    subroutine xxx() bind(C, name = 'f_x')
        call h5write('golt.h5','/x', [1,2,3,4,5,6])
    end subroutine xxx
end module mod

After that, I call the f_x subroutine in my C++ application:

extern "C" {
    void f_x(void);
}

int main()
{
    f_x();

    return 0;
}

Library Directory: C:\HDF5\1.12.0\lib Include Directory: C:\HDF5\1.12.0\include

Linked HDF5 libraries:

Of course, I linked my own generated Fortran static library Lib1.lib and a single Fortran library dependence called ifmodintr.lib

But I got this compile time error below:

LNK2019: unresolved external symbol, H5FORTRAN_mp_LT1WRITE

Could you help me?

How can I fix it?

My system:

nyckmaia commented 4 years ago

Sorry about that... I forgot to link the h5fortran.lib...

Now it works great!

scivision commented 4 years ago

I think Intel Fortran on Windows is the trickiest linking situation for HDF5, but one that does work as you see. I also regularly using HDF5 + h5fortran with Intel compiler or GCC on Windows.

In general, the easiest way to use h5fortran cross platform is to use CMake. Then if your program above is named "main.f90" the CMakeLists.txt for your project is like

cmake_minimum_required(VERSION 3.14)
project(MyTest Fortran)

include(FetchContent)

FetchContent_Declare(h5fortran_proj
  GIT_REPOSITORY https://github.com/geospace-code/h5fortran.git
  GIT_TAG v2.11.1
)

FetchContent_MakeAvailable(h5fortran_proj)

# ------------------------------------------------------
add_executable(my_program main.f90)
target_link_libraries(my_program h5fortran::h5fortran)

this would be built like:

cmake -B build
cmake --build build

which creates executable "build/my_program.exe"

scivision commented 4 years ago

This question has come up a few times, I think I will make a simple standalone example for this under Examples/

scivision commented 4 years ago

https://github.com/geospace-code/h5fortran/tree/master/Examples