文件结构并在Cmake中生成可执行文件

时间:2018-03-07 00:44:40

标签: opencv cmake

我有一个像这样的Cmake.txt文件

project(seat_analysis)
cmake_minimum_required(VERSION 2.8)
find_package( OpenCV REQUIRED )
add_executable( main main.cpp )
add_executable(canny canny.cpp )
add_executable (threshold thresholding.cpp )
target_link_libraries( main ${OpenCV_LIBS} )
target_link_libraries( canny ${OpenCV_LIBS} )
target_link_libraries( threshold ${OpenCV_LIBS} )

最后六行是多余的,首先是如何将库添加到项目中以便所有可执行文件都可以使用它,其次如何减少工作量或单独添加每个可执行文件。

2 个答案:

答案 0 :(得分:2)

使用这段代码,您可以打印检测到的所有文件,通过cpp扩展名进行过滤,并创建二进制文件而不用手动放置它们

# The following loop create an executable from each source file
# -----------------------------------------------------------------------------
file(GLOB ALL_FILES_CURRENT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/" "*.cpp")

foreach(CURRENT_FILE ${ALL_FILES_CURRENT_PATH})
    get_filename_component(FILENAME ${CURRENT_FILE} NAME_WE)
    MESSAGE("Compiling file: ${FILENAME}")
    add_executable(${FILENAME} ${FILENAME}.cpp)
    target_link_libraries( ${FILENAME} ${OpenCV_LIBS} )
endforeach()

答案 1 :(得分:1)

您可以使用循环:

# The following loop assumes the name of executable is equal to the
# name of the source file: mybin.cpp -> mybin
foreach(app
    main
    canny
    threshold
)
    add_executable (${app} ${app}.cpp)
    target_link_libraries( ${app} ${OpenCV_LIBS} )
endforeach()