miniob编译的时候遇到cmake命令错误

如题,在编译miniob的时候遇到如下错误

[mu@vm-cnt8:~/code/miniob]$ sudo bash build.sh init
build.sh init
HEAD is now at 5df3037d Merge branch 'release-2.1.12-stable-pull' into patches-2.1
build.sh: line 83: cmake: command not found
build.sh: line 91: cmake: command not found
build.sh: line 99: cmake: command not found
build.sh: line 107: cmake: command not found

根据字面意思,是cmake命令找不到,但是我的系统里面已经有了符合条件的环境;以下是gihub/miniob仓库中docs里面how_to_build.md的内容

MiniOB 需要使用:

  • cmake 版本 >= 3.13
  • gcc/clang gcc建议8.3以上,编译器需要支持c++20新标准
  • flex (2.5+), bison (3.7+) 用于生成词法语法分析代码

以下是我的系统中各个依赖项的版本号

[mu@vm-cnt8:~/code/miniob]$ gcc --version
gcc (GCC) 8.5.0 20210514 (Red Hat 8.5.0-20)
Copyright (C) 2018 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.

[mu@vm-cnt8:~/code/miniob]$ cmake --version
cmake version 3.27.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[mu@vm-cnt8:~/code/miniob]$ flex --version
flex 2.6.1
[mu@vm-cnt8:~/code/miniob]$ bison --version
bison (GNU Bison) 3.8
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2021 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.

系统是centos8-steam的vmware虚拟机;请问是我的依赖项安装有问题?还是有其他的原因?

当前使用的miniob的commit为 76221e46e66ef408771ce886aa0c586a09374b0d

你的cmake应该是在当前用户下安装的,使用root运行命令时找不到cmake。
试试 sudo -E bash build.sh init

sudo -E 是继承当前用户的环境变量运行sudo后面的命令,否则环境变量会被清理掉,所以就找不到只在当前用户安装的cmake

我的这些环境都是在root下面装的

[root@vm-cnt8:~]# cmake --version
cmake version 3.27.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).
[root@vm-cnt8:~]# flex --version
flex 2.6.1
[root@vm-cnt8:~]# bison --version
bison (GNU Bison) 3.8
Written by Robert Corbett and Richard Stallman.

Copyright (C) 2021 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.
[root@vm-cnt8:~]# cd pkg
[root@vm-cnt8:~/pkg]# ll
total 56344
drwxr-xr-x. 14  505 games     4096 Sep  1 00:53 bison-3.8
drwxr-xr-x. 14  505 games     4096 Sep  1 00:01 bison-3.8.1
-rw-r--r--.  1 root root   5606280 Aug 31 23:46 bison-3.8.1.tar.gz
-rw-r--r--.  1 root root   5588772 Sep  7  2021 bison-3.8.tar.gz
drwxrwxr-x. 15 root root      4096 Sep  1 00:00 cmake-3.27.4
-rw-r--r--.  1 root root  10980052 Aug 31 23:46 cmake-3.27.4.tar.gz
drwxrwxr-x. 19 root root      4096 Aug 31 23:37 openssl-1.1.1q
-rw-r--r--.  1 root root   9864061 Oct 12  2022 openssl-1.1.1q.tar.gz
drwxr-xr-x. 17 mu   mu        4096 Aug 31 23:39 Python-3.10.5
-rw-r--r--.  1 root root  25628472 Jun  6  2022 Python-3.10.5.tgz

感谢告知sudo的用法,但是依旧不行

[mu@vm-cnt8:~/code/miniob]$ sudo -E bash build.sh init
[sudo] password for mu: 
build.sh init
HEAD is now at 5df3037d Merge branch 'release-2.1.12-stable-pull' into patches-2.1
build.sh: line 83: cmake: command not found
build.sh: line 91: cmake: command not found
build.sh: line 99: cmake: command not found
build.sh: line 107: cmake: command not found

使用sudo -E cmake --version 看看结果

1 个赞
[mu@vm-cnt8:~]$ sudo -E cmake --version
[sudo] password for mu: 
sudo: cmake: command not found

确实啊,sudo了之后找不到cmake,为什么呢?

尝试添加一下cmake 到系统的PATH环境变量中。
sudo nano ~/.bashrc
文件末尾增加:
export PATH="/path/to/cmake/bin:$PATH"
执行:
source ~/.bashrc
再重试一下

感谢大佬的帮助,依照大佬所言定位了问题,并成功解决;

当前在子用户使用 sudo -E是找不到cmake命令的。

[mu@vm-cnt8:~]$ sudo -E cmake --version
[sudo] password for mu: 
sudo: cmake: command not found

我的系统里面的path环境变量如下。

[mu@vm-cnt8:~]$ sudo env | grep PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin

而cmake的路径如下

[mu@vm-cnt8:~]$ type cmake
cmake is /usr/local/bin/cmake
[mu@vm-cnt8:~]$ whereis cmake
cmake: /usr/local/bin/cmake /usr/share/cmake

有没有可能,是因为cmake不在PATH环境变量里面,导致sudo的时候找不到命令呢?虽然在root和mu用户下都可以直接执行cmake。

于是我就去root里面执行了一下软连接

[root@vm-cnt8:~]# ls /usr/bin | grep cmake
[root@vm-cnt8:~]# ln -s /usr/local/bin/cmake /usr/bin/cmake
[root@vm-cnt8:~]# ll /usr/bin | grep cmake
lrwxrwxrwx. 1 root root          20 Sep  1 05:57 cmake -> /usr/local/bin/cmake

再来试试

$ sudo -E cmake --version
cmake version 3.27.4

CMake suite maintained and supported by Kitware (kitware.com/cmake).

最后再来试试编译,应该是OK了,至少这次没有报错cmake命令找不到了!

更新一下结果,初始化构建和编译都成功了

感谢大佬,已经OK了(见楼上)

1 个赞