Shiva Reddy
Shiva Reddy

Reputation: 37

file structure and making executable in Cmake

I have a Cmake.txt file like this

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} )

The last six lines are kind of redundant, first how to add the library to the project so that all executables can use it and secondly how can I reduce the work or adding each executable separately.

Upvotes: 1

Views: 41

Answers (2)

vgonisanz
vgonisanz

Reputation: 11930

With this piece of code you can print all files detected, filtered by cpp extension and create the binaries WITHOUT put them manually

# 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()

Upvotes: 2

Th. Thielemann
Th. Thielemann

Reputation: 2824

You can use a loop:

# 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()

Upvotes: 1

Related Questions