Bash: can't launch a program created with make on linux

此生再无相见时 提交于 2020-06-29 04:14:11

问题


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. Edit: The exact error message is: "/path/to/my/executable: no such file or directory"

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: Edit: 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

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?


回答1:


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
...

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



来源:https://stackoverflow.com/questions/56375416/bash-cant-launch-a-program-created-with-make-on-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!