我使用这些 bash 脚本在 bin 目录中创建了指向 exe 文件的符号链接:
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cat do_links.sh
./make_links.sh as
./make_links.sh nm
./make_links.sh ar
./make_links.sh ranlib
./make_links.sh strip
./make_links.sh objdump
./make_links.sh ld
./make_links.sh gcov
./make_links.sh gcc-4.0.1
./make_links.sh gcc
./make_links.sh gprof
./make_links.sh gccbug
./make_links.sh g++
./make_links.sh c++
./make_links.sh strings
./make_links.sh c++filt
./make_links.sh size
./make_links.sh cpp
./make_links.sh readelf
./make_links.sh addr2line
./make_links.sh objcopy
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cat make_links.sh
ln -s arm-unknown-linux-gnu-$1 $1
然后我在 bin 目录中运行 ./do_links.sh ,这就是 bin 目录的样子:
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ lt
total 26552
-rwxr-xr-x 2 dan dan 2566199 May 30 16:44 arm-unknown-linux-gnu-as
-rwxr-xr-x 2 dan dan 1762396 May 30 16:44 arm-unknown-linux-gnu-nm
-rwxr-xr-x 2 dan dan 1706104 May 30 16:44 arm-unknown-linux-gnu-ar
-rwxr-xr-x 2 dan dan 1706099 May 30 16:44 arm-unknown-linux-gnu-ranlib
-rwxr-xr-x 2 dan dan 2203025 May 30 16:44 arm-unknown-linux-gnu-strip
-rwxr-xr-x 2 dan dan 2346625 May 30 16:44 arm-unknown-linux-gnu-objdump
-rwxr-xr-x 2 dan dan 2471491 May 30 16:44 arm-unknown-linux-gnu-ld
-rwxr-xr-x 1 dan dan 67493 May 30 16:44 arm-unknown-linux-gnu-gcov
-rwxr-xr-x 2 dan dan 201417 May 30 16:44 arm-unknown-linux-gnu-gcc-4.0.1
-rwxr-xr-x 2 dan dan 201417 May 30 16:44 arm-unknown-linux-gnu-gcc
-rwxr-xr-x 1 dan dan 2013095 May 30 16:44 arm-unknown-linux-gnu-gprof
-rwxr-xr-x 1 dan dan 16196 May 30 16:44 arm-unknown-linux-gnu-gccbug
-rwxr-xr-x 2 dan dan 206621 May 30 16:44 arm-unknown-linux-gnu-g++
-rwxr-xr-x 2 dan dan 206621 May 30 16:44 arm-unknown-linux-gnu-c++
-rwxr-xr-x 1 dan dan 1602788 May 30 16:44 arm-unknown-linux-gnu-strings
-rwxr-xr-x 1 dan dan 1692490 May 30 16:44 arm-unknown-linux-gnu-c++filt
-rwxrwxr-x 1 dan dan 12281 May 30 16:44 fix-embedded-paths
-rwxr-xr-x 1 dan dan 1620406 May 30 16:44 arm-unknown-linux-gnu-size
-rwxr-xr-x 1 dan dan 202864 May 30 16:44 arm-unknown-linux-gnu-cpp
-rwxr-xr-x 1 dan dan 398151 May 30 16:44 arm-unknown-linux-gnu-readelf
-rwxr-xr-x 1 dan dan 1727820 May 30 16:44 arm-unknown-linux-gnu-addr2line
-rwxr-xr-x 1 dan dan 2203026 May 30 16:44 arm-unknown-linux-gnu-objcopy
-rwxrwxr-x 1 dan dan 339 May 30 18:31 check_ver.sh
-rwxrwxr-x 1 dan dan 460 May 30 19:09 do_links.sh
-rwxr-xr-x 1 dan dan 129 May 30 19:13 make_links.sh
lrwxrwxrwx 1 dan dan 24 May 30 19:38 as -> arm-unknown-linux-gnu-as
lrwxrwxrwx 1 dan dan 24 May 30 19:38 nm -> arm-unknown-linux-gnu-nm
lrwxrwxrwx 1 dan dan 28 May 30 19:38 ranlib -> arm-unknown-linux-gnu-ranlib
lrwxrwxrwx 1 dan dan 24 May 30 19:38 ar -> arm-unknown-linux-gnu-ar
lrwxrwxrwx 1 dan dan 27 May 30 19:38 strip -> arm-unknown-linux-gnu-strip
lrwxrwxrwx 1 dan dan 29 May 30 19:38 objdump -> arm-unknown-linux-gnu-objdump
lrwxrwxrwx 1 dan dan 24 May 30 19:38 ld -> arm-unknown-linux-gnu-ld
lrwxrwxrwx 1 dan dan 26 May 30 19:38 gcov -> arm-unknown-linux-gnu-gcov
lrwxrwxrwx 1 dan dan 31 May 30 19:38 gcc-4.0.1 -> arm-unknown-linux-gnu-gcc-4.0.1
lrwxrwxrwx 1 dan dan 25 May 30 19:38 gcc -> arm-unknown-linux-gnu-gcc
lrwxrwxrwx 1 dan dan 27 May 30 19:38 gprof -> arm-unknown-linux-gnu-gprof
lrwxrwxrwx 1 dan dan 28 May 30 19:38 gccbug -> arm-unknown-linux-gnu-gccbug
lrwxrwxrwx 1 dan dan 25 May 30 19:38 g++ -> arm-unknown-linux-gnu-g++
lrwxrwxrwx 1 dan dan 25 May 30 19:38 c++ -> arm-unknown-linux-gnu-c++
lrwxrwxrwx 1 dan dan 29 May 30 19:38 strings -> arm-unknown-linux-gnu-strings
lrwxrwxrwx 1 dan dan 29 May 30 19:38 c++filt -> arm-unknown-linux-gnu-c++filt
lrwxrwxrwx 1 dan dan 26 May 30 19:38 size -> arm-unknown-linux-gnu-size
lrwxrwxrwx 1 dan dan 25 May 30 19:38 cpp -> arm-unknown-linux-gnu-cpp
lrwxrwxrwx 1 dan dan 29 May 30 19:38 readelf -> arm-unknown-linux-gnu-readelf
lrwxrwxrwx 1 dan dan 31 May 30 19:38 addr2line -> arm-unknown-linux-gnu-addr2line
lrwxrwxrwx 1 dan dan 29 May 30 19:38 objcopy -> arm-unknown-linux-gnu-objcopy
所以我可以在该目录中执行符号链接:
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
但是当我尝试在其他任何地方执行符号链接时:
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cd ..
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu$ bin/gcc --version
bash: bin/gcc: No such file or directory
只是为了好玩,我在 bin 目录中创建了一个 bash 脚本:
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ echo "gcc --version" > check.sh
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ chmod +x check.sh
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ ./check.sh
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin$ cd ..
dan@Linux-Desktop:~/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu$ bin/check.sh
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
它可以在任何其他目录中正常工作。
然后我认为它与其他gcc冲突并将链接名称更改为gcc2:
dan@Linux-Desktop:~$ which gcc
/usr/bin/gcc
dan@Linux-Desktop:~$ which gcc2
/home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/gcc2
dan@Linux-Desktop:~$ gcc2 --version
bash: /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/gcc2: No such file or directory
我什至无法从任何其他目录运行显式 exe:
dan@Linux-Desktop:~$ cat testd.sh
ls -ltr /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc
/home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc --version
dan@Linux-Desktop:~$ ./testd.sh
-rwxr-xr-x 2 dan dan 201417 May 30 16:44 /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc
./testd.sh: line 2: /home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin/arm-unknown-linux-gnu-gcc: No such file or directory
dan@Linux-Desktop:~$
这是我的PATH
:
dan@Linux-Desktop:~$ printenv PATH
/home/dan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/dan/java/jdk-11.0.11+9/bin:/home/dan/dev/arm/gcc-4.0.1-glibc-2.3.5/arm-unknown-linux-gnu/bin
假设:您尝试运行的可执行文件适用于不兼容的架构
或者它错过了所需的库。
尤其是:
gcc --version
运行,而不是目录中存在的文件(符号链接)。大概是吧。标题是“除非在链接所在的目录中,否则无法执行指向 exe 的符号链接”,但“除非”部分是一种误解。在您没有运行符号链接的目录中。会运行它。gcc
PATH
/usr/bin/gcc
./gcc --version
bin/gcc --version
尝试运行符号链接,它指向arm-unknown-linux-gnu-gcc
哪个触发每个假设的误导性错误。check.sh
以任何一种方式工作,因为通过您的第一个找到它的地方PATH
是目录,并且问题不适用于运行脚本本身。注意gcc
脚本内部的位置PATH
类似于(1),它的工作方式类似于(1)。gcc2
,因为. 唯一的是目录中的符号链接,它指向有问题的可执行文件。gcc2
PATH
gcc2
testd.sh
显式调用并出现问题。arm-unknown-linux-gnu-gcc