从Vim检测生成的可执行文件
我有一个多文件项目,我使用Cmake构建系统。我已经将从Vim检测生成的可执行文件,vim,cmake,Vim,Cmake,我有一个多文件项目,我使用Cmake构建系统。我已经将:make映射到一个键,因为我需要经常编译它。问题是我需要经常运行生成的可执行文件。但是键入:/变量\u程序\u名称非常乏味 是否有任何方法可以检测/获取生成的可执行文件名?推荐的方法是通过Makefile中的一个单独目标执行此操作,例如:make仅触发生成,而:make run触发可执行文件的(生成和)运行。毕竟,Makefile最清楚它在构建什么,因此如何运行构建工件(可能还带有传递的参数)的决策最好委托给它 可供替代的 要从Makefi
:make
映射到一个键,因为我需要经常编译它。问题是我需要经常运行生成的可执行文件。但是键入:/变量\u程序\u名称
非常乏味
是否有任何方法可以检测/获取生成的可执行文件名?推荐的方法是通过Makefile中的一个单独目标执行此操作,例如
:make
仅触发生成,而:make run
触发可执行文件的(生成和)运行。毕竟,Makefile最清楚它在构建什么,因此如何运行构建工件(可能还带有传递的参数)的决策最好委托给它
可供替代的
要从Makefile“返回”可执行文件,将解析:make
输出并填充快速修复列表。您可以定义一个自定义映射(用于为此类窗口设置的qf
filetype),用于解析当前快速修复行中的可执行文件名,甚至可以使用getqflist()
解析整个输出。这要求Makefile以可检测的方式打印出可执行文件名(和路径)
可供替代的
如果您甚至无法从输出中可靠地获取可执行文件名,但知道生成可执行文件的目录,则可以在运行
:make
之前创建一个文件列表(使用glob()
),然后再运行一次,并比较两个列表以获取可执行文件名。如果您不想从Vim中删除以前的可执行文件,那么文件时间检查(getftime()
)可能会有所帮助。扩展Ingo Karkat的思想,这个脚本应该可以做到这一点(我不擅长vimscript,所以我用bash编写了它)
只需将此脚本放入您的$PATH
并映射一个键即可执行它。对于查看此脚本的任何人。。
我也有同样的需求,并决定用vim脚本/插件解决它。
感谢您的回答,但是我不希望污染makefile/cmakelistsI。我会发现这样一个目标在Vim之外也很有用。无论如何,我想到了另一个(更绝望的)选择;请参阅我的编辑。
#!/bin/sh
# This script tries to build the project using Makefile, and if that fails
# it tries to generate the Makefile with Cmake.
# Then it finds the latest executable file and runs it
# Remember to manually run cmake after changing CMakeLists.txt as a new
# Makefile will not be automatically regenerated
if [[ -e "Makefile" ]] || [[ -e "makefile" ]]; then
make
if [[ $? -ne 0 ]]; then
echo "Error when running make"
exit
fi
else
if [[ -e "CMakeLists.txt" ]]; then
cmake .
if [[ $? -ne 0 ]]; then
echo "Error when running cmake"
exit
fi
make
if [[ $? -ne 0 ]]; then
echo "Error when running make"
exit
fi
else
echo "CMakeLists.txt doesn't exist"
exit
fi
fi
# Find latest executable file
unset latest
for file in "${1:-.}"/*
do
if [[ -f "$file" ]]; then
latest=${latest-$file}
find "$file" -executable -prune -newer "$latest" | read -r dummy && latest=$file
fi
done
if [[ -x "$latest" ]]; then
./$latest
else
echo "Latest file $latest is not executable"
fi