我有一个很大的Fortran程序,其中包含许多目录。每个目录都在伪库中单独编译,但是仍然存在相互依赖的混乱,因此最后,所有伪库都组合在一个可用的库中。我想使用Fortran模块,但是它非常脆弱,因为我不能依赖自动依赖项检查,并且编译可能会因顺序而失败。
例如,考虑以下CMakeLists.txt
文件:
project (test Fortran)
add_library (lib1 dir1/lib1.f90)
add_library (lib2 dir2/lib2.f90 dir2/mod.f90)
add_executable (exe dir3/exe.f90)
target_link_libraries (exe lib1 lib2)
Run Code Online (Sandbox Code Playgroud)
资料来源:
dir1/lib1.f90
:
subroutine bar
use foo, only: foofoo
implicit none
write(6,*) foofoo
end subroutine bar
Run Code Online (Sandbox Code Playgroud)
dir2/lib2.f90
:
subroutine bar2
use foo, only: foofoo
implicit none
write(6,*) foofoo,' again'
end subroutine bar2
Run Code Online (Sandbox Code Playgroud)
dir2/mod.f90
:
module foo
implicit none
integer :: foofoo=3
end module foo
Run Code Online (Sandbox Code Playgroud)
dir3/exe.f90
:
program meh
implicit none
call bar()
call bar2()
end program meh
Run Code Online (Sandbox Code Playgroud)
从头开始编译失败:
$ make
[ 25%] Building Fortran object CMakeFiles/lib1.dir/dir1/lib1.f90.o
/home/user/cmake/dir1/lib1.f90:2.4:
use foo, only: foofoo
1
Fatal Error: Can't open module file 'foo.mod' for reading at (1): No such file or directory
make[2]: *** [CMakeFiles/lib1.dir/dir1/lib1.f90.o] Error 1
make[1]: *** [CMakeFiles/lib1.dir/all] Error 2
make: *** [all] Error 2
Run Code Online (Sandbox Code Playgroud)
但按正确的顺序进行操作可以:
$ make lib2
Scanning dependencies of target lib2
[ 50%] Building Fortran object CMakeFiles/lib2.dir/dir2/mod.f90.o
[100%] Building Fortran object CMakeFiles/lib2.dir/dir2/lib2.f90.o
Linking Fortran static library liblib2.a
[100%] Built target lib2
$ make
[ 25%] Building Fortran object CMakeFiles/lib1.dir/dir1/lib1.f90.o
Linking Fortran static library liblib1.a
[ 25%] Built target lib1
[ 75%] Built target lib2
Scanning dependencies of target exe
[100%] Building Fortran object CMakeFiles/exe.dir/dir3/exe.f90.o
Linking Fortran executable exe
[100%] Built target exe
Run Code Online (Sandbox Code Playgroud)
CMake有什么办法可以弄清楚依赖关系并lib2
在mod.f90
之前(至少)进行编译lib1
?
ETA:一个健壮的解决方案应该起作用,而不管文件中定义的顺序lib1
和顺序如何,并且一旦程序被编译,就在运行之后。lib2
CMakeLists.txt
rm foo.mod ; touch ../dir1/lib1.f90
即将推出的 ninja 构建系统版本 (1.10.0)支持动态依赖关系,这将正确解析模块编译顺序。
要将其与 CMake 一起使用,您必须指定Ninja
生成器:
cmake .. -DCMAKE_GENERATOR=Ninja # generate project
ninja # build the project
Run Code Online (Sandbox Code Playgroud)