How can I speed up build of c++ project using OpenCV on WSL2?

Viewed 844

I have a c++ project that uses the OpenCV library. It takes a long time to build when I run make. How can I speed up the build time?

I did install OpenCV with GPU support and thought this might be the bottleneck (since it may be compiling for different GPU architectures?), so I tried using cmake -D WITH_CUDA=OFF .. to build my project. Unfortunately, this didn't do the trick... Any tips?

Note: I'm fine disabling the GPU if it speeds up compile time significantly.

This is what my CMakeLists.txt file looks like:

cmake_minimum_required(VERSION 3.10)
project(Cruisin VERSION 1.0)
find_package( OpenCV REQUIRED)
find_package (Threads)

include_directories(include)
include_directories( ${OpenCV_INCLUDE_DIRS} )

file(GLOB SOURCES src/*.cpp app/main.cpp)

add_executable(main ${SOURCES})
target_link_libraries( main ${CMAKE_THREAD_LIBS_INIT} ) # Add threading lib
target_link_libraries( main ${OpenCV_LIBS} )

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)

Other Details:

  • Compiler: gcc

  • OS: virtual Ubuntu 20.04 (WSL2 on Windows 10)

  • Project structure: there's one main cpp file in ROOT/app/, 2 source files in ROOT/src/ with 2 corresponding headers in ROOT/include/

  • OpenCV and my project files are located on the regular Windows file system (accessed through /mnt/ directory), but CUDA and Make/CMake are in the virtual WSL

Edit: Solution

The issue seemed to be WSL being very slow with any I/O being done in the windows filesystem. In my case, I was using OpenCV installed somewhere in mnt/ (Windows filesystem). I rebuilt OpenCV in WSL somewhere in /, and moved my project there as well and it is so much faster.

3 Answers

Are you rebuilding OpenCV library each time you build your project? This can happen unintentionally in some cases if you add the library as an external project. OpenCV should only be built once as a target and then linked in to your executable target with target_link_libraries.

Also you can set the -j flag for number of threads, see https://cmake.org/cmake/help/v3.12/manual/cmake.1.html#build-tool-mode.

It is difficult to say more without seeing your CMakeLists.txt file.

use make -j x x = number of cores example:

make -j 8
Related