0

I try to set up a C++ environment for working with images (using opencv) and parallel computing. The main problem is that I can't success to link all the pieces together. As a mention, I use mingw from console (windows cmd), not from an IDE

What I did by now:

  • I added mingw bin to the system Path variable; if I run gcc cplusplus.cpp -o ccc -fopenmp -lstdc++ on cmd, it works fine (with #include <omp.h> in the cplusplus.cpp file)

now for CMake and opencv:

  • I added CMake bin to system Path variable
  • I added \opencv\build\x64\vc16\lib and \opencv\build\x64\vc16\bin to the system Path variable

NOW, the problem appear when I try to build mingw with CMake I think. This is c++ file:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
#include <omp.h>
using namespace std;
int main() {
    #pragma omp parallel
    {
    int d = omp_get_thread_num();
    cout<<d<<" Hello world"<<endl;
    }
    return 0;
}

This is CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)
project(OpenCV_cpp)

find_package(OpenCV REQUIRED)
find_package(OpenMP)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")


include_directories(${OpenCV_INCLUDE-DIRECTORIES})
add_executable(display_image cplusplus.cpp)

target_link_libraries(display_image ${OpenCV_LIBS})

In cmd: cmake . -G "MinGW Makefiles" works fine (ends without errors). But if I run mingw32-make.exe I get errors:

[ 50%] Linking CXX executable display_image.exe
d:/winlibs-x86_64-posix-seh-gcc-12.2.0-llvm-15.0.7-mingw-w64ucrt-10.0.0-r4/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\display_image.dir/objects.a(cplusplus.cpp.obj):cplusplus.cpp:(.text+0xe): undefined reference to 'omp_get_thread_num'

collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\display_image.dir\build.make:115: display_image.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/display_image.dir/all] Error 2
mingw32-make: *** [makefile:90: all] Error 2

As you can see from the error, the issue is related to openMP. How can I solve this?

EDIT: the above errors were solved, but now appeared a 'entry point could not be located' error - see the comments for details.

273K
  • 29,503
  • 10
  • 41
  • 64
dacian
  • 95
  • 1
  • 8
  • 1
    `x64\vc16\lib`, `x64\vc16\bin` - These paths are for libraries suitable for **Visual Studio**. For MinGW you need other libraries. – Tsyvarev Mar 11 '23 at 12:31
  • @Tsyvarev ok, I changed bin and lib for opencv with properly files for mingw; now, I get the output (the executable) but I have another problem - "Entry point could not be located in the dynamic link library... mingw\bin\libopencv_imgcodecs455.dll", I checked the bin directory and there exists libopencv_imgcodecs455.dll**.a** (so ending with **.a** extension), not simply with **.dll** extension. Is this normal, or I have some files missing? – dacian Mar 11 '23 at 13:03
  • If you used [msys2](https://www.msys2.org/#installation) to install MinGW (which I highly recommend) and installed opencv via pacman the correct dlls should be in `C:\msys64\mingw64\bin` – drescherjm Mar 11 '23 at 14:08
  • `libopencv_imgcodecs455.dll.a` is not the file you need to run your executable but needed in linking. Read here about `dll.a` files: [https://stackoverflow.com/questions/13998575/mingw-creating-dll-a-files-what-type-of-library-files-are-those](https://stackoverflow.com/questions/13998575/mingw-creating-dll-a-files-what-type-of-library-files-are-those) – drescherjm Mar 11 '23 at 14:09
  • @drescherjm I checked again and actually I have that .dll file in the bin directory. First time it was my mistake because is searched for it in lib directory. So in this case, the expected dll exists, but the entry point error still exists, what could I solve it? Would be akward to reinstall again mingw and opencv – dacian Mar 11 '23 at 14:21
  • ***Entry point could not be located in the dynamic link library*** Means you have some type of dll conflict caused by linking against a different import library than the one that matches the dll that windows found when it tried to execute your program. Make sure that the `opencv\build\x64\vc16\bin` folder is not in your windows `PATH` environment variable so windows does not find the dlls in that folder since they are incompatible with MinGW. Also make sure that the correct MinGW compiled opencv dlls are in your `PATH` – drescherjm Mar 11 '23 at 14:23
  • @drescherjm I checked again and the Path environment variable point to the correct bin and lib directories. I tried with another version for mingw opencv (initially with 4.5.5 and I got dll error for libopencv_imgcodecs455.dll) BUT NOW I tried with 4.5.2 and I receive a similar error (but now for libopencv_imgcodecs45**2**.dll). I don't know exactly, but I think that the problem is with the .dll file itself; could it be corrupted or something? I downloaded these files from https://github.com/huihut/OpenCV-MinGW-Build – dacian Mar 11 '23 at 15:24
  • It would be much easier for you if you just used msys2 and used pacman to install proper opencv binaries for your compiler. [https://packages.msys2.org/package/mingw-w64-ucrt-x86_64-opencv?repo=ucrt64](https://packages.msys2.org/package/mingw-w64-ucrt-x86_64-opencv?repo=ucrt64) this will install it into the system path for the MinGW 12.2 compiler that msys2 installs. so no need to change paths. – drescherjm Mar 11 '23 at 15:38
  • could I take only opencv archive without installing pacman or msys32? I tried with that tar.zst file but I cannot unzip it – dacian Mar 11 '23 at 15:49

0 Answers0