<div dir="ltr">Dear All,<br><br>I have the below directory structure for Fortran code which has very 
basic dependencies, I am not able to get CMake to recognize the 
dependencies automatically without me explicitly having to specify it using add_dependencies or target_link_libraries:<br>
<br>
src/<br>
<br>
    CMakeLists.txt<br>
<br>
    lib1/CMakeLists.txt<br>
    lib1/lib1.f<br>
<br>
    lib2/CMakeLists.txt<br>
    lib2/lib2.f<br>
<br>
    lib3/CMakeLists.txt<br>
    lib3/lib3.f<br>
<br>
    main/CMakeLists.txt<br>
    main/myMain.f<br>
<br>
<br>
Dependencies are:<br>
myMain uses lib3<br>
lib3   uses lib2<br>
lib2   uses lib1<br>
<br>
<br>
My top directory CMakeLists.txt looks like this:<br>
[sindimo@lnx src]$ cat CMakeLists.txt<br>
CMAKE_MINIMUM_REQUIRED (VERSION 2.8)<br>
PROJECT(Fortran-Example-Dependency-Failure Fortran)<br>
<br>
INCLUDE_DIRECTORIES(/workspace/Fortran-Example-Dependency-Failure/obj/linux_gnu_release_64-bit)<br>
<br>
set (EXECUTABLE_OUTPUT_PATH &quot;/workspace/Fortran-Example-Dependency-Failure/bin/linux_gnu_release_64-bit&quot;)<br>
set (LIBRARY_OUTPUT_PATH &quot;/workspace/Fortran-Example-Dependency-Failure/obj/linux_gnu_release_64-bit&quot;)<br>
set (CMAKE_Fortran_MODULE_DIRECTORY &quot;/workspace/Fortran-Example-Dependency-Failure/obj/linux_gnu_release_64-bit&quot;)<br>
<br>
ADD_SUBDIRECTORY(lib3)<br>
ADD_SUBDIRECTORY(lib2)<br>
ADD_SUBDIRECTORY(lib1)<br>
ADD_SUBDIRECTORY(main)<br>
<br>
<br>
The subdirectory CMakeLists.txt files look like:<br>
[sindimo@lnx src]$ cat lib1/CMakeLists.txt <br>
ADD_LIBRARY(lib1 SHARED  lib1.f)<br>
<br>
[sindimo@lnx src]$ cat lib2/CMakeLists.txt<br>
ADD_LIBRARY(lib2 SHARED  lib2.f)<br>
<br>
[sindimo@lnx src]$ cat lib3/CMakeLists.txt<br>
ADD_LIBRARY(lib3 SHARED  lib3.f)<br>
<br>
[sindimo@lnx src]$ cat main/CMakeLists.txt <br>
ADD_EXECUTABLE(Fortran-Example-Dependency-Failure.exe  myMain.f)<br>
TARGET_LINK_LIBRARIES(Fortran-Example-Dependency-Failure.exe  lib1 lib2 lib3)<br>
<br>
<br>
When I run cmake followed by make I get the below error which apparently
 seems to be due to CMake not figuring out the correct dependencies in 
Fortran and doing the wrong build sequence:<br>
[sindimo@lnx build_linux_gnu_release_64-bit]$ cmake ../src<br>
-- The CXX compiler identification is GNU<br>
-- The Fortran compiler identification is GNU<br>
-- Check for working CXX compiler: /usr/bin/c++<br>
-- Check for working CXX compiler: /usr/bin/c++ -- works<br>
-- Detecting CXX compiler ABI info<br>
-- Detecting CXX compiler ABI info - done<br>
-- Check for working Fortran compiler: /usr/bin/gfortran<br>
-- Check for working Fortran compiler: /usr/bin/gfortran  -- works<br>
-- Detecting Fortran compiler ABI info<br>
-- Detecting Fortran compiler ABI info - done<br>
-- Checking whether /usr/bin/gfortran supports Fortran 90<br>
-- Checking whether /usr/bin/gfortran supports Fortran 90 -- yes<br>
-- Configuring done<br>
-- Generating done<br>
-- Build files have been written to: /workspace/Fortran-Example-Dependency-Failure/build_linux_gnu_release_64-bit<br>
<br>
[sindimo@lnx build_linux_gnu_release_64-bit]$ make<br>
Scanning dependencies of target lib3<br>
[ 25%] Building Fortran object lib3/CMakeFiles/lib3.dir/lib3.f.o<br>
 In file /workspace/Fortran-Example-Dependency-Failure/src/lib3/lib3.f:6<br>
<br>
            use m_lib2                                                  <br>
                                                                       1<br>
Fatal Error: Can&#39;t open module file &#39;m_lib2.mod&#39; for reading at (1): No such file or directory<br>
make[2]: *** [lib3/CMakeFiles/lib3.dir/lib3.f.o] Error 1<br>
make[1]: *** [lib3/CMakeFiles/lib3.dir/all] Error 2<br>
make: *** [all] Error 2<br>
<br>
<br>
To my understanding CMake should be able to handle such simple Fortran 
dependencies (as I recall, it uses similar intelligence to that of 
makedepf90 to detect dependencies), am I doing something wrong here?<br><br>
If I add the below explicit dependency statements manually to the top level CMakeLists.txt file, things go well:<br>
ADD_DEPENDENCIES(lib2 lib1)<br>
ADD_DEPENDENCIES(lib3 lib2)<br>
<br>
Also if I sort the ADD_SUBDIRECTORY commands in the correct build sequence, that obviously solves the problem as well.<br>
<br>
However our actual application that we need to use CMake with consists 
of hundreds of libraries, so adding these dependencies manually or 
sorting them in the correct build sequence is not feasible.<br>
<br>
I would appreciate any feed back or help with this.<br>
<br>
Many thanks!<br><br>M. Sindi<br>
</div>