Pedro Roios
Pedro Roios

Reputation: 45

Share files between multiple groups under CMake

I`m trying to create a CMake setup for a project with the following structure:

workshop
|    CMakeLists.txt
|    utilities
|    |    utilities.h
|    |    utilities.cpp
|    |    CMakeLists.txt
|    week_1
|    |    week_1.h
|    |    week_1.cpp
|    |    main.cpp
|    |    CMakeLists.txt
|    week_2
|    |    main.h
|    |    week_2.cpp
|    |    main.cpp
|    |    CMakeLists.txt

Everything depends on OpenCV and the programs in week_X depend also on utilities.

I was able to do everything except the connection with utilities.

My trial:

File workshop\CMakeLists.txt

cmake_minimum_required(VERSION 2.8.12)
PROJECT(workshop)

SET(OpenCV_DIR OPENCV_DIR)
find_package( OpenCV REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS})
add_subdirectory(${CMAKE_SOURCE_DIR}/utilities)
add_subdirectory(${CMAKE_SOURCE_DIR}/week_1)
add_subdirectory(${CMAKE_SOURCE_DIR}/week_2)

File week_1\CMakeLists.txt (week_2\CMakeLists.txt is identical just changing 1 by 2)

cmake_minimum_required(VERSION 2.8.12)

include_directories( ${OpenCV_INCLUDE_DIRS})
set(SRCFILES week_1.cpp week_1.h main.cpp)
source_group(week_1 FILES ${SRCFILES})
add_executable(week_1 ${SRCFILES})
target_link_libraries(week_1 ${OpenCV_LIBS})

All is okay until the utilities. How would be the utilities\CMakeLists.txt?

Upvotes: 0

Views: 97

Answers (1)

mattdibi
mattdibi

Reputation: 811

I think you want utilities to be a library.

In the utilities CMakeLists.txt

# This will create libutilities.a
add_library(utilities
   utilities.cpp
)

target_include_directories( utilities PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}
    # Third party libs
    ${OpenCV_INCLUDE_DIRS}

)

target_link_libraries(utilities
    ${OpenCV_LIBRARIES}
)

In week_X you'll use it as a library like so:

include_directories(
    ${OpenCV_INCLUDE_DIRS} 
    ${CMAKE_SOURCE_DIR}/utilities # Include utilities header
)

set(SRCFILES week_1.cpp week_1.h main.cpp)
source_group(week_1 FILES ${SRCFILES})
add_executable(week_1 ${SRCFILES})

target_link_libraries(
   ${OpenCV_LIBS}
   utilities # Link to libutilites.a
)

Upvotes: 3

Related Questions