CMAKE:如何包含和链接替代库?

时间:2016-03-29 02:07:11

标签: boost sqlite cmake

我的项目需要四个库,包括MPIBOOSTVTKSQLITE3。由于BOOSTSQLITE3太旧,我安装了两个库的新版本,旧版本未卸载:

BOOST    # alternative BOOST
  |___include
  |___lib
  |
SQLITE   # alternative SQLITE
  |___bin
  |___include
  |___lib
  |___share
  |
Mypoject
  |___src
  |    |___CMakeLists
  |    |___.cpp and .h file
  |
  |___CMakeLists

我使用FindSQLite3.cmake找到替代SQLite3:

# Look for the header file.
FIND_PATH(SQLITE3_INCLUDE_DIR sqlite3.h /export/home/hh/hh/sqlite/include)

# Look for the library.
FIND_LIBRARY(SQLITE3_LIBRARY sqlite3 /export/home/hh/hh/sqlite/lib)

# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE.
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR)

# Copy the results to the output variables.
IF(SQLITE3_FOUND)
    SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY})
    SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR})
ELSE(SQLITE3_FOUND)
    SET(SQLITE3_LIBRARIES)
    SET(SQLITE3_INCLUDE_DIRS)
ENDIF(SQLITE3_FOUND)

MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)

我正在尝试通过以下行链接替代库:

# CMake version
CMAKE_MINIMUM_REQUIRED (VERSION 2.6)

SET(VERSION 0.1)
SET(SOVERSION 1)

# searching ENV{PATH} to find mpicc and
# store the path of mpicc to MPI_INTEL_C
FIND_PATH(MPI_INTEL_C mpicc $ENV{PATH})

# searching ENV{PATH} to find mpicxx and
# store the path of mpicc to MPI_INTEL_CXX
FIND_PATH(MPI_INTEL_CXX mpicxx $ENV{PATH})

IF(MPI_INTEL_C AND MPI_INTEL_CXX)
    MESSAGE(STATUS "Intel MPI compiler is used.")

    # set C++ complier as mpicxx, and set C complier as mpicc   
    SET(CMAKE_CXX_COMPILER mpicxx)
    SET(CMAKE_C_COMPILER mpicc)
ENDIF(MPI_INTEL_C AND MPI_INTEL_CXX)

find_package(VTK REQUIRED NO_MODULE)
IF(VTK_FOUND)
    MESSAGE(STATUS "VTK IS USED")
    include(${VTK_USE_FILE})
ELSE()
    MESSAGE("VTK IS NOT FOUND")
ENDIF(VTK_FOUND)

list(APPEND CMAKE_MODULE_PATH "/export/home/hh/hh/iRoot-make/cmake")


FIND_PACKAGE(SQLite3 REQUIRED)
MESSAGE(STATUS "${SQLITE3_INCLUDE_DIRS}")
MESSAGE(STATUS "${SQLITE3_LIBRARY}")

# try to link boost--error
INCLUDE_DIRECTORIES(/export/home/hh/hh/boost/include)
LINK_DIRECTORIES(/export/home/hh/hh/boost/lib)

find_package(MPI REQUIRED)
IF(MPI_FOUND)
    MESSAGE(STATUS "MPI IS USED")
    INCLUDE_DIRECTORIES(${MPI_INCLUDE_PATH})
ELSE()
    MESSAGE("MPI IS NOT FOUND")
ENDIF(MPI_FOUND)

aux_source_directory(. DIR_SRCS)
ADD_EXECUTABLE(test ${DIR_SRCS})

#TARGET_LINK_LIBRARIES(iRoot)

# set location of binary generated by the program, 
# here PROJECT_BINARY_DIR = DIR OF build
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

# set location of lib generated by the program
SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

install(TARGETS test DESTINATION bin)

CMake无法找到替代SQLite3,并且可以找到旧版本:

-- /usr/include
-- /usr/lib64/libsqlite3.so

但新版本位于:

/export/home/hh/hh/sqlite/include
/export/home/hh/hh/sqlite/lib/libsqlite3.so

我是CMAKE的新手,请帮助我,提前谢谢你!

2 个答案:

答案 0 :(得分:0)

您应该在缓存中更改适当的值(builddir / CMakeCache.txt),而不是在CMakeLists.txt中对这些设置进行硬编码。你可以这样做

  • 手动

  • cmake -D VARNAME=value .

  • 使用cmake GUI app

答案 1 :(得分:0)

我将以使用boost作为示例来解释这一点。这就是我在设置中完成它的方法。

我们假设安装了较新的升级版本:

/export/home/hh/hh/Boost/boost-1_60/boost/include/
/export/home/hh/hh/Boost/lib

现在在CMakeLists.txt中将这些路径添加到CMAKE_INCLUDE_PATHCMAKE_LIBRARY_PATH

LIST (APPEND CMAKE_INCLUDE_PATH "/export/home/hh/hh/Boost/boost-1_60")
LIST (APPEND CMAKE_LIBRARY_PATH "/export/home/hh/hh/Boost/lib)

然后调用FindPackage提及所需的最低版本以及可选的内置于.so中的库组件:

FIND_PACKAGE (Boost 1.60 COMPONENTS "program_options" "log" "system" "filesystem" "thread" "python" REQUIRED)

这也适用于其他图书馆。

<强>更新 添加到自定义cmake模块的CMakeLists.txt路径,例如FindSQLite3.cmake所在的路径:

LIST (APPEND CMAKE_MODULE_PATH "${MAINFOLDER}/tools/share/cmake")

然后致电:FIND_PACKAGE(SQLite3 REQUIRED)

同时从/export/home/hh/hh/sqlite/include移除/export/home/hh/hh/sqlite/libFindSQLite3.cmake,但添加类似于提升方式。