Bash:无法启动在Linux上使用make创建的程序 [英] Bash: can't launch a program created with make on linux

查看:46
本文介绍了Bash:无法启动在Linux上使用make创建的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用make来构建程序,然后通过bash脚本启动创建的可执行文件,但是bash找不到可执行文件,尽管它已创建并且可以手动启动.该问题仅存在于Gnome终端中的Linux mint 19上.确切的错误消息是:"/path/to/my/executable:没有这样的文件或目录"

I want to build my program using make and then launch the created executable via bash script, but bash can't find the executable, although it was created and i can launch it manually. The problem only exist on linux mint 19 in Gnome-terminal. The exact error message is: "/path/to/my/executable: no such file or directory"

我有一个跨平台项目,需要运行cmake,然后构建该项目,最后启动创建的可执行文件.我有一个bash脚本来自动化该过程:它只是造成麻烦的部分;)

I have a cross-platform project where i need to run cmake, then build the project and finally launch the created executable. I have a bash script to automate the process: Its only the part that causes the trouble ;)

for TASK in $@; do
    if [[ $TASK == "make" ]]; then
        call cmake here, this creates a .sln or a make file
    elif [[ $TASK == "build" ]]; then
        if  [[ $OS == 'CYGWIN_NT-10.0' ]]; then
            MSBuild.exe "./build/debug/myproject.sln"
        elif [[ $OS == 'Linux' ]]; then
            cd ./build/debug/ && make
        else
            error...
        fi
    elif [[ $TASK == "run" ]]; then
        if  [[ $OS == 'CYGWIN_NT-10.0' ]]; then
            ./build/debug/Debug/program.exe
        elif [[ $OS == 'Linux' ]]; then
            ./build/debug/program
        else
            error...
        fi
    else
        error...
    fi
done

例如,调用"./script.sh make build run"应调用cmake创建构建文件,然后调用构建程序(在Linux上为make或在Windows上为msbuild),然后启动创建的可执行文件.这在cygwin终端的Windows 10上正常工作.在Linux上,调用"./script.sh构建运行"失败,因为它找不到可执行文件.但是,"./script.sh build& ./build/debug/program"效果很好.令人惊讶地,"./script.sh build& ../script.sh run"也按预期工作.这是为什么?bash脚本中是否有任何Bug?为什么它不能在Cygwin上运行,但不能在Linux Mint上运行?

Calling "./script.sh make build run" should, for example, call cmake to create the build files, then call the build program (make on linux or msbuild on windows) and then launch the created executable. This works fine on windows 10 in a cygwin terminal. On Linux the call "./script.sh build run" fails, because it can't find the executable. However "./script.sh build && ./build/debug/program" works nicely. Surprisingly "./script.sh build && ./script.sh run" also works as expected. Why is that? Is there any Bug in the bash script? And why does it work on Cygwin but not on linux mint?

推荐答案

好,我解决了.提出问题似乎是找到解决方案的第一步.问题似乎在于,调用"./script.sh构建运行"首先会评估构建命令"cd ./build/debug/&&make".我想这样的命令将产生一个新的子shell,然后在其中评估cd和make命令,然后shell将在原始工作目录中继续.显然,这不会发生,执行脚本的工作目录更改为"./build/debug".下一个命令运行"尝试执行"./build/debug/program",但是由于当前工作目录已更改,因此它实际上正在寻找不存在的"./build/debug/build/debug/program".我添加了行

Ok, I solved it. Asking questions seems to be the first step to finding a solution. The problem seems to be, that the call "./script.sh build run" first evaluates the build command "cd ./build/debug/ && make". I thougth such commands would spawn a new sub shell, where the cd and make commands are then evaluated and then the shell would continue in the original working directory. Apparently this does not happen and the working directory of the executing script is changed to "./build/debug". The next command "run" tries to execute "./build/debug/program" but since the current working directory is changed, it is essentially looking for "./build/debug/build/debug/program" which does not exist. I added the line

cd ../../

生成命令的代码.现在看起来像这样

to the code for the build-command. It now looks like this

...
elif [[ $OS == 'Linux' ]]; then
    cd ./build/debug/ && make
    cd ../../
else
...

我不知道为什么,但是在Linux上可以这样工作.

I don't know exactly why, but it works this way on linux.

这篇关于Bash:无法启动在Linux上使用make创建的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆