1

I'm trying to use CMake to build a static library consisting of one Fortran source file and a lot of object libraries from subdirectories. Try as I might, I always get an error like this:

[  0%] Building Fortran object modules/map/CMakeFiles/maplib_obj.dir/src/MAP_Fortran_Types.f90.o
cd /home/rcrozier/src/openfast-reos-git/build/modules/map && /usr/bin/gfortran -DHAS_FORTRAN2008_FEATURES -DOPENFAST_DOUBLE_PRECISION -DUSE_DLL_INTERFACE -I/home/rcrozier/src/openfast-reos-git/build/ftnmods  -DCOMPILE_SIMULINK -fpic  -ffree-line-length-none -cpp -fstack-reuse=none -fdefault-real-8 -O3 -DNDEBUG -O3 -J../../ftnmods   -c /home/rcrozier/src/openfast-reos-git/modules/map/src/MAP_Fortran_Types.f90 -o CMakeFiles/maplib_obj.dir/src/MAP_Fortran_Types.f90.o
/home/rcrozier/src/openfast-reos-git/modules/map/src/MAP_Fortran_Types.f90:34:4:

   34 | USE NWTC_Library
      |    1
Fatal Error: Cannot open module file ‘nwtc_library.mod’ for reading at (1): No such file or directory

My project structure is like this:

root_dir
|- CMakeLists.txt
|- nwtc-library
  |- CMakeLists.txt
  |- src
    |- nwtc_source_1.f90
    |- nwtc_source_2.f90
|- version
  |- CMakeLists.txt
  |- src
    |- VersionInfo.f90
|- lots_more_libraries
  |- CMakeLists.txt
  |- src
    |- foo.f90
    |- bar.f90

My versioninfo CMakelists.txt file looks like this:

add_library(versioninfolib src/VersionInfo.f90)

add_library(versioninfolib_obj OBJECT src/VersionInfo.f90)

install(TARGETS versioninfolib
  EXPORT "${CMAKE_PROJECT_NAME}Libraries"
  RUNTIME DESTINATION bin
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib)

I believe the relevant part of the nwtc lib CMakelists.txt is like this:

set(NWTCLIBS_SOURCES
  src/ModMesh.f90
  src/ModMesh_Mapping.f90
  src/ModMesh_Types.f90
  src/NWTC_Base.f90
  <snip, lots more source files>
  src/NetLib/slatec/xermsg.f  )

add_library(nwtclibs ${NWTCLIBS_SOURCES})

add_library(nwtclibs_obj OBJECT ${NWTCLIBS_SOURCES})

install(TARGETS nwtclibs
  EXPORT "${CMAKE_PROJECT_NAME}Libraries"
  LIBRARY DESTINATION lib
  ARCHIVE DESTINATION lib)

The relevant part of the top level CMakeLists.txt looks like this:

set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_SOURCE_DIR}/build/ftnmods)

set(OPENFAST_MODULES
  nwtc-library
  inflowwind
  aerodyn
  aerodyn14
  servodyn
  elastodyn
  beamdyn
  subdyn
  hydrodyn
  orcaflex-interface
  extptfm
  openfoam
  supercontroller
  turbsim
  openfast-library
  version
  feamooring
  moordyn
  icedyn
  icefloe
  map
  wakedynamics
  awae
)

set(OPENFAST_REGISTRY_INCLUDES "" CACHE INTERNAL "Registry includes paths")
set_registry_includes("modules" ${OPENFAST_MODULES})
# Fix non-standard path addition to OPENFAST_REGISTRY_INCLUDES in icefloe module
set(OPENFAST_REGISTRY_INCLUDES
  ${OPENFAST_REGISTRY_INCLUDES} -I ${CMAKE_SOURCE_DIR}/modules/icefloe/src/interfaces/FAST/
  CACHE INTERNAL "Registry includes paths")

foreach(IDIR IN ITEMS ${OPENFAST_MODULES})
  add_subdirectory("${CMAKE_SOURCE_DIR}/modules/${IDIR}")
endforeach(IDIR IN ITEMS ${OPENFAST_MODULES})

add_subdirectory(glue-codes)

add_library(openfastlib STATIC
  ${CMAKE_SOURCE_DIR}/modules/openfast-library/src/FAST_Library.f90
  $<TARGET_OBJECTS:openfast_prelib_obj>
  $<TARGET_OBJECTS:maplib_obj>
  $<TARGET_OBJECTS:nwtclibs_obj>
  $<TARGET_OBJECTS:ifwlib_obj>
  $<TARGET_OBJECTS:aerodynlib_obj>
  $<TARGET_OBJECTS:aerodyn14lib_obj>
  $<TARGET_OBJECTS:servodynlib_obj>
  $<TARGET_OBJECTS:elastodynlib_obj>
  $<TARGET_OBJECTS:beamdynlib_obj>
  $<TARGET_OBJECTS:subdynlib_obj>
  $<TARGET_OBJECTS:hydrodynlib_obj>
  $<TARGET_OBJECTS:orcaflexlib_obj>
  $<TARGET_OBJECTS:extptfm_mckflib_obj>
  $<TARGET_OBJECTS:openfoamtypeslib_obj>
  $<TARGET_OBJECTS:foamfastlib_obj>
  $<TARGET_OBJECTS:scdataextypeslib_obj>
  $<TARGET_OBJECTS:feamlib_obj>
  $<TARGET_OBJECTS:moordynlib_obj>
  $<TARGET_OBJECTS:icedynlib_obj>
  $<TARGET_OBJECTS:icefloelib_obj>
  $<TARGET_OBJECTS:openfast_postlib_obj>
  $<TARGET_OBJECTS:versioninfolib_obj>
)

target_link_libraries(openfastlib 
  versioninfolib_obj
  openfast_prelib_obj
  maplib_obj
  nwtclibs_obj
  ifwlib_obj
  aerodynlib_obj
  aerodyn14lib_obj
  servodynlib_obj
  elastodynlib_obj
  beamdynlib_obj
  subdynlib_obj
  hydrodynlib_obj
  orcaflexlib_obj
  extptfm_mckflib_obj
  openfoamtypeslib_obj
  foamfastlib_obj
  scdataextypeslib_obj
  feamlib_obj
  moordynlib_obj
  icedynlib_obj
  icefloelib_obj
  openfast_postlib_obj
  versioninfolib_obj
  ${LAPACK_LIBRARIES} 
  ${CMAKE_DL_LIBS}
)

I have tried lots of different things, but am absolutely stumped, why am I receiveing the error about missing mod files, and how do do this correctly to avoid the problem?

EDIT

The main CMake file already contains

set(CMAKE_Fortran_MODULE_DIRECTORY ${CMAKE_SOURCE_DIR}/build/ftnmods)

I have also now tried the following:

add_library(openfastlib STATIC
  ${CMAKE_SOURCE_DIR}/modules/openfast-library/src/FAST_Library.f90
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/openfast-library/,$<TARGET_PROPERTY:openfast_prelib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/map/,$<TARGET_PROPERTY:maplib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/nwtc-library/,$<TARGET_PROPERTY:nwtclibs_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/inflowwind/,$<TARGET_PROPERTY:ifwlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/aerodyn/,$<TARGET_PROPERTY:aerodynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/aerodyn14/,$<TARGET_PROPERTY:aerodyn14lib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/servodyn/,$<TARGET_PROPERTY:servodynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/elastodyn/,$<TARGET_PROPERTY:elastodynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/beamdyn/,$<TARGET_PROPERTY:beamdynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/subdyn/,$<TARGET_PROPERTY:subdynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/hydrodyn/,$<TARGET_PROPERTY:hydrodynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/orcaflex-interface/,$<TARGET_PROPERTY:orcaflexlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/extptfm/,$<TARGET_PROPERTY:extptfm_mckflib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/openfoam/,$<TARGET_PROPERTY:openfoamtypeslib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/openfoam/,$<TARGET_PROPERTY:foamfastlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/supercontroller/,$<TARGET_PROPERTY:scdataextypeslib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/feamooring/,$<TARGET_PROPERTY:feamlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/moordyn/,$<TARGET_PROPERTY:moordynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/icedyn/,$<TARGET_PROPERTY:icedynlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/icefloe/,$<TARGET_PROPERTY:icefloelib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/openfast-library/,$<TARGET_PROPERTY:openfast_postlib_obj,SOURCES>>
  $<JOIN:${CMAKE_SOURCE_DIR}/modules/version/,$<TARGET_PROPERTY:versioninfolib_obj,SOURCES>>
)

to see if it would work, but I get the same error, the mod files are not being created.

EDIT2

The verbose make output is shown below:

$ make -j1 VERBOSE=1
/usr/bin/cmake -S/home/rcrozier/src/openfast-reos-git -B/home/rcrozier/src/openfast-reos-git/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/rcrozier/src/openfast-reos-git/build/CMakeFiles /home/rcrozier/src/openfast-reos-git/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/rcrozier/src/openfast-reos-git/build'
make -f modules/map/CMakeFiles/maplib_obj.dir/build.make modules/map/CMakeFiles/maplib_obj.dir/depend
make[2]: Entering directory '/home/rcrozier/src/openfast-reos-git/build'
cd /home/rcrozier/src/openfast-reos-git/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/rcrozier/src/openfast-reos-git /home/rcrozier/src/openfast-reos-git/modules/map /home/rcrozier/src/openfast-reos-git/build /home/rcrozier/src/openfast-reos-git/build/modules/map /home/rcrozier/src/openfast-reos-git/build/modules/map/CMakeFiles/maplib_obj.dir/DependInfo.cmake --color=
Dependee "/home/rcrozier/src/openfast-reos-git/build/modules/map/CMakeFiles/maplib_obj.dir/DependInfo.cmake" is newer than depender "/home/rcrozier/src/openfast-reos-git/build/modules/map/CMakeFiles/maplib_obj.dir/depend.internal".
Dependee "/home/rcrozier/src/openfast-reos-git/build/modules/map/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/rcrozier/src/openfast-reos-git/build/modules/map/CMakeFiles/maplib_obj.dir/depend.internal".
Scanning dependencies of target maplib_obj
make[2]: Leaving directory '/home/rcrozier/src/openfast-reos-git/build'
make -f modules/map/CMakeFiles/maplib_obj.dir/build.make modules/map/CMakeFiles/maplib_obj.dir/build
make[2]: Entering directory '/home/rcrozier/src/openfast-reos-git/build'
[  0%] Building Fortran object modules/map/CMakeFiles/maplib_obj.dir/src/MAP_Fortran_Types.f90.o
cd /home/rcrozier/src/openfast-reos-git/build/modules/map && /usr/bin/gfortran -DHAS_FORTRAN2008_FEATURES -DOPENFAST_DOUBLE_PRECISION -DUSE_DLL_INTERFACE -I/home/rcrozier/src/openfast-reos-git/build/ftnmods  -DCOMPILE_SIMULINK -fpic  -ffree-line-length-none -cpp -fstack-reuse=none -fdefault-real-8 -O3 -DNDEBUG -O3 -J../../ftnmods   -c /home/rcrozier/src/openfast-reos-git/modules/map/src/MAP_Fortran_Types.f90 -o CMakeFiles/maplib_obj.dir/src/MAP_Fortran_Types.f90.o
/home/rcrozier/src/openfast-reos-git/modules/map/src/MAP_Fortran_Types.f90:34:4:

   34 | USE NWTC_Library
      |    1
Fatal Error: Cannot open module file ‘nwtc_library.mod’ for reading at (1): No such file or directory
compilation terminated.
make[2]: *** [modules/map/CMakeFiles/maplib_obj.dir/build.make:349: modules/map/CMakeFiles/maplib_obj.dir/src/MAP_Fortran_Types.f90.o] Error 1
make[2]: Leaving directory '/home/rcrozier/src/openfast-reos-git/build'
make[1]: *** [CMakeFiles/Makefile2:2595: modules/map/CMakeFiles/maplib_obj.dir/all] Error 2
make[1]: Leaving directory '/home/rcrozier/src/openfast-reos-git/build'
make: *** [Makefile:130: all] Error 2
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
crobar
  • 2,810
  • 4
  • 28
  • 46
  • 1
    @veryreverie, I'm not sure if I understood correctly, but I've added the verbose make output when I try to build. And yes I appreciate a MWE would be helpful, and I may need to do that, I'm kind of hoping someone spots an obvious mistake first! – crobar Feb 10 '22 at 11:19
  • @veryreverie, as far as I can tell it is not being created, CMake doesn't seem to recognise the dependancy – crobar Feb 10 '22 at 11:21
  • I get the exact same output after `$ make clean $ rm -r * $ cmake -L .. -DBUILD_SHARED_LIBS=OFF -DBUILD_OPENFAST_SIMULINK_API=ON -DBUILD_OPENFAST_CPP_API=OFF $ make -j1 VERBOSE=1` – crobar Feb 10 '22 at 11:27
  • I think you're right and it's a dependency problem. How are you specifying dependencies? I don't see any `target_link_libraries` or `add_dependencies` or similar. – veryreverie Feb 10 '22 at 11:30
  • I have `target_link_libraries` in the fifth code block down if you scroll to the bottom – crobar Feb 10 '22 at 11:32
  • That's the dependencies for `openfastlib`, but are you specifying the dependencies for `nwtclips` anywhere? – veryreverie Feb 10 '22 at 11:34
  • That library is an object library defined like this: `add_library(nwtclibs_obj OBJECT ${NWTCLIBS_SOURCES})`. Now it is actually another library called `map` which is using the NWTC_Library. The map library is also an object library defined like this `add_library(maplib_obj OBJECT ${MAP_CLIB_SOURCES} ${MAP_F_SOURCES})`. I thought I could not add target_link_libraries to object libraries, but is there another way (I've just seen your link btw, will look.) – crobar Feb 10 '22 at 11:40
  • 1
    You can and should use `target_link_libraries` with object libraries. The dependencies need to be specified all the way up the hierarchy. There may be some way of automating this from the source files, but if so I'm unaware of it. – veryreverie Feb 10 '22 at 11:42
  • @veryreverie, you are right, I just tested, and the build proceeds. Thanks very much for your help and persistence. Feel free to add an answer for me to accept, but i did now find an answer [here](https://stackoverflow.com/questions/31290077/how-to-specify-imported-dependencies-of-an-object-library) – crobar Feb 10 '22 at 11:46
  • glad to be of help :) Your question is a dupe, so I won't answer. Just leave it be and it'll serve as a useful dupe signpost for others with the same problem. – veryreverie Feb 10 '22 at 11:48

0 Answers0