bspline-fortran icon indicating copy to clipboard operation
bspline-fortran copied to clipboard

.mod files not found during (cmake) compilation

Open scarpma opened this issue 2 years ago • 4 comments

Hello, I have never used cmake, so I have (I think) a very dumb question.

I'm trying to compile with cmake, so I followed the readme and started the compilation with make. The problem is that after few instant the compilation stops with an error since it cannot find the .mod files (that are inside the lib directory).

Is there something that is not written in the readme that I should do ? It seems that the makefile doesn't know where to look for .mod files.

Thanks in advance !

scarpma avatar Feb 06 '23 11:02 scarpma

It seems I'm having the same problem, I'm getting "Fatal Error: Cannot open module file 'bspline_module.mod' for reading at (1): No such file or directory" (at use bspline_module) when building the bspline_test module. (I'm trying to use bspline-fortran within another CMake-based Fortran project)

bilderbuchi avatar Jan 22 '24 14:01 bilderbuchi

So, it seems that we need to make sure that, in addition to the library, we need to make sure that all targets find the module files in the (non-default) lib folder: https://stackoverflow.com/a/43918277/599884.

In my case, after https://github.com/jacobwilliams/bspline-fortran/blob/85cbfe19d5ab2fd07b4424f2a0baa62045a0fafa/CMakeLists.txt#L122 adding the line

    target_include_directories(${prog_name} PUBLIC $<TARGET_PROPERTY:${LIB_NAME}-static,Fortran_MODULE_DIRECTORY>)

fixes the problem at least for the test executables. I'm not sure if that is not papering over a deeper issue, though. @jacobwilliams, @Gjacquenot, any idea ?

bilderbuchi avatar Jan 23 '24 10:01 bilderbuchi

I'm not a CMake expert so I'm not sure what the proper solution is. If this works, I can merge it in.

See also: #31

jacobwilliams avatar Jan 23 '24 14:01 jacobwilliams

I think this is a better patch, inspired by CMake issues and https://github.com/jacobwilliams/json-fortran/commit/d8dc940bdef5759964fb81aecbf426eb995b8c8b:

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7754d07..20aba1c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -73,6 +73,11 @@ endif()
 add_library( ${LIB_NAME}        SHARED ${SOURCES} )
 add_library( ${LIB_NAME}-static STATIC ${SOURCES} )
 
+# Make sure modules for the static library can be found by depending targets
+# put together from hints in https://gitlab.kitware.com/cmake/cmake/-/issues/24223
+# and https://gitlab.kitware.com/cmake/cmake/-/issues/25425
+target_include_directories(${LIB_NAME}-static PUBLIC $<BUILD_INTERFACE:${MODULE_DIR}>)
+
 set_target_properties(${LIB_NAME}-static
   PROPERTIES
   OUTPUT_NAME "${LIB_NAME}"

bilderbuchi avatar Jan 24 '24 14:01 bilderbuchi