ubuntu中使用cmake编译报错No CMAKE_CXX_COMPILER could be found.的解决方法

时间:2024-10-21 09:07:48

ubuntu中使用cmake编译报错No CMAKE_CXX_COMPILER could be found.的解决方法

  • No CMAKE_CXX_COMPILER could be found.
  • Could NOT find CUDA (missing: CUDA_NVCC_EXECUTABLE CUDA_CUDART_LIBRARY)
  • Could not find a package configuration file provided by "OpenCV" with any
  • [E] [TRT] 6: [libLoader.cpp::Impl::293] Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resource.so.8.6.1: libnvinfer_builder_resource.so.8.6.1: cannot open shared object file: No such file or directory)
  • wsl安装cuda11-6
  • error: parameter packs not expanded with ‘...’:
  • Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resource.so.8.6.1: libnvinfer_builder_resource.so.8.6.1: cannot open shared object file: No such file or directory)

No CMAKE_CXX_COMPILER could be found.

ubuntu编译的时候出现以下报错:

CMake Error at CMakeLists.txt:3 (project):
  No CMAKE_CXX_COMPILER could be found.

  Tell CMake where to find the compiler by setting either the environment
  variable "CXX" or the CMake cache entry CMAKE_CXX_COMPILER to the full path
  to the compiler, or to the compiler name if it is in the PATH.

解决办法如下:

  1. 确保安装了C++编译器:
  • 对于 Windows,你可能需要安装 Visual Studio,并确保选择了 Desktop development with C++ 选项,这样才能安装C++编译器(如 cl.exe)。
  • 对于 Linux 和 macOS,你需要安装 g++ 或者 clang:
    Ubuntu: 你可以通过以下命令安装:
	sudo apt update
	sudo apt install build-essential

安装过程
在这里插入图片描述
重新编译,成功。报cuda错误
在这里插入图片描述

Could NOT find CUDA (missing: CUDA_NVCC_EXECUTABLE CUDA_CUDART_LIBRARY)

  1. 检查环境变量, ls /usr/local/cuda/bin,如果已安装,运行下面的代码,否则会提示没路径
export PATH=/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

然后运行 source ~/.bashrc 使配置生效。再试运行 nvcc --version。

  1. 否则安装CUDA开发包在这里插入代码片
sudo apt-get install nvidia-cuda-toolkit

在这里插入图片描述

Could not find a package configuration file provided by “OpenCV” with any

cmake出现以下报错

CMake Warning at CMakeLists.txt:28 (find_package):
  By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "OpenCV", but
  CMake did not find one.

  Could not find a package configuration file provided by "OpenCV" with any
  of the following names:

    OpenCVConfig.cmake
    opencv-config.cmake

  Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
  "OpenCV_DIR" to a directory containing one of the above files.  If "OpenCV"
  provides a separate development package or SDK, be sure it has been
  installed.

输入这个指令

sudo apt-get install libopencv-dev python3-opencv

在这里插入图片描述

[E] [TRT] 6: [libLoader.cpp::Impl::293] Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resource.so.8.6.1: libnvinfer_builder_resource.so.8.6.1: cannot open shared object file: No such file or directory)

报错原因:缺少tensorrt
下载tensorrt压缩包:https://developer.nvidia.com/downloads/compute/machine-learning/tensorrt/secure/8.6.1/tars/TensorRT-8.6.1.6.Linux.x86_64-gnu.cuda-11.8.tar.gz
在这里插入图片描述

tar -xzvf TensorRT-${version}.Linux.${arch}-gnu.${cuda}.tar.gz
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"/home/sjh/sjh/TensorRT-8.6.1.6/lib"

wsl安装cuda11-6

在这里插入图片描述

wget https://developer.download.nvidia.com/compute/cuda/repos/wsl-ubuntu/x86_64/cuda-wsl-ubuntu.pin
sudo mv cuda-wsl-ubuntu.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.6.0/local_installers/cuda-repo-wsl-ubuntu-11-6-local_11.6.0-1_amd64.deb
sudo dpkg -i cuda-repo-wsl-ubuntu-11-6-local_11.6.0-1_amd64.deb
sudo apt-key add /var/cuda-repo-wsl-ubuntu-11-6-local/7fa2af80.pub
sudo apt-get update
sudo apt-get -y install cuda

error: parameter packs not expanded with ‘…’:

环境:gcc-11、cuda11.6

gcc与cuda不匹配的错误
https://github.com/NVIDIA/nccl/issues/650

我使用的是gcc-11,尝试使用gcc-10

add_definitions(-std=c++11)
add_definitions(-DAPI_EXPORTS)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)

方法:

  1. 首先测试是否安装gcc-10
gcc-10 --version
g++-10 --version

如果未安装,请使用以下命令进行安装:

sudo apt update
sudo apt install gcc-10 g++-10
  1. 运行以下命令,将 GCC 10 和 G++ 10 设置为系统默认编译器:
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 100
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 100

再次输入:

gcc --version
g++ --version

在这里插入图片描述
3. 再次编译

cmake_minimum_required(VERSION 3.10)

project(yolov8)

set(CMAKE_C_COMPILER "/usr/bin/gcc-10")
set(CMAKE_CXX_COMPILER "/usr/bin/g++-10")

add_definitions(-std=c++11)
add_definitions(-DAPI_EXPORTS)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)

我这里还是c++11,但发现不影响,最终调用的还是c++10

rm -rf build
mkdir build && cd build
cmake ..
make

编译成功啦!!!
在这里插入图片描述

Error Code 6: Internal Error (Unable to load library: libnvinfer_builder_resource.so.8.6.1: libnvinfer_builder_resource.so.8.6.1: cannot open shared object file: No such file or directory)

Segmentation fault
报错问题:找不到libnvinfer_builder_resource.so.8.6.1
解决方法:

sudo cp /home/sjh/sjh/TensorRT-8.6.1.6/targets/x86_64-linux-gnu/lib/libnvinfer_builder_resource.so.8.6.1 /usr/lib