从文件夹名称中获取makefile参数

时间:2016-12-11 22:29:37

标签: c++ opencv makefile cmake

我正在尝试为OpenCV项目创建一个makefile。模板看起来像这样:

cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

有没有办法用文件夹的名称替换硬编码字符串DisplayImage?我的目标是将每个项目放在一个文件夹中,然后放入一个makefile。

2 个答案:

答案 0 :(得分:0)

您可以使用cmake方法:get_filename_component来提取父目录并将其放置在您需要的位置:

cmake_minimum_required(VERSION 2.8)

get_filename_component(PARENT_DIR ${CMAKE_SOURCE_DIR} NAME)
project( ${PARENT_DIR} )

find_package( OpenCV REQUIRED )

# If you want to change the executable name as well
add_executable( ${PARENT_DIR} DisplayImage.cpp )
target_link_libraries( ${PARENT_DIR} ${OpenCV_LIBS} )

答案 1 :(得分:0)

为每个子项目创建. ├── build ├── CMakeLists.txt ├── SubProjectA │   ├── CMakeLists.txt │   └── SubProjectA.cpp └── SubProjectB ├── CMakeLists.txt └── SubProjectB.cpp

cmake_minimum_required (VERSION 2.6)

get_filename_component(PROJECT_NAME "${CMAKE_CURRENT_SOURCE_DIR}" NAME)
project("${PROJECT_NAME}")

find_package( OpenCV REQUIRED )

set(PROJECT_INCLUDE_DIR "${PROJECT_SOURCE_DIR}")
set(PROJECT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(SUB_PROJECT_SRC "${PROJECT_SOURCE_DIR}/${PROJECT_NAME}.cpp")
add_executable("${PROJECT_NAME}" ${SUB_PROJECT_SRC})
target_link_libraries("${PROJECT_NAME}" ${OpenCV_LIBS})

具有以下内容:

CMakeLists.txt

在主add_subdirectory(SubProjectA) add_subdirectory(SubProjectB) 中包含子项目目录:

{{1}}

从当前目录获取项目名称的方法取自https://www.playframework.com/documentation/2.5.x/ScalaStream answer。