我在运行.sh
文件的 gvim 工具栏上添加了一个按钮。该.sh
文件运行 scons 以在 /build 子目录中构建我的 c++ 应用程序并运行它。问题是,当应用程序运行时,它的当前工作目录是包含文件的.sh
文件夹(而不是应用程序 /build 子目录)!那么如何从.sh
文件运行构建的 c++ 应用程序可执行文件(linux),以便其工作目录成为包含可执行文件的文件夹?
问问题
948 次
2 回答
2
只是
cd $(dirname "$0")
./exec_test
注意,你需要 ./exec_test
,exec_test
除非目录实际上已经在PATH
于 2013-11-02T23:51:53.493 回答
1
这是一个类似的例子(我不使用scons
。)
我添加我的工具栏图标:
:amenu ToolBar.mytool :!/home/me/code/misc/foo.sh "%"
对我来说,当我单击它时,vim
在与vim
.
foo.sh
包含:
#!/bin/bash
set -e
# You should see the name of your file.
# It might just be "my_file.c"
echo "$1"
# This will tell you where your script is current cd'd to.
pwd
# `cd` to where the file passed on the command line is:
cd "$(dirname "$1")"
# Look for "CMakeLists.txt"
# You only need this loop if your build file / program might be up a few directories.
# My stuff tends to be:
# / - project root
# CMakeLists.txt
# src/
# foo.c
# bar.c
while true; do
# We found it.
if [[ -e "CMakeLists.txt" ]]; then
break
fi
# We didn't find it. If we're at the root, just abort.
if [[ "`pwd -P`" = "/" ]]; then
echo "Couldn't find CMakeLists.txt." >&2
exit 1
fi
cd ..
done
# I do builds in a separate directory.
cd build && make
您将替换CMakeLists.txt
为SConstruct
,最后一个cd build && make
替换为scons
,或适合scons
.
于 2013-11-02T23:53:02.583 回答