I want to compile it at the command prompt in NVCC of CUDA, but I get an error.

Asked 2 years ago, Updated 2 years ago, 110 views

The environment is
Windows 10 (64bit) CUDA 9.1

Here's the source code

#include<opencv2/opencv.hpp>
# include <opencv2/gpu/gpu.hpp>
# include <ocvlibs.h>

using namespace cv;

//--------------------------------------------------------------------------
//main
int main(intargc, char*argv[])
{
    try
    {
        int wait = 1;
        int numOfGpu=gpu::getCudaEnabledDeviceCount();

        if(numOfGpu<=0)
            throw("no Gpu available.");

        VideoCapture capture;

        if(argc==2)
        {
            capture=VideoCapture(argv[1]);
            wait = 33;
        }
        else
            capture=VideoCapture(0);

        const char*wName="dst";
        Mat src, dst;
        gpu::GpuMat gpuSrc, gpuDst;
        namedWindow(wName, CV_WINDOW_AUTOSIZE);

        while(true)
        {
            capture>>src;
            if(src.empty())break;

            gpuSrc.upload(src);
            gpu::cvtColor(gpuSrc, gpuDst, COLOR_RGB2GRAY);
            gpuDst.download(dst);

            imshow(wName,dst);

            if(waitKey(wait)>=0)break;
        }
    }
    catch(const char*str)
    {
        std::cout<<str<<std::endl;
    }
    catch(const cv::Exception*ex)
    {
        std::cout<<"Error:"<ex->what()<std::endl;
    }
    return 0;
}

The header file is unique, so I'll put it here.

//-----------------------------------------------------------------------------
# Ifdef_DEBUG//Debug Mode
    # pragma comment (lib, "opencv_calib3d246d.lib")
    # pragma comment (lib, "opencv_core246d.lib")
    # pragma comment (lib, "opencv_contrib246d.lib")
    # pragma comment (lib, "opencv_features2d246d.lib")
    # pragma comment (lib, "opencv_flann246d.lib")
    # pragma comment (lib, "opencv_gpu246d.lib")
    # pragma comment (lib, "opencv_haartrain_engined.lib")
    # pragma comment (lib, "opencv_highgui246d.lib")
    # pragma comment (lib, "opencv_imgproc246d.lib")
    # pragma comment (lib, "opencv_legacy246d.lib")
    # pragma comment (lib, "opencv_ml246d.lib")
    # pragma comment (lib, "opencv_objdetect246d.lib")
    # pragma comment (lib, "opencv_ts246d.lib")
    # pragma comment (lib, "opencv_video246d.lib")
# else // Release mode
    # pragma comment (lib, "opencv_calib3d246.lib")
    # pragma comment (lib, "opencv_core246.lib")
    # pragma comment (lib, "opencv_contrib246.lib")
    # pragma comment (lib, "opencv_features2d246.lib")
    # pragma comment (lib, "opencv_flann246.lib")
    # pragma comment (lib, "opencv_gpu246.lib")
    # pragma comment (lib, "opencv_haartrain_engine.lib")
    # pragma comment (lib, "opencv_highgui246.lib")
    # pragma comment (lib, "opencv_imgproc246.lib")
    # pragma comment (lib, "opencv_legacy246.lib")
    # pragma comment (lib, "opencv_ml246.lib")
    # pragma comment (lib, "opencv_objdetect246.lib")
    # pragma comment (lib, "opencv_ts246.lib")
    # pragma comment (lib, "opencv_video246.lib")
#endif

This is the error.

GPUGPU.cu
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin/../include\cuda_runtime.h:warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin/../include\w32api.h(186): warning C4005: '_EXTERN_C'—Macro redefined.
C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/../../VC/INCLUDE\yvals.h(560):note: '_EXTERN_C' previous definition
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\cuda_runtime_api.h (1950): warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\cuda_runtime_api.h (1950): warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h:warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(838): The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(1772)—The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(2628): The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h (3477)—The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h (4417): The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h (5319): The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(6229)—The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h (7104)—The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\math_functions.h(7914): The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h:warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(776): The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt/device_functions.h(1636): The warning C4819: file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\crt\device_double_functions.h:warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h:warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
c:\program files\nvidia gpu computing toolkit\cuda\v9.1\include\sm_20_intrinsics.h(925):warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin/../include\float.h(57): fatal error C1021: The preprocessor command 'include_next' is invalid.

The header file should have been loaded properly, but I don't understand why the error occurs.
Why do these errors occur?
Could you please help me to solve this problem?

c++ cuda gpu

2022-09-30 18:35

4 Answers

nvcc is a special compiler for compiling CUDA kernel codes (device codes for GPUs) and automatically generating connections (dispatchers) to CPU host codes.nvcc alone does not make sense.
The host code for CPU is passed to the host compiler where nvcc is parasitic and compiled.Visual C++ (cl.exe) is usually used as the host compiler in Windows environments.
What host environment do you use? (What version of Visual Studio?)
Originally, #include_next is a GCC-specific preprocessor, so this is probably due to an incorrectly configured link between nvcc and the host compiler.

But why do you want to use nvcc in the first place?

It looks like you're just using OpenCV 2.4.6's gpu module, so you shouldn't have to use nvcc at all.The code should normally be compiled as .cpp.
Also, you do not need to use nvcc in the command line by default, and Visual Studio Integration, which is usually installed with CUDA Toolkit, ensures that .cu files in Visual C++ projects are compiled using nvcc.CUDA beginners typically learn from project templates in CUDA console applications.Also, it is customary to write only code that uses CUDAC/C++ language in .cu, wrap it as a normal C/C++ function, and write code that does not use CUDAC/C++ language in .c or .cpp.p>

Note that C4819 is not an error, but a warning from the Visual C++ compiler.You should not rewrite the first-party or third-party library headers unnecessarily to erase the warning.
If you really have to use nvcc (CUDAC/C++ language), you can use the .cu file properties in the Visual C++ project to

under CUDAC/C++ → Command Line → Additional Options -Xcompiler"/wd4819"
allows you to erase the C4819 warning without unnecessary rewriting of the library header.

-Xcompiler is used to pass options to the host compiler.See the nvcc documentation for more information.
http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html

Additionally, depending on the CUDA Toolkit version, the supported Visual Studio and VC++ compiler versions are different.CUDA Toolkit 9.1 supports Visual Studio 2012/2013/2015/2017.VS2010 support is deprecated.
http://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/index.html


2022-09-30 18:35

It may not be necessary because the discussion seems to be progressing toward not using CUDA, but

warning C4819: The file contains characters that cannot be displayed on the current code page (932).Save the file in Unicode format to avoid data loss.

Although cubick has been offered a re-save source code and sygh has been offered a suppression of the /wd4819, we recommend that you configure the /source-charset (Set Source Character Set) option added in Visual C++ 2015.Visual C++ requires character code analysis for Unicode support (L" strings are converted to Unicode instead of being the byte string of the source code).There is no problem with the source code with the BOM, but if not, use the current OS configuration to assume that it is Shift-JIS (codepage932).Source code obtained from abroad may be Windows-1252, for example, Visual C++.

/source-charset:.1252

Or if it's CUDA

-Xcompiler"/source-charset:.1252"

allows you to correctly identify the character code in the source code.However, be aware that Shift-JIS will no longer be available this time.

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin/../include\w32api.h(186): warning C4005: '_EXTERN_C'—Macro redefined.
C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/../../VC/INCLUDE\yvals.h(560):note: '_EXTERN_C' Check the previous definition

C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.1\bin/../include\float.h(57): fatal error C1021: The preprocessor command 'include_next' is invalid.

The w32api.h and float.h in this path are header files for GCC (Is it MinGW?) and cannot be used in Visual C++. As sygh pointed out, it seems that the environment has not been built correctly.


2022-09-30 18:35

What kind of software do you use to describe source code, including header files?I don't think it's a notepad, but the header file (*.h) I created was saved as 932 (Shift-JIS), so it seems to be a compilation error.

Try saving the header file or source code back to Unicode (UTF-8) using an editor that allows you to specify and save the character code.

Note:
Text Editor for Windows
Change the character code of the file in Notepad++


2022-09-30 18:35

It seems that it has already been solved, but I will write it down just in case.
To compile OPENCV with CUDA First verify that the CUDA version you downloaded matches the VisualStudio version.Currently, CUDA 9.1 only supports up to 15.45.If the version is 15.5 or higher,
https://docs.microsoft.com/en-us/visualstudio/productinfo/installing-an-earlier-release-of-vs2017
If you're lucky, there's still a 15.45 link here, so please uninstall the current version and reinstall it using the installer that you can download from here.
Additional installation options include an English language pack.
Once this is done, open the control panel, open the region, select the Administration tab, and press the Change System Locale button at the bottom to change the language to English (US).
You will be asked to reboot, so I will restart it honestly and then CMAKE with CUDA included.
This way you can compile without having to re-save the CUDA header or source code in the editor.
You can also use the DLLs that have been created by restoring them to the code page.


2022-09-30 18:35

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.