program example
implicit none
real(8) :: array(20000000)
real(8) :: r
integer :: i
do i = 1, size(array)
call random_number(r)
array(i) = r
end do
print *, sum(array)
end program
import subprocessimport sys# for simplicity we do not check number of# arguments and whether the file really existsfile_path = sys.argv[-1]try: output = subprocess.check_output(['size', file_path]).decode('utf-8')exceptFileNotFoundError:print('command "size" is not available on this platform') sys.exit(0)size =0.0for line in output.split('\n'):if file_path in line:# we are interested in the 4th number on this line size =int(line.split()[3])print('{0:.3f} MB'.format(size/1.0e6))
import sys# for simplicity we do not verify the number and# type of argumentsfile_path = sys.argv[-1]try:withopen(file_path, 'r')as f:print(f.read())exceptFileNotFoundError:print('ERROR: file {0} not found'.format(file_path))
具体实施
来看看CMakeLists.txt:
首先声明一个Fortran项目:
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(recipe-05 LANGUAGES Fortran)
$ mkdir -p build
$ cd build
$ cmake ..
$ cmake --build .
Scanning dependencies of target example
[ 50%] Building Fortran object CMakeFiles/example.dir/example.f90.o
[100%] Linking Fortran executable example
link line:
/usr/bin/f95 -O3 -DNDEBUG -O3 CMakeFiles/example.dir/example.f90.o -o example
static size of executable:
160.003 MB
[100%] Built target example