将这些变量包含在cmake中

时间:2015-02-25 05:02:14

标签: cmake

我使用的这个科学应用程序使用cmake来构建它的程序。附件是CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

set(PROJECT_NAME myproject)
project(${PROJECT_NAME})

# Set verbose output while testing CMake
#set(CMAKE_VERBOSE_MAKEFILE 1)

# Set CMake behavior
cmake_policy(SET CMP0004 OLD)

# Get DOLFIN configuration data (DOLFINConfig.cmake must be in
# DOLFIN_CMAKE_CONFIG_PATH)
find_package(DOLFIN)

# Need to get VTK config because VTK uses advanced VTK features which
# mean it's not enough to just link to the DOLFIN target. See
# http://www.vtk.org/pipermail/vtk-developers/2013-October/014402.html
find_package(VTK HINTS ${VTK_DIR} $ENV{VTK_DIR} NO_MODULE QUIET)

# Default build type (can be overridden by user)
if (NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
    "Choose the type of build, options are: Debug MinSizeRel Release RelWithDebInfo." FORCE)
endif()

# Compiler definitions
add_definitions(${DOLFIN_CXX_DEFINITIONS})

# Compiler flags
set(CMAKE_CXX_FLAGS "${DOLFIN_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")

# Include directories
include_directories(${DOLFIN_INCLUDE_DIRS})
include_directories(SYSTEM ${DOLFIN_3RD_PARTY_INCLUDE_DIRS})

# Executable
add_executable(${PROJECT_NAME} main.cpp)

# Target libraries
target_link_libraries(${PROJECT_NAME} ${DOLFIN_LIBRARIES})

但是,我需要包含一些不包含在DOLFIN中的额外编译器标志和库。我需要加入名为PAPI的这个包。在我的系统上,路径/环境是:

PAPI_LIBDIR=/project/cacds/apps/papi/5.4.0/lib
PAPI_INCLUDE=/project/cacds/apps/papi/5.4.0/include
PAPI_FLAG=-lpapi

通常我会做这样的事情:

gcc -O3 -o -I myproject $(PAPI_LIBDIR) main.c -L $(PAPI_INCLUDE) $(PAPI_FLAG)

但我还需要额外的DOLFIN库和第三方软件包。请原谅我这是一个愚蠢或简单的问题,但我如何修改上面的CMakeLists.txt?我是cmake的新手,所以任何帮助表示赞赏,谢谢

1 个答案:

答案 0 :(得分:4)

最干净的解决方案是创建FindPAPI.cmake。因此,将此文件添加到项目中cmake/文件夹:

<强> FindPAPI.cmake

# Try to find PAPI headers and libraries.
#
# Usage of this module as follows:
#
#     find_package(PAPI)
#
# Variables used by this module, they can change the default behaviour and need
# to be set before calling find_package:
#
#  PAPI_PREFIX         Set this variable to the root installation of
#                      libpapi if the module has problems finding the
#                      proper installation path.
#
# Variables defined by this module:
#
#  PAPI_FOUND              System has PAPI libraries and headers
#  PAPI_LIBRARIES          The PAPI library
#  PAPI_INCLUDE_DIRS       The location of PAPI headers

find_path(PAPI_PREFIX
    NAMES include/papi.h
)

find_library(PAPI_LIBRARIES
    # Pick the static library first for easier run-time linking.
    NAMES libpapi.a papi
    HINTS ${PAPI_PREFIX}/lib ${HILTIDEPS}/lib
)

find_path(PAPI_INCLUDE_DIRS
    NAMES papi.h
    HINTS ${PAPI_PREFIX}/include ${HILTIDEPS}/include
)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PAPI DEFAULT_MSG
    PAPI_LIBRARIES
    PAPI_INCLUDE_DIRS
)

mark_as_advanced(
    PAPI_PREFIX_DIRS
    PAPI_LIBRARIES
    PAPI_INCLUDE_DIRS
)

然后,修改您的CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

set(PROJECT_NAME myproject)
project(${PROJECT_NAME})

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

find_package(PAPI REQUIRED)

...

# Include directories
include_directories(${DOLFIN_INCLUDE_DIRS})
include_directories(SYSTEM ${DOLFIN_3RD_PARTY_INCLUDE_DIRS})
include_directories(${PAPI_INCLUDE_DIRS})

# Executable
add_executable(${PROJECT_NAME} main.cpp)

# Target libraries
target_link_libraries(${PROJECT_NAME} ${PAPI_LIBRARIES} ${DOLFIN_LIBRARIES})

我希望它能解决它! ;)