如果您打算使用 using 的路线autotools
,具体而言autoconf
,以下是我将其全部编码的方式。
创建configure.ac
:
dnl -*- Autoconf -*-
dnl Process this file with autoconf to produce a configure script.
dnl
AC_PREREQ(2.61)
AC_INIT([test], [0.0.1], [me@example.com])
# Define our M4 macro directory
AC_CONFIG_MACRO_DIR([m4])
# Put our generated config header in the source directory
AC_CONFIG_HEADERS([src/config.h])
# Make sure we have a fortran compiler
AC_PROG_FC([ifort xlf pgfortran gfortran])
AC_LANG([Fortran])
AC_FC_FREEFORM
# Check for newunit option to open()
AX_F08_NEWUNIT
AC_OUTPUT
在子目录中创建ax_f08_newunit.m4
宏。m4/
因为这是我指定宏所在的位置configure.ac
:
AC_DEFUN([AX_F08_NEWUNIT], [
AC_REQUIRE([AC_PROG_FC])
AC_LANG_PUSH([Fortran])
AC_MSG_CHECKING([for NEWUNIT support])
AC_COMPILE_IFELSE([
program conftest
integer :: i
open(newunit=i,file='test')
end program conftest],
[ax_f08_newunit=yes], [ax_f08_newunit=no])
AC_LANG_POP([Fortran])
if test "x$ax_f08_newunit" = "xyes"; then
AC_MSG_RESULT([yes])
AC_DEFINE([HAVE_NEWUNIT], [1], [NEWUNIT support])
else
AC_MSG_RESULT([no])
fi
])
然后遵循所有常规autotools
程序:
aclocal -I m4
autoheader
autoconf
然后你可以运行./configure
,这是我的输出:
checking for Fortran compiler default output file name... a.out
checking whether the Fortran compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU Fortran compiler... no
checking whether ifort accepts -g... yes
checking for Fortran flag needed to allow free-form source... -FR
checking for NEWUNIT support... yes
configure: creating ./config.status
config.status: creating src/config.h
最后在你的src/config.h
文件中你应该有:
/* src/config.h. Generated from config.h.in by configure. */
/* src/config.h.in. Generated from configure.ac by autoheader. */
/* NEWUNIT support */
#define HAVE_NEWUNIT 1
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "me@example.com"
/* Define to the full name of this package. */
#define PACKAGE_NAME "test"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "test 0.0.1"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "test"
/* Define to the version of this package. */
#define PACKAGE_VERSION "0.0.1"
当然,您的源代码现在也必须通过预处理器运行。例如src/test.F90
:
program test
#include "config.h"
integer :: i
#ifdef HAVE_NEWUNIT
open(newunit=i,file='data')
#else
i = 7
open(unit=i,file='data')
#endif
end program test
编译测试程序的时候ifort
明白大写的F表示需要对文件进行预处理。