Linking error OpenCV 2.4+CUDA windows 7(x64) comma

2019-06-27 03:27发布

问题:

I am trying to compile a program that uses both CUDA and OpenCV. I am sure that the paths to OpenCV are right because compiling a simple OpenCV program with this:

cl /I"%OPENCV_DIR%\include" /LINK"%OPENCV_DIR%\x64\vc10\lib\opencv_core240.lib" "%OPENCV_DIR%\x64\vc10\lib\opencv_highgui240.lib" testCV.cpp

it successfully compiles the program. Now when I try to compile with NVCC like this:

nvcc testCuda.cu --cl-version 2010 --use-local-env -I"%OPENCV_DIR%\include" -L"%OPENCV_DIR%\x64\vc10\lib\opencv_core240.lib" "%OPENCV_DIR%\x64\vc10\lib\opencv_highgui240.lib"

I got an error when trying to link that says:

error LNK2019: unresolved external symbol cvLoadImage referenced in function main a.exe : fatal error LNK1120: 1 unresolved externals

What am I missing or doing wrong when compiling with NVCC?

回答1:

-L is used to specify library directories, not files.

You probably want to execute:

nvcc testCuda.cu --cl-version 2010 --use-local-env -I"%OPENCV_DIR%\include" -L"%OPENCV_DIR%\x64\vc10\lib" -lopencv_core240 -lopencv_highgui240

If that doens't work, drop the -l and add their extensions:

nvcc testCuda.cu --cl-version 2010 --use-local-env -I"%OPENCV_DIR%\include" -L"%OPENCV_DIR%\x64\vc10\lib" opencv_core240.lib opencv_highgui240.lib

Once upon a time, when we had CUDA 2.x and OpenCV 2.1, I wrote a Makefile to compile an application that used both frameworks:

CXX=g++

CUDA_INSTALL_PATH=/usr/local/cuda
CFLAGS= -I. -I$(CUDA_INSTALL_PATH)/include -I/usr/include/opencv 
LDFLAGS= -L$(CUDA_INSTALL_PATH)/lib -lcudart -L/usr/lib -lcxcore -lcv -lhighgui -lcvaux -lml 

ifdef EMU
CUDAFLAGS+=-deviceemu
endif

all:
    $(CXX) $(CFLAGS) -c main.cpp -o main.o -m32 -arch i386
    nvcc $(CUDAFLAGS) -c kernel_gpu.cu -o kernel_gpu.o
    $(CXX) $(LDFLAGS) main.o kernel_gpu.o -o grayscale -arch i386

clean:
    rm -f *.o grayscale


回答2:

May be a missing -L before the second library file like the first one?



回答3:

I was just able to link against cuBLAS on Windows by adding a pragma directive to my code:

#pragma comment(lib,"cublas.lib")

This might work with OpenCV as well. Be aware that this is non-portable, though.