通过ExternalProject_Add使用pybind11来实现CM项目的智能方法

时间:2017-10-31 04:07:39

标签: cmake pybind11

我正在使用pybind11CMake 3.9.4编写python模块。 为方便起见,我希望在pybind11中使用ExternalProject_Add下载CMakeLists.txt源文件。

当我运行cmake .时,它不会下载pybind11源文件,并会抛出错误。

CMake Error at CMakeLists.txt:21 (add_subdirectory):
  The source directory
    /Users/me/foo/pybind11_external-prefix/src/pybind11_external
  does not contain a CMakeLists.txt file.

CMake Error at CMakeLists.txt:22 (pybind11_add_module):
  Unknown CMake command "pybind11_add_module".

有一种解决方法:

  1. 注释掉CMakeLists.txt中的最后3行
  2. 运行cmake .
  3. 运行make(然后,它会下载pybind11源文件)
  4. 恢复CMakeLists.txt
  5. 中的最后3行
  6. 运行cmake .
  7. 运行make
  8. 然而,这并不聪明......有没有办法使用pybind11下载ExternalProject_Add而不评论线路并恢复它们(并且没有运行cmake和{{1两次)?

    /Users/me/foo/CMakeLists.txt

    make

    /Users/me/foo/foo.hpp

    cmake_minimum_required(VERSION 3.8)
    project(foo)
    set(CMAKE_CXX_STANDARD 14)
    
    include(ExternalProject)
    ExternalProject_Add(
            pybind11_external
            GIT_REPOSITORY https://github.com/pybind/pybind11.git
            GIT_TAG v2.2.1
            CONFIGURE_COMMAND ""
            BUILD_COMMAND ""
            INSTALL_COMMAND ""
    )
    set(PYBIND11_CPP_STANDARD -std=c++14)
    ExternalProject_Get_Property(pybind11_external source_dir)
    include_directories(${source_dir}/include)
    
    add_subdirectory(${source_dir})             # comment out, then restore this line
    pybind11_add_module(foo SHARED foo.cpp)     # comment out, then restore this line
    add_dependencies(foo pybind11_external)     # comment out, then restore this line
    

    /Users/me/foo/foo.cpp

    #ifndef FOO_LIBRARY_H
    #define FOO_LIBRARY_H
    
    #include<pybind11/pybind11.h>
    
    int add(int i, int j);
    
    #endif
    

0 个答案:

没有答案