我在使用库的fortran上编写程序。
为了进行编译,我使用了简单的shell脚本,一切都很好:
gfortran -c mod_input.f90 mod_cpp.f90 mod_data.f90 main.f90 b_coils.f
gfortran -o主mod_input.o mod_cpp.o mod_data.o main.o b_coils.o libspline.a -lstdc ++
(libspline.a-用C ++编写的库)
现在,我想使用makefile代替shell脚本,但是不知道如何正确编写它。
生成文件:
target = main
srcdir = src/
objdir = obj/
targetdir = run/
FORT = gfortran
mflag = module
FORTFLAGS =
LDFLAGS = -lstdc++
MKLPATH =/home/gatto/Desktop/ray_u3/src
LIBDIR =-L$(MKLPATH)
LIBS = -lspline
MODDIR = obj/
# Ray sources
sources = mod_input.f90 mod_cpp.f90 mod_data.f90 main.f90 b_coils.f
# objects
objects = $(patsubst %.for,%.o,$(filter %for,$(sources)) )
objects += $(patsubst %.f90,%.o,$(filter %f90,$(sources)) )
# main target
build:$(targetdir)$(target)
# build rules
$(targetdir)$(target):$(addprefix $(objdir),$(objects))
@echo === build $@ ===
$(FORT) $(FORTFLAGS) $(LDFLAGS) $(LIBDIR) -o $@ $(LIBS) $^
#compilation target
compile :$(addprefix $(objdir),$(objects))
# compilation rules
$(objdir)%.o : $(srcdir)%.f90
@echo === compile $< ===
$(FORT) -c $(objdir) $(FORTFLAGS) -o $@ $<
$(objdir)%.o : $(srcdir)%.f
@echo === compile $< ===
$(FORT) -c $(objdir) $(FORTFLAGS) -o $@ $<
# cleaning rules
cleanobj:
-rm $(objdir)*.o $(objdir)*.mod
cleantarget:
-rm $(targetdir)$(target)
cleanall:cleanobj cleantarget
.PHONY: compile build cleanall cleanobj cleantarget
输出:
gfortran -lstdc++ -L/home/gatto/Desktop/ray_u3/src -o run/main -lspline obj/mod_input.o obj/mod_cpp.o obj/mod_data.o obj/main.o
obj/mod_data.o: In function `__global_data_MOD_modbfield':
mod_data.f90:(.text+0x3e2): undefined reference to `gbcoil_'
obj/mod_data.o: In function `__global_data_MOD_moddensity':
mod_data.f90:(.text+0x5d4): undefined reference to `GetSplineValue'
mod_data.f90:(.text+0x64f): undefined reference to `GetSplineValue'
mod_data.f90:(.text+0x6ca): undefined reference to `GetSplineValue'
mod_data.f90:(.text+0x745): undefined reference to `GetSplineValue'
obj/mod_data.o: In function `__global_data_MOD_inpfile':
mod_data.f90:(.text+0xa66): undefined reference to `Approx_input'
mod_data.f90:(.text+0xa75): undefined reference to `SetSpline'
collect2: error: ld returned 1 exit status
make: *** [run/main] Error 1
最佳答案
我建议您使用automake
(和autoconf
)。它确实有大量的metatag
,但是对于简单的程序而言,它的编写速度更快,并且在大多数情况下,您可能会发现自己所做的改动很小(例如filename
)。
这是我的autoconf
和automake
用于类似项目的输入,该项目仅使用lapack
而不是mkl
。
configure.ac
#AC_PREREQ(2.69)
AC_INIT([src], [0.1],
[],
[mftc],
[])
AC_CONFIG_AUX_DIR([build-aux])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_MACRO_DIR([m4])
AM_INIT_AUTOMAKE([1.9.6 dist-bzip2 subdir-objects foreign])
AC_SEARCH_LIBS([dsyev_], [lapack], ,AC_MSG_ERROR([No LAPACK library]))
AC_OPENMP
FCFLAGS="$OPENMP_FFLAGS "
AC_PROG_FC([gfortran, ifort])
AC_CHECK_PROG([DEPF90_CHECK],[makedepf90],[yes],[no])
AM_CONDITIONAL([FOUND_MAKEDEPF90], [test "x$DEPF90_CHECK" = xyes])
AC_CONFIG_FILES([
Makefile
])
AC_OUTPUT
Makefile.am
bin_PROGRAMS = mftc
mftc_SOURCES = src/mftc.f90 src/parse.f90 src/sort.f90
depend depend.mk:
makedepf90 $(mftc_SOURCES) >depend.mk
@am__include@ @[email protected]@am__quote@
mftc_LDADD =
EXTRA_DIST=depend.mk autogen.sh Makefile.am
CLEANFILES =*.mod *.log
depend depend.mk
取决于makedepf90
程序,该程序在大多数linux平台上都可用。该程序负责相关性(即,在程序之前先编译模块)。用法
如果您对LAPACK没问题,那么configure.ac中没有任何更改。将_SOURCES行更改为正确的文件名。
现在,它在终端的3个命令:
$autoconf
$./configure
$make
当然,您还需要安装automake和autoconf。
希望这可以帮助