新手请教:vim中调用gfortran编译fortran程序的问题

Vim、Emacs配置和使用
回复
gm2wie
帖子: 2
注册时间: 2011-10-23 10:00

新手请教:vim中调用gfortran编译fortran程序的问题

#1

帖子 gm2wie » 2011-10-23 11:23

这是我的_vimrc文件中关于编译调试运行的部分
在编译fortran文件(*.f和*.for)时,总是不能生成对应的exe文件。
哪个地方写错了?请不吝赐教

代码: 全选

"定义CompileRun函数,用来调用进行编译和运行
func CompileRun()
exec "w"
"C程序
if &filetype == 'c'
exec "!gcc -Wall -enable-auto-import % -g -o %<.exe"
"c++程序
elseif &filetype == 'cpp'
exec "!g++ -Wall -enable-auto-import  % -g -o %<.exe"
"Fortran程序
elseif &filetype == 'f' || &filetype == 'for' || &filetype == 'f90'
exec "!gfortran % -g -o %<.exe"
endif
endfunc
"结束定义CompileRun
"定义Run函数
func Run()
if &filetype == 'c' || &filetype == 'cpp'
exec "!%<.exe"
elseif &filetype == 'f' || &filetype == 'for' || &filetype == 'f90'
exec "!%<.exe"
endif
endfunc
"定义Debug函数,用来调试程序
func Debug()
exec "w"
"C程序
if &filetype == 'c'
exec "!gcc % -g -o %<.exe"
exec "!gdb %<.exe"
elseif &filetype == 'cpp'
exec "!g++ % -g -o %<.exe"
exec "!gdb %<.exe"
"Fortran程序
elseif &filetype == 'f' || &filetype == 'for' || &filetype == 'f90'
exec "!gcc % -g -o %<.exe"
exec "!gdb %<.exe"
endif
endfunc
"结束定义Debug
"设置程序的运行和调试的快捷键F5和Ctrl-F5
map <F5> :call CompileRun()<CR>
map <F6> :call Run()<CR>
map <C-F5> :call Debug()<CR>
gm2wie
帖子: 2
注册时间: 2011-10-23 10:00

Re: 新手请教:vim中调用gfortran编译fortran程序的问题

#2

帖子 gm2wie » 2011-10-23 12:17

编译函数CompileRun中的文件类型应该为:

代码: 全选

elseif &filetype == 'fortran'
头像
Fermat618
帖子: 728
注册时间: 2008-12-28 16:01

Re: 新手请教:vim中调用gfortran编译fortran程序的问题

#3

帖子 Fermat618 » 2011-10-23 19:33

跟我的这个好像啊。可能我们在一个地方抄的. :)
[vim]
func! CompileGcc()
exec "w"
let compilecmd="gcc "
let compileflag="-o %< "
if search("mpi\.h") != 0
let compilecmd = "!mpicc "
endif
if search("glut\.h") != 0
let compileflag .= " -lglut -lGLU -lGL "
endif
if search("cv\.h") != 0
let compileflag .= " -lcv -lhighgui -lcvaux "
endif
if search("omp\.h") != 0
let compileflag .= " -fopenmp "
endif
if search("math\.h") != 0
let compileflag .= " -lm "
endif
compiler gcc
let s:oldmp = &makeprg
let &makeprg = compilecmd." % ".compileflag
make
let &makeprg = s:oldmp
" exec compilecmd." % ".compileflag
endfunc
func! CompileGpp()
exec "w"
let compilecmd="g++ "
let compileflag="-o %< "
if search("mpi\.h") != 0
let compilecmd = "!mpic++ "
endif
if search("glut\.h") != 0
let compileflag .= " -lglut -lGLU -lGL "
endif
if search("cv\.h") != 0
let compileflag .= " -lcv -lhighgui -lcvaux "
endif
if search("omp\.h") != 0
let compileflag .= " -fopenmp "
endif
if search("math\.h") != 0
let compileflag .= " -lm "
endif
compiler gcc
let s:oldmp = &makeprg
let &makeprg = compilecmd." % ".compileflag
make
let &makeprg = s:oldmp
" exec compilecmd." % ".compileflag
endfunc

func! RunPython()
exec "!python %"
endfunc
func! CompileJava()
exec "!javac %"
endfunc


func! CompileCode()
exec "w"
if &filetype == "cpp"
exec "call CompileGpp()"
elseif &filetype == "c"
exec "call CompileGcc()"
elseif &filetype == "python"
exec "call RunPython()"
elseif &filetype == "java"
exec "call CompileJava()"
endif
endfunc

func! RunResult()
exec "w"
if search("mpi\.h") != 0
exec "!mpirun -np 4 ./%<"
elseif &filetype == "cpp"
exec "! ./%<"
elseif &filetype == "c"
exec "! ./%<"
elseif &filetype == "python"
exec "call RunPython"
elseif &filetype == "java"
exec "!java %<"
endif
endfunc

noremap <F7> :call CompileCode()<CR>
noremap <Leader>k :call CompileCode()<CR>
inoremap <F7> <ESC>:call CompileCode()<CR>
vnoremap <F7> <ESC>:call CompileCode()<CR>

noremap <F5> :call RunResult()<CR>
noremap <Leader>r :call RunResult()<CR>
[/vim]
爱因斯坦会弹钢琴
爱因斯坦会拉小提琴
爱因斯坦会骑自行车
回复