Ask Your Question

cuda_runtime.h error without even including it

asked 2016-08-05 18:28:48 -0500

shaun gravatar image


I'm written a very simple "hello world" opencv3 program from which I'm going to start my project. But I am getting an error in currently setting an error and I'm not sure if its because of something I did, or a bug (from the latest snapshot of the code that I cloned). Below is the setup.

Here is my CMakeLists.txt file:

cmake_minimum_required ( VERSION 2.8 )

set ( CMAKE_CXX_FLAGS "-std=c++11" )
set ( PROJ_NAME "cv_pp" )

find_package ( OpenCV REQUIRED )
include_directories ( ${OpenCV_INCLUDE_DIR} )

file ( GLOB_RECURSE PROJ_SRCS src/*.cpp )

include_directories ( "${PROJECT_INCLUDE_DIR}" )
add_executable ( ${PROJ_NAME} ${PROJ_SRCS} )
target_link_libraries ( ${PROJ_NAME} ${OpenCV_LIBS} )

Here is the C++ source file:

#include <iostream>
#include <opencv2/opencv.hpp>

void print_version()
    std::cout << "OpenCV version : " << CV_VERSION << std::endl;
    std::cout << "Major version : " << CV_MAJOR_VERSION << std::endl;
    std::cout << "Minor version : " << CV_MINOR_VERSION << std::endl;
    std::cout << "Subminor version : " << CV_SUBMINOR_VERSION << std::endl;

int main(int argc, char** argv )
    if ( argc != 2 )
        std::cerr << "Usage: " << argv[0] << " <Image_Path>\n";
        return -1;

    cv::Mat image;
    image = cv::imread( argv[1], 1 );

    if ( ! )
        std::cerr << "No image data \n";
        return -1;

    cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE );
    cv::imshow("Display Image", image);


    return 0;

Here is the error about cuda_runtime that I'm getting:

[100%] Building CXX object CMakeFiles/cv_pp.dir/src/cv_pp.cpp.o
In file included from /usr/local/include/opencv2/cudalegacy.hpp:47:0,
                 from /usr/local/include/opencv2/opencv.hpp:78,
                 from /home/sudarshan/research/cv_pp/src/cv_pp.cpp:4:
/usr/local/include/opencv2/cudalegacy/NCV.hpp:52:26: fatal error: cuda_runtime.h: No such file or directory
 #include <cuda_runtime.h>
compilation terminated.
make[2]: *** [CMakeFiles/cv_pp.dir/src/cv_pp.cpp.o] Error 1
make[1]: *** [CMakeFiles/cv_pp.dir/all] Error 2
make: *** [all] Error 2

The error seems to indicate that I haven't linked the CUDA libraries to my exe. I'm aware of this since I haven't linked it in my CMakeLists.txt. However, I'm not including cuda_runtime.h in code. This means that opencv2/opencv.hpp must include it in theirs by default. Does this mean I have to link the CUDA libs even if I'm not going to be using any of the CUDA stuff? If that's the case could someone show me how to link the CUDA libs in my CMakeLists file?

Path exports after installing CUDA:

# cuda
export PATH=/usr/local/cuda-7.5/bin:$PATH
export LD_LIBRARY_PATH=/home/user/cudaNN/lib64:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH


  • Operating System 64-bit Ubuntu 14.04
  • OpenCV 3 built and installed from source following instructions from here, but cloned source from here.
  • Installed Cuda 7.5 along with cudaNN (not required for this error but listing it anyway).

Any help is appreciated.

Thanks, Shaun.

edit retag flag offensive close merge delete


did you build the opencv libs with cuda support ?

berak gravatar imageberak ( 2016-08-06 00:16:31 -0500 )edit
berak gravatar imageberak ( 2016-08-06 00:22:25 -0500 )edit

Sorry for breaking it down! Building the system to that exact point now and will try to get a solution going!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-08 04:29:53 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2016-08-09 05:02:01 -0500

So I get back to you with the solution. First of all, the PR itself did not really break it, but rather you should make sure that your system is set up correctly. Had the same issue of cuda_runtime.h not being found, but doing the following steps clearly fixed it for me. I am on Ubuntu, but it will probably be quite similar on other systems.

  1. Clean up your system. This means going to /usr/local/ and simply deleting all folders containing cuda in their name. This will make sure old versions are completely gone. Do the same at your home location /home/username/ because samples and such tend to go there for a specific installation.
  2. Get yourself the latest stable NVIDIA CUDA installer, which was CUDA7.5 for me.
  3. Run the installer AND run the included video driver. If there is a mismatch between these two, it might jug up your CUDA installation making it not run smooth. Also install the CUDA samples, so you are sure CUDA is working properly on your system. [NOTE: I took all default paths, if you change them, you will need to change everything accordingly]

Before we can continue to OpenCV, we need to make sure that our system is fully configured for using CUDA.

  1. Go to /etc/
  2. Inside the folder, make a file called cuda.conf and inside add this rule /usr/local/cuda/lib64
  3. Save the file and run sudo ldconfig
  4. Go to your home directory and edit the .bashrc file, adding both export PATH="$PATH:/usr/local/cuda/bin" and export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib at the end of the file. This will simply make sure that every time you open a terminal, that your system knows where CUDA can be found.

Now you can go get the latest OpenCV 3 master branch from github. When running cmake it will look for the installation and find the CUDA7.5 installation normally. It is very important that you check this. If it does not, run the cmake-gui installer and manually set the paths to your installation.

Now you are all set up. However there are still 2 important things to do

  1. If you are using an IDE or gcc/g++, do not forget to add -L /usr/local/cuda/lib64 to your linker settings.
  2. Add the following folders to your compiler search directions /usr/local/cuda/lib64 and /usr/local/lib/.

Using these guidelines every OpenCV GPU sample will build and run. The suggested opencv.hpp header expansion was partially undone, but there is a new PR going in to add the CUDA modules none the less, so if this above is set up correctly, the header will work just fine again!

edit flag offensive delete link more


Thank you, I've been following the bug thread. I am still using the an earlier commit (based on the op on the bug thread) and it works fine. The problem I had (during install of CUDA) is if I install the video driver along with CUDA7.5 my system ends up crashing even though the included driver is the same as the additional drivers on available to Ubuntu from the software sources. That is why I didn't install the driver (and also didn't install the samples because it just took longer). I'm curious why a CUDA installer would hurt the OpenCV compilation especially since it was actually working before an update.

shaun gravatar imageshaun ( 2016-08-09 06:48:25 -0500 )edit

But thats just it, it wasn't working beforehand, simply because the opencv.hpp header was incomplete, thus not including the necessary headers and thus not warning that it would crash. In this new PR you can open up the discussions. Basically there are 2 headers that are heavily dependant on cuda being explicitly linked via the compiler, that is cudev.hpp (which can only be accessed by .cu files) and cudalegacy (which is legacy code and just there for backwards compatibility). All the other cuda modules have fully wrapped CUDA functionality and thus do not need explicit cuda runtime libraries like those two headers. Therefore it was decided to excluded those 2 from the opencv.hpp header. The new file awaiting merge will be correct now!

StevenPuttemans gravatar imageStevenPuttemans ( 2016-08-10 02:12:17 -0500 )edit

answered 2016-08-06 07:46:35 -0500

shaun gravatar image

Yup. I saw that bug thread, just before coming here. I was able to checkout a previous commit and got the thing working (which was working 2 days before I built it again yesterday).

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2016-08-05 18:28:48 -0500

Seen: 3,930 times

Last updated: Aug 09 '16