更改Cmake文件以从Source编译依赖项而不是使用FIND_PACKAGE

时间:2018-06-06 15:27:11

标签: c++ c build cmake zlib

我正在尝试使用Cmake编译名为xlsxio的库。 Cmake文件对Zlib(或miniz)使用FIND_PACKAGE命令。

相反,我想将Zlib源作为子目录放置并从源代码编译,但是我无法完全改变xlsxio Cmake文件以使其工作。

我有xlsxio repo,在此我放置了一个子目录zlib-1.2.11

原始的xlsxio Cmake文件是:

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
PROJECT(xlsxio)

# parameters
OPTION(BUILD_STATIC "Build static libraries" ON)
OPTION(BUILD_SHARED "Build shared libraries" ON)
OPTION(BUILD_TOOLS "Build tools" ON)
OPTION(BUILD_EXAMPLES "Build examples" ON)
OPTION(WITH_LIBZIP "Use libzip instead of Minizip" OFF)
OPTION(WITH_WIDE "Also build UTF-16 library (libxlsxio_readw)" OFF)

# conditions
IF(NOT BUILD_STATIC AND NOT BUILD_SHARED)
  MESSAGE(FATAL_ERROR "Cannot build with both BUILD_STATIC and BUILD_SHARED disabled")
ENDIF()

# dependancies
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH})
IF(WITH_LIBZIP)
FIND_PACKAGE(LibZip REQUIRED)
SET(ANYZIP_INCLUDE_DIRS ${LIBZIP_INCLUDE_DIRS})
SET(ANYZIP_LIBRARIES ${LIBZIP_LIBRARIES})
SET(ANYZIP_DEF USE_LIBZIP)
ELSE()
FIND_PACKAGE(Minizip REQUIRED)
SET(ANYZIP_INCLUDE_DIRS ${MINIZIP_INCLUDE_DIRS})
SET(ANYZIP_LIBRARIES ${MINIZIP_LIBRARIES})
SET(ANYZIP_DEF USE_MINIZIP)
ENDIF()
FIND_PACKAGE(EXPAT REQUIRED)
IF(WITH_WIDE)
FIND_LIBRARY(EXPATW_LIBRARY NAMES expatw)
ENDIF()
FIND_PACKAGE(Doxygen)

# build parameters
OPTION(BUILD_DOCUMENTATION "Create and install API documentation (requires Doxygen)" ${DOXYGEN_FOUND})
#SET(CMAKE_CXX_FLAGS "-g -Wall")
#ADD_SUBDIRECTORY(src)

IF(NOT WIN32 AND NOT APPLE)
  SET(THREADLIB "-pthread")
ENDIF()

INCLUDE_DIRECTORIES(include)
INCLUDE_DIRECTORIES(${ANYZIP_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${EXPAT_INCLUDE_DIRS})

# build definitions
SET(ALLTARGETS)
SET(LINKTYPES)
IF(BUILD_STATIC)
  LIST(APPEND LINKTYPES "STATIC")
ENDIF()
IF(BUILD_SHARED)
  LIST(APPEND LINKTYPES "SHARED")
ENDIF()

FOREACH(LINKTYPE ${LINKTYPES})
  ADD_LIBRARY(xlsxio_read_${LINKTYPE} ${LINKTYPE} lib/xlsxio_read.c lib/xlsxio_read_sharedstrings.c)
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "BUILD_XLSXIO;${ANYZIP_DEF}")
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_read)
  TARGET_INCLUDE_DIRECTORIES(xlsxio_read_${LINKTYPE} PRIVATE lib)
  TARGET_LINK_LIBRARIES(xlsxio_read_${LINKTYPE} ${ANYZIP_LIBRARIES} ${EXPAT_LIBRARIES})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_read_${LINKTYPE})

  ADD_LIBRARY(xlsxio_write_${LINKTYPE} ${LINKTYPE} lib/xlsxio_write.c)
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "BUILD_XLSXIO;${ANYZIP_DEF}")
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_write)
  TARGET_LINK_LIBRARIES(xlsxio_write_${LINKTYPE} ${ANYZIP_LIBRARIES} ${THREADLIB})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_write_${LINKTYPE})

  IF(WITH_WIDE)
    ADD_LIBRARY(xlsxio_readw_${LINKTYPE} ${LINKTYPE} lib/xlsxio_read.c lib/xlsxio_read_sharedstrings.c)
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE;BUILD_XLSXIO;${ANYZIP_DEF}")
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_readw)
    TARGET_INCLUDE_DIRECTORIES(xlsxio_readw_${LINKTYPE} PRIVATE lib)
    TARGET_LINK_LIBRARIES(xlsxio_readw_${LINKTYPE} ${ANYZIP_LIBRARIES} ${EXPATW_LIBRARY})
    SET(ALLTARGETS ${ALLTARGETS} xlsxio_readw_${LINKTYPE})

    #ADD_LIBRARY(xlsxio_writew_${LINKTYPE} ${LINKTYPE} lib/xlsxio_write.c)
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE;BUILD_XLSXIO;${ANYZIP_DEF}")
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_write_w)
    #TARGET_LINK_LIBRARIES(xlsxio_writew_${LINKTYPE} ${ANYZIP_LIBRARIES} ${THREADLIB})
    #SET(ALLTARGETS ${ALLTARGETS} xlsxio_writew_${LINKTYPE})
  ENDIF()

  SET(EXELINKTYPE ${LINKTYPE})
ENDFOREACH()

IF(BUILD_TOOLS)
  ADD_EXECUTABLE(xlsxio_xlsx2csv src/xlsxio_xlsx2csv.c)
  TARGET_LINK_LIBRARIES(xlsxio_xlsx2csv xlsxio_read_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_xlsx2csv)

  ADD_EXECUTABLE(xlsxio_csv2xlsx src/xlsxio_csv2xlsx.c)
  TARGET_LINK_LIBRARIES(xlsxio_csv2xlsx xlsxio_write_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_csv2xlsx)
ENDIF()

IF(BUILD_EXAMPLES)
  ADD_EXECUTABLE(example_xlsxio_write_getversion examples/example_xlsxio_write_getversion.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_write_getversion xlsxio_write_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_write_getversion)

  ADD_EXECUTABLE(example_xlsxio_write examples/example_xlsxio_write.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_write xlsxio_write_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_write)

  ADD_EXECUTABLE(example_xlsxio_read examples/example_xlsxio_read.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_read xlsxio_read_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_read)

  ADD_EXECUTABLE(example_xlsxio_read_advanced examples/example_xlsxio_read_advanced.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_read_advanced xlsxio_read_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_read_advanced)

  IF(WITH_WIDE)
    ADD_EXECUTABLE(example_xlsxio_readw examples/example_xlsxio_read.c)
    SET_TARGET_PROPERTIES(example_xlsxio_readw PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE")
    TARGET_LINK_LIBRARIES(example_xlsxio_readw xlsxio_readw_${EXELINKTYPE})
    SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_readw)
  ENDIF()
ENDIF()

IF(BUILD_DOCUMENTATION)
  IF(NOT DOXYGEN_FOUND)
    MESSAGE(FATAL_ERROR "Doxygen is needed to build the documentation.")
  ENDIF()
  ADD_CUSTOM_TARGET(doc ALL
    COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile
    #WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Generating API documentation with Doxygen"
    VERBATIM
  )
  INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc/man
    DESTINATION .
  )
  #INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc/html
  #  DESTINATION share/doc
  #)
ENDIF()

# install definitions
INSTALL(TARGETS ${ALLTARGETS}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)
INSTALL(DIRECTORY include/
  DESTINATION include 
  FILES_MATCHING PATTERN "xlsxio*.h"
)

我已将此更改为:

CMAKE_MINIMUM_REQUIRED(VERSION 3.0)
PROJECT(xlsxio)

# parameters
OPTION(BUILD_STATIC "Build static libraries" ON)
OPTION(BUILD_SHARED "Build shared libraries" OFF)
OPTION(BUILD_TOOLS "Build tools" ON)
OPTION(BUILD_EXAMPLES "Build examples" OFF)
OPTION(WITH_LIBZIP "Use libzip instead of Minizip" ON)
OPTION(WITH_WIDE "Also build UTF-16 library (libxlsxio_readw)" OFF)

# conditions
IF(NOT BUILD_STATIC AND NOT BUILD_SHARED)
  MESSAGE(FATAL_ERROR "Cannot build with both BUILD_STATIC and BUILD_SHARED disabled")
ENDIF()

# dependancies
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMake" ${CMAKE_MODULE_PATH})

SET(ANYZIP_INCLUDE_DIRS zlib-1.2.11)
SET(ANYZIP_LIBRARIES zlib-1.2.11)
SET(ANYZIP_DEF USE_LIBZIP)

FIND_PACKAGE(EXPAT REQUIRED)
IF(WITH_WIDE)
FIND_LIBRARY(EXPATW_LIBRARY NAMES expatw)
ENDIF()
FIND_PACKAGE(Doxygen)

# build parameters
OPTION(BUILD_DOCUMENTATION "Create and install API documentation (requires Doxygen)" ${DOXYGEN_FOUND})
#SET(CMAKE_CXX_FLAGS "-g -Wall")
#ADD_SUBDIRECTORY(src)

IF(NOT WIN32 AND NOT APPLE)
  SET(THREADLIB "-pthread")
ENDIF()

INCLUDE_DIRECTORIES(include)
INCLUDE_DIRECTORIES(${ANYZIP_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${EXPAT_INCLUDE_DIRS})

# build definitions
SET(ALLTARGETS)
SET(LINKTYPES)
IF(BUILD_STATIC)
  LIST(APPEND LINKTYPES "STATIC")
ENDIF()
IF(BUILD_SHARED)
  LIST(APPEND LINKTYPES "SHARED")
ENDIF()

FOREACH(LINKTYPE ${LINKTYPES})
  ADD_LIBRARY(xlsxio_read_${LINKTYPE} ${LINKTYPE} lib/xlsxio_read.c lib/xlsxio_read_sharedstrings.c)
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "BUILD_XLSXIO;${ANYZIP_DEF}")
  SET_TARGET_PROPERTIES(xlsxio_read_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_read)
  TARGET_INCLUDE_DIRECTORIES(xlsxio_read_${LINKTYPE} PRIVATE lib)
  add_subdirectory(zlib-1.2.11)
  TARGET_LINK_LIBRARIES(xlsxio_read_${LINKTYPE} zlib-1.2.11 ${EXPAT_LIBRARIES})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_read_${LINKTYPE})

  ADD_LIBRARY(xlsxio_write_${LINKTYPE} ${LINKTYPE} lib/xlsxio_write.c)
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "BUILD_XLSXIO;${ANYZIP_DEF}")
  SET_TARGET_PROPERTIES(xlsxio_write_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_write)
  add_subdirectory(zlib-1.2.11)
  TARGET_LINK_LIBRARIES(xlsxio_write_${LINKTYPE} ${ANYZIP_LIBRARIES} ${THREADLIB})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_write_${LINKTYPE})

  IF(WITH_WIDE)
    ADD_LIBRARY(xlsxio_readw_${LINKTYPE} ${LINKTYPE} lib/xlsxio_read.c lib/xlsxio_read_sharedstrings.c)
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE;BUILD_XLSXIO;${ANYZIP_DEF}")
    SET_TARGET_PROPERTIES(xlsxio_readw_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_readw)
    TARGET_INCLUDE_DIRECTORIES(xlsxio_readw_${LINKTYPE} PRIVATE lib)
    add_subdirectory(zlib-1.2.11)
    TARGET_LINK_LIBRARIES(xlsxio_readw_${LINKTYPE} ${ANYZIP_LIBRARIES} ${EXPATW_LIBRARY})
    SET(ALLTARGETS ${ALLTARGETS} xlsxio_readw_${LINKTYPE})

    #ADD_LIBRARY(xlsxio_writew_${LINKTYPE} ${LINKTYPE} lib/xlsxio_write.c)
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES DEFINE_SYMBOL "BUILD_XLSXIO_DLL")
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE;BUILD_XLSXIO;${ANYZIP_DEF}")
    #SET_TARGET_PROPERTIES(xlsxio_writew_${LINKTYPE} PROPERTIES OUTPUT_NAME xlsxio_write_w)
    #TARGET_LINK_LIBRARIES(xlsxio_writew_${LINKTYPE} ${ANYZIP_LIBRARIES} ${THREADLIB})
    #SET(ALLTARGETS ${ALLTARGETS} xlsxio_writew_${LINKTYPE})
  ENDIF()

  SET(EXELINKTYPE ${LINKTYPE})
ENDFOREACH()

IF(BUILD_TOOLS)
  ADD_EXECUTABLE(xlsxio_xlsx2csv src/xlsxio_xlsx2csv.c)
  TARGET_LINK_LIBRARIES(xlsxio_xlsx2csv xlsxio_read_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_xlsx2csv)

  ADD_EXECUTABLE(xlsxio_csv2xlsx src/xlsxio_csv2xlsx.c)
  TARGET_LINK_LIBRARIES(xlsxio_csv2xlsx xlsxio_write_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} xlsxio_csv2xlsx)
ENDIF()

IF(BUILD_EXAMPLES)
  ADD_EXECUTABLE(example_xlsxio_write_getversion examples/example_xlsxio_write_getversion.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_write_getversion xlsxio_write_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_write_getversion)

  ADD_EXECUTABLE(example_xlsxio_write examples/example_xlsxio_write.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_write xlsxio_write_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_write)

  ADD_EXECUTABLE(example_xlsxio_read examples/example_xlsxio_read.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_read xlsxio_read_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_read)

  ADD_EXECUTABLE(example_xlsxio_read_advanced examples/example_xlsxio_read_advanced.c)
  TARGET_LINK_LIBRARIES(example_xlsxio_read_advanced xlsxio_read_${EXELINKTYPE})
  SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_read_advanced)

  IF(WITH_WIDE)
    ADD_EXECUTABLE(example_xlsxio_readw examples/example_xlsxio_read.c)
    SET_TARGET_PROPERTIES(example_xlsxio_readw PROPERTIES COMPILE_DEFINITIONS "XML_UNICODE")
    TARGET_LINK_LIBRARIES(example_xlsxio_readw xlsxio_readw_${EXELINKTYPE})
    SET(ALLTARGETS ${ALLTARGETS} example_xlsxio_readw)
  ENDIF()
ENDIF()

IF(BUILD_DOCUMENTATION)
  IF(NOT DOXYGEN_FOUND)
    MESSAGE(FATAL_ERROR "Doxygen is needed to build the documentation.")
  ENDIF()
  ADD_CUSTOM_TARGET(doc ALL
    COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile
    #WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    COMMENT "Generating API documentation with Doxygen"
    VERBATIM
  )
  INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc/man
    DESTINATION .
  )
  #INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/doc/html
  #  DESTINATION share/doc
  #)
ENDIF()

# install definitions
INSTALL(TARGETS ${ALLTARGETS}
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin
)
INSTALL(DIRECTORY include/
  DESTINATION include 
  FILES_MATCHING PATTERN "xlsxio*.h"
)

Cmake抱怨:

CMake Error at CMakeLists.txt:78 (add_subdirectory):
      The binary directory

        /Users/x/Documents/repos/xlsx-test/xlsxio/cmake-build-debug/zlib-1.2.11

      is already used to build a source directory.  It cannot be used to build
      source directory

        /Users/x/Documents/repos/xlsx-test/xlsxio/zlib-1.2.11

      Specify a unique binary directory name.

我花了几个小时尝试了许多不同的方法而没有成功。任何帮助,将不胜感激。一个工作的Cmake文件将是天赐之物。如果有帮助,我可以使用更高的最低Cmake版本。

1 个答案:

答案 0 :(得分:2)

这就是我将zlib添加到我的项目中的方法,

WebElement elem = driver.findElement(By.xpath("//select[@class='initialized' and @id='comp_drpdwn'][@data-select-id]"));
Select select = new Select(elem);
select.selectByIndex(1);

在您的代码中,cmake_minimum_required (VERSION 3.0) project (simple) SET(SIMPLE_EXAMPLE main.cpp) ##Since your zlib folder is named as "zlib-1.2.11" set(ENV{PATH} "${CMAKE_SOURCE_DIR}/zlib-1.2.11") set(ZLIB_LIBRARY "${CMAKE_SOURCE_DIR}/zlib-1.2.11" ) add_subdirectory (zlib-1.2.11) ADD_EXECUTABLE (simple ${SIMPLE_EXAMPLE}) TARGET_LINK_LIBRARIES(simple zlib) 根据{{​​1}}或TARGET_LINK_LIBRARIES(xlsxio_read_${LINKTYPE} zlib-1.2.11 ${EXPAT_LIBRARIES})取决于zlib-1.2.11是基于静态库还是动态库。{/ p>

zlib应该做一次。最好在项目申报开始时。 这将在内部调用zlibstatic add_subdirectory (zlib-1.2.11)内的CMakeLists.txt文件夹并构建可在zlib-1.2.11中链接的库 希望能帮助到你。