Skip to content

cmake: Fix DirectX detection in mingw#22462

Merged
alalek merged 3 commits intoopencv:4.xfrom
Biswa96:fix-directx-check
Oct 3, 2022
Merged

cmake: Fix DirectX detection in mingw#22462
alalek merged 3 commits intoopencv:4.xfrom
Biswa96:fix-directx-check

Conversation

@Biswa96
Copy link
Copy Markdown
Contributor

@Biswa96 Biswa96 commented Sep 2, 2022

The pragma comment directive is valid for MSVC only. So, the DirectX detection
fails in mingw. The failure is fixed by adding the required linking library
(here d3d11) in the try_compile() function in OpenCVDetectDirectX.cmake file.
Also add a message if the first DirectX check fails.

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

The pragma comment directive is valid for MSVC only. So, the DirectX detection
fails in mingw. The failure is fixed by adding the required linking library
(here d3d11) in the try_compile() function in OpenCVDetectDirectX.cmake file.
Also add a message if the first DirectX check fails.
#include <windows.h>

#include <d3d11.h>
#pragma comment (lib, "d3d11.lib")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is still several places in OpenCV codebase which use this approach (and they don't use LINK_LIBRARIES).
Fix should be complete and not introduce mess.

Try cmake -DBUILD_EXAMPLES=ON ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. I have already built those. But I thought those will require a separate PR according to the guidelines. I shall add another commit here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question. The samples/opengl/opengl_interop.cpp file contains pragma comment directive but it is not compiled for Windows platform because X11_FOUND is false. Would you like to take look at it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the required changes. Please review.

These changes remove MSVC specific pragma directive. The compilation fails at
linking time due to absence of proper linking library. The required libraries
are added in corresponding CMakeLists.txt file.
These changes remove MSVC specific pragma directive. The compilation fails at
linking time due to absence of proper linking library. The required libraries
are added in corresponding CMakeLists.txt file.
@asmorkalov
Copy link
Copy Markdown
Contributor

@alalek please take a look.

@asmorkalov
Copy link
Copy Markdown
Contributor

~/Projects/opencv$ grep -Rin "#pragma comment"
modules/gapi/test/streaming/gapi_streaming_vpl_device_selector.cpp:19:#pragma comment(lib,"d3d11.lib")
modules/core/src/system.cpp:190:#pragma comment(lib, "runtimeobject.lib")
modules/videoio/src/cap_obsensor/obsensor_stream_channel_msmf.cpp:31:#pragma comment(lib, "mfplat")
modules/videoio/src/cap_obsensor/obsensor_stream_channel_msmf.cpp:32:#pragma comment(lib, "mf")
modules/videoio/src/cap_obsensor/obsensor_stream_channel_msmf.cpp:33:#pragma comment(lib, "mfuuid")
modules/videoio/src/cap_obsensor/obsensor_stream_channel_msmf.cpp:34:#pragma comment(lib, "Strmiids")
modules/videoio/src/cap_obsensor/obsensor_stream_channel_msmf.cpp:35:#pragma comment(lib, "Mfreadwrite")
modules/videoio/src/cap_obsensor/obsensor_stream_channel_msmf.cpp:36:#pragma comment(lib, "dxgi")
modules/videoio/src/cap_obsensor/obsensor_stream_channel_msmf.cpp:37:#pragma comment(lib, "Shlwapi")
modules/videoio/src/cap_winrt_video.cpp:53:#pragma comment(lib, "mfplat")
modules/videoio/src/cap_winrt_video.cpp:54:#pragma comment(lib, "mf")
modules/videoio/src/cap_winrt_video.cpp:55:#pragma comment(lib, "mfuuid")
modules/videoio/src/cap_winrt_video.cpp:58:#pragma comment(lib, "Shlwapi")
modules/videoio/src/cap_msmf.cpp:47:#pragma comment(lib, "mfplat")
modules/videoio/src/cap_msmf.cpp:48:#pragma comment(lib, "mf")
modules/videoio/src/cap_msmf.cpp:49:#pragma comment(lib, "mfuuid")
modules/videoio/src/cap_msmf.cpp:50:#pragma comment(lib, "Strmiids")
modules/videoio/src/cap_msmf.cpp:51:#pragma comment(lib, "Mfreadwrite")
modules/videoio/src/cap_msmf.cpp:52:#pragma comment(lib, "dxgi")
modules/videoio/src/cap_msmf.cpp:54:#pragma comment(lib, "d3d11")
modules/videoio/src/cap_msmf.cpp:71:#pragma comment(lib, "Shlwapi.lib")

@Biswa96
Copy link
Copy Markdown
Contributor Author

Biswa96 commented Sep 10, 2022

Those can not be compiled with mingw-w64 toolchain yet. missing symbols, compiler error etc.

Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍
Thank you for contribution!

@alalek alalek merged commit 6cf0910 into opencv:4.x Oct 3, 2022
@Biswa96 Biswa96 deleted the fix-directx-check branch October 3, 2022 05:37
@alalek alalek mentioned this pull request Jan 8, 2023
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
* cmake: Fix DirectX detection in mingw

The pragma comment directive is valid for MSVC only. So, the DirectX detection
fails in mingw. The failure is fixed by adding the required linking library
(here d3d11) in the try_compile() function in OpenCVDetectDirectX.cmake file.
Also add a message if the first DirectX check fails.

* gapi: Fix compilation with mingw

These changes remove MSVC specific pragma directive. The compilation fails at
linking time due to absence of proper linking library. The required libraries
are added in corresponding CMakeLists.txt file.

* samples: Fix compilation with mingw

These changes remove MSVC specific pragma directive. The compilation fails at
linking time due to absence of proper linking library. The required libraries
are added in corresponding CMakeLists.txt file.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants