Why won't OpenCV compile in NVCC? - c++

I am trying to integrate CUDA and openCV in a project. Problem is openCV won't compile when NVCC is used, while a normal c++ project compiles just fine. This seems odd to me, as I thought NVCC passed all host code to the c/c++ compiler, in this case the visual studio compiler.
The errors I get are?
c:\opencv2.0\include\opencv\cxoperations.hpp(1137): error: no operator "=" matches these operands
operand types are: const cv::Range = cv::Range
c:\opencv2.0\include\opencv\cxoperations.hpp(2469): error: more than one instance of overloaded function "std::abs" matches the argument list:
function "abs(long double)"
function "abs(float)"
function "abs(double)"
function "abs(long)"
function "abs(int)"
argument types are: (ptrdiff_t)
So my question is why the difference considering the same compiler (should be) is being used and secondly how I could remedy this.

In general I would recommend keeping separation between host code and CUDA code, only using nvcc for the kernels and host "wrappers". This is particularly easy with Visual Studio, create your project as normal (e.g. a Console application) and then implement your application in .cpp files. When you want to run a CUDA function, create the kernel and a wrapper in one or more .cu files. The Cuda.rules file provided with the SDK will automatically enable VS to compile the .cu files and link the result with the rest of the .cpp files.

NVCC passes C++ code through to the host compiler, but it has to first parse and understand the code. Unfortunately, NVCC has troubles with STL. If at all possible, separate code that makes use of STL into .cpp files and have those compiled with Visual Studio (without passing them first through NVCC).

Compile the .cu code as a library and then link it to the main program. I suggest to use cmake as it makes the process a breeze

There's a project hosted at cuda-grayscale that shows how to integrate OpenCV + CUDA together. If you download the sources check the Makefile:
g++ $(CFLAGS) -c main.cpp -o Debug/main.o
nvcc $(CUDAFLAGS) -c kernel_gpu.cu -o Debug/kernel_gpu.o
g++ $(LDFLAGS) Debug/main.o Debug/kernel_gpu.o -o Debug/grayscale
It's a very simple project that demonstrates how to separate regular C++ code (OpenCV and stuff) from the CUDA code and compile them.

So there's no easy way to use nvcc to compile your current C++ code, you have to write wrappers and compile them using nvcc, while you compile the rest of your code using g++ or what-have-you?

Related

Is Boost's Small Vector Compatible with NVCC 8? [duplicate]

I'm trying to integrate CUDA to an existing aplication wich uses boost::spirit.
Isolating the problem, I've found out that the following code does not copile with nvcc:
main.cu:
#include <boost/spirit/include/qi.hpp>
int main(){
exit(0);
}
Compiling with nvcc -o cudaTest main.cu I get a lot of errors that can be seen here.
But if I change the filename to main.cpp, and compile again using nvcc, it works. What is happening here and how can I fix it?
nvcc sometimes has trouble compiling complex template code such as is found in Boost, even if the code is only used in __host__ functions.
When a file's extension is .cpp, nvcc performs no parsing itself and instead forwards the code to the host compiler, which is why you observe different behavior depending on the file extension.
If possible, try to quarantine code which depends on Boost into .cpp files which needn't be parsed by nvcc.
I'd also make sure to try the nvcc which ships with the recent CUDA 4.1. nvcc's template support improves with each release.

MinGW gcc gives errors when other compilers do not

Before you asked, yes i did look this up FOR DAYS. Im completely stuck... I'm using MINGW32 (my shortcut says MSYS) to compile my c and cpp code. For about 2 or 3 days now I have been getting strange errors. (below) It was working JUST FINE before. I even ran the same code i've compiled before and it gave the same error. I then go into DevC++ and open then compile an it works just fine.
ERRORS:
namespace: command not found
using: command not found
syntax error:
int main(){
(sometimes it gives me a big unreadable mess)
I'm really stuck... I dont want to have to switch to DevC++... I like to use my own text editor and compile in a command line.
From your errors namespace: command not found and using: command not found says to me that you aren't compiling the code with an appropriate compiler.
For reference in MinGW32 toolchain:
gcc.exe = C
g++.exe = C++
You may find it useful to take a look at what the IDE's actualy do with your compiler.
My current IDE allows me to see all the commands that it runs to build my project:
C:/mingw32/bin/g++.exe -c "C:/MyProgram/main.cpp" -g -O0 -std=c++14 -Wall -o ./Obj/main.cpp.o -I. -IDependencies/Something/include
So lets examine what this does.
My current toolchain is MinGW32 which is located in C:/mingw32/bin/g++.exe
g++ is our c++ compiler so we call g++.exe and we pass the following switch:
-c "C:/MyProgramm/main.cpp"
This tells my compiler to compile the main.cpp from my project directory. then my IDE adds a few additional command line switches. For the purpose of the answer I will only consider -o. This tells us the output file from our code main.cpp into an output file.
The reason we produce such a file is to save us time compiling so that we do not have to compile the same file twice without making changes to it. We perform this step on each of our files creating a collection of .o files.
The new file is then saved in "C:/MyProgramm/Obj/main.cpp.o"
Which means that your command line function will look something like this:
C:/mingw32/bin/g++.exe -c "<my project directory>/<file>.cpp" -o ./Obj/<file>.cpp.o
I would recommend that you read up on documentation for the g++ function and learn from different IDE's as you will soon find that you need to do more advanced things with your compiler.
For example to enable features from c++14 I add -std=c++14
*Edited to reflect feedback.

How to link using GCC commands in Bloodshed Dev-C++

I was trying to figure out how to link Fortran and C++ code, and one of the tutorials had written 2 programs, one in C++ in a file named testC.cpp, and the other in Fortran in a file named testF.f but I need to input the following compilation instructions:
gfortran -c testF.f
g++ -c testC.cpp
g++ -o test testF.o testC.o -lg2c
Problem is, I'm working in an IDE called Bloodshed Dev-C++ so I have no idea how to do this. I tried going in compiler options and in the general section I appended those instructions in the option "add the following commands when calling the compiler". Doesn't work.
Maybee you need use custom Makefile. Project->project options. Or include *.mak files

CUDA syntax error '<'

in my test.cu file (cu file item type is CUDA C/C++)
__global__ void foo()
{
}
void CudaMain()
{
foo<<<1,1>>>();
}
and in my test.cpp file
#include "mycuda.cu"
int main()
{
CudaMain();
return 0;
}
and compilator send me error "error c2059 syntax error ' <' " in test.cu file
Inclusion of CUDA source files in a C++ file doesn't work because this simply makes the CUDA source part of the C++ program code and regular C++ compilers do not understand CUDA syntax extensions. If you still want to keep your CUDA code separate from the non-CUDA C++ code, then you might want to look into separate compilation. CUDA source code can be compiled to regular object files, that can then be linked with other object files to produce an executable.
Modify the C++ code to read:
extern void CudaMain(void);
int main()
{
CudaMain();
return 0;
}
Compile the CUDA file with nvcc, the C++ code with your C++ compiler and then link the resulting object files with nvcc (you may also need to specify the standard C++ library in the link command):
$ nvcc -c -o test_cuda.o test.cu
$ g++ -c -o test_cpp.o test.cpp
$ nvcc -o test.exe test_cuda.o test_cpp.o -lstdc++
Edit: your question is about VS2010. May be you have to create custom build steps there.
Based on the thread here: https://forums.developer.nvidia.com/t/cuda-build-error/52615/4
Your test file extension should be .cu as well, but if you're using MSCV rename does not enough you should create a new CUDA C/C++ source module in your VS project.
Also you should put spaces between the <> operators like.
foo< < <1,1> > >();
Because C++ cannot parse the <<<>>>.
I know this is an old question but I was searching around and it jogged my memory for a solution that hasn't been mentioned.
The nvcc help offers:
--x {c|c++|cu} (-x)
Explicitly specify the language for the input files, rather than letting
the compiler choose a default based on the file name suffix.
Allowed values for this option: 'c','c++','cu'.
So although it's a bit of a blunt tool, you can do:
nvcc my_source.cpp -x cu ...
and it'll compile the .cpp as if it was named .cu (ie as CUDA).

Why does nvcc fails to compile a CUDA file with boost::spirit?

I'm trying to integrate CUDA to an existing aplication wich uses boost::spirit.
Isolating the problem, I've found out that the following code does not copile with nvcc:
main.cu:
#include <boost/spirit/include/qi.hpp>
int main(){
exit(0);
}
Compiling with nvcc -o cudaTest main.cu I get a lot of errors that can be seen here.
But if I change the filename to main.cpp, and compile again using nvcc, it works. What is happening here and how can I fix it?
nvcc sometimes has trouble compiling complex template code such as is found in Boost, even if the code is only used in __host__ functions.
When a file's extension is .cpp, nvcc performs no parsing itself and instead forwards the code to the host compiler, which is why you observe different behavior depending on the file extension.
If possible, try to quarantine code which depends on Boost into .cpp files which needn't be parsed by nvcc.
I'd also make sure to try the nvcc which ships with the recent CUDA 4.1. nvcc's template support improves with each release.