Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit 8c6e11b

Browse files
committed
samples: link with pthread
1 parent 5359791 commit 8c6e11b

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

‎samples/CMakeLists.txt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
if(POLICY CMP0003)
2+
cmake_policy(SET CMP0003 NEW)
3+
endif()
4+
15
# Utility function: adds sample executable target with name "example_<group>_<file_name>"
26
# Usage:
37
# ocv_define_sample(<output target> <relative filename> <group>)
48
function(ocv_define_sample out_target source sub)
59
get_filename_component(name "${source}" NAME_WE)
610
set(the_target "example_${sub}_${name}")
711
add_executable(${the_target} "${source}")
12+
if(TARGET Threads::Threads AND NOT OPENCV_EXAMPLES_DISABLE_THREADS)
13+
target_link_libraries(${the_target} Threads::Threads)
14+
endif()
815
set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
916
if(ENABLE_SOLUTION_FOLDERS)
1017
set_target_properties(${the_target} PROPERTIES FOLDER "samples/${sub}")
@@ -43,6 +50,10 @@ function(ocv_install_example_src relpath)
4350
endif()
4451
endfunction()
4552

53+
if((TARGET Threads::Threads OR HAVE_PTHREAD OR MSVC OR APPLE) AND NOT OPENCV_EXAMPLES_DISABLE_THREADS)
54+
add_definitions(-DHAVE_THREADS=1)
55+
endif()
56+
4657
add_subdirectory(cpp)
4758
add_subdirectory(java/tutorial_code)
4859
add_subdirectory(dnn)
@@ -127,6 +138,17 @@ if(MSVC)
127138
endif()
128139
endif()
129140

141+
if(OPENCV_EXAMPLES_DISABLE_THREADS)
142+
# nothing
143+
elseif(MSVC OR APPLE)
144+
set(HAVE_THREADS 1)
145+
else()
146+
find_package(Threads)
147+
endif()
148+
if((TARGET Threads::Threads OR HAVE_THREADS) AND NOT OPENCV_EXAMPLES_DISABLE_THREADS)
149+
add_definitions(-DHAVE_THREADS=1)
150+
endif()
151+
130152
add_definitions(-DDISABLE_OPENCV_24_COMPATIBILITY=1) # Avoid C-like legacy API
131153

132154
add_subdirectory(cpp)

‎samples/dnn/object_detection.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#include <iostream>
2+
3+
#if defined(_MSC_VER) || defined(__APPLE__) || defined HAVE_THREADS
4+
15
#include <fstream>
26
#include <sstream>
37
#include <thread>
@@ -379,3 +383,11 @@ void callback(int pos, void*)
379383
{
380384
confThreshold = pos * 0.01f;
381385
}
386+
387+
#else
388+
int main(int argc, char** argv)
389+
{
390+
std::cerr << "ERROR: This sample has been compiled without threads support" << std::endl;
391+
return 1;
392+
}
393+
#endif

0 commit comments

Comments
 (0)