Repository: pytorch
Issue: Undefined reference to `cv::imread`
Labels: module: build,triaged,has workaround

## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->
Error message when using CMake: ```undefined reference to `cv::imread(std::string const&, int)'``` on a personal project.

## To Reproduce

Steps to reproduce the behaviour:

1. Install OpenCV from source on Linux [https://github.com/pytorch/examples/blob/master/cpp/tools/InstallingOpenCV.md](url)
2. Build LibTorch from source [https://github.com/pytorch/pytorch#from-source](url) at /home/USERNAME/pytorch
3. Run `make_project.sh` which has the following commands 
```
$ cmake -DCMAKE_PREFIX_PATH=~/pytorch ..
$ cmake --build . --config Release 
```
4. Error
```
(base) USERNAME@USERNAME-desktop:~/Tese/code/robust-deep-nets-master/ResilientArtificalNeuralNetworks/Code/build$ ./make_project.sh 
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/USERNAME/Tese/code/robust-deep-nets-master/ResilientArtificalNeuralNetworks/Code/build
Scanning dependencies of target trainTraffic
[ 50%] Building CXX object CMakeFiles/trainTraffic.dir/trainTraffic.cpp.o
[100%] Linking CXX executable trainTraffic
/usr/bin/ld: CMakeFiles/trainTraffic.dir/trainTraffic.cpp.o: in function `CustomDataset::get(unsigned long)':
trainTraffic.cpp:(.text._ZN13CustomDataset3getEm[_ZN13CustomDataset3getEm]+0x83): undefined reference to `cv::imread(std::string const&, int)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/trainTraffic.dir/build.make:109: trainTraffic] Error 1
make[1]: *** [CMakeFiles/Makefile2:95: CMakeFiles/trainTraffic.dir/all] Error 2

```

<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->

## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->
Expected my cpp source file `trainTraffic.cpp` to include the OpenCV library correctly.


## Environment
```
PyTorch version: 1.8.0a0+9b6fb85
Is debug build: False
CUDA used to build PyTorch: Could not collect
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.1 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: version 3.18.2

Python version: 3.8 (64-bit runtime)
Is CUDA available: False
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 2060
Nvidia driver version: 455.38
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.19.2
[pip3] numpydoc==1.1.0
[pip3] torch==1.8.0a0
[conda] blas                      1.0                         mkl  
[conda] magma-cuda110             2.5.2                         1    pytorch
[conda] mkl                       2020.2                      256  
[conda] mkl-include               2020.2                      256  
[conda] mkl-service               2.3.0            py38he904b0f_0  
[conda] mkl_fft                   1.2.0            py38h23d657b_0  
[conda] mkl_random                1.1.1            py38h0573a6f_0  
[conda] numpy                     1.19.2           py38h54aff64_0  
[conda] numpy-base                1.19.2           py38hfa32c7d_0  
[conda] numpydoc                  1.1.0              pyhd3eb1b0_1  
[conda] torch                     1.8.0a0                  pypi_0    pypi
```


## Additional context

This is my `CMakeLists.txt`:
```
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)

project(trainTraffic)
set(CMAKE_CXX_STANDARD 17)

find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED COMPONENTS core imgproc imgcodecs)

add_executable(${PROJECT_NAME} "trainTraffic.cpp")
target_link_libraries(${PROJECT_NAME} "${OpenCV_LIBS}")
target_link_libraries(${PROJECT_NAME} "${TORCH_LIBRARIES}")

configure_file("info_traffic.txt" "info_traffic.txt" COPYONLY)
```

My file `trainTraffic.cpp` is similar to  [https://raw.githubusercontent.com/pytorch/examples/master/cpp/custom-dataset/custom-dataset.cpp](url), I just switched the dataset path and made other small adjustments


cc @malfet @seemethere @walterddr