【问题标题】:How do I get CMake to compile a source file containing Boost Local Functions如何让 CMake 编译包含 Boost Local Functions 的源文件
【发布时间】:2019-06-14 05:36:42
【问题描述】:

我正在尝试构建/编译一些 Boost Local_functions。

我有我的 CMakeLists.txt 文件,该文件为我希望从 Python 访问的 C++ 函数构建了一个库,但我还需要/想要定义一些 BOOST_LOCAL_FUNCTIONS 但是当我尝试将它们添加到我的构建中时,我​​收到以下错误

In file included from /usr/include/boost/preprocessor   /seq/cat.hpp:18:0,
             from /usr/include/boost/local_function/aux_/symbol.hpp:12,
             from /usr/include/boost/local_function/aux_/macro/code_/result.hpp:11,
             from /usr/include/boost/local_function/aux_/macro/decl.hpp:11,
             from /usr/include/boost/local_function.hpp:13,
             from /home/keith/FreeCAD_Geant4/myg4py/MyFC-G4.cc:1:
/home/keith/FreeCAD_Geant4/myg4py/MyFC-G4.cc:3:5: error:    conflicting declaration ‘boost::scope_exit::detail::declared<> boost_local_function_auxXargsX’

int BOOST_LOCAL_FUNCTION(int x, int y) { // 本地函数。 ^ /usr/include/boost/local_function/aux_/macro/decl.hpp:53:9:注意:先前的声明为“boost::scope_exit::detail::undeclared boost_local_function_auxXargsX” BOOST_LOCAL_FUNCTION_AUX_DECL_ARGS_VAR;

我的 CMakeLists.txt 文件是

cmake_minimum_required(VERSION 3.0)

#set(CMAKE_CXX_STANDARD 14)
find_package(PythonLibs 2 REQUIRED)
#the version of libboost_python
#See https://gitlab.kitware.com/cmake/cmake/issues/16391
if(UNIX)
   set(BOOST_PYTHONLIB python-py36)
else()
#set(BOOST_PYTHONLIB python3)
set(BOOST_PYTHONLIB python2)
endif()

find_package(Geant4 REQUIRED)
find_package(Boost COMPONENTS system python)

include_directories(${Geant4_INCLUDE_DIRS}    ${CMAKE_CURRENT_SOURCE_DIR}/include)

link_directories (${GEANT4_LIBRARY_DIR} ${Boost_LIBRARY_DIRS})

add_library(myg4py SHARED
     myg4py.cc
)

add_library(MyFC-G4 SHARED
      MyFC-G4.cc
)

target_include_directories(myg4py PUBLIC
${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS} ${Geant4_INCLUDE_DIRS}  ${CMAKE_CURRENT_SOURCE_DIR}/include )

target_link_libraries(myg4py
    ${Boost_LIBRARIES} ${PYTHON_LIBRARIES} ${Geant4_LIBRARIES})

MyFC-G4.cc 包含(目前只是一个简单的测试)

#include <boost/local_function.hpp> // This library header.

int BOOST_LOCAL_FUNCTION(int x, int y) { // Local function.
      return x + y;
} BOOST_LOCAL_FUNCTION_NAME(add)

【问题讨论】:

    标签: python c++ python-2.7 boost


    【解决方案1】:

    您的函数不是本地,它是在命名空间范围内定义的。如果这是您需要的,请改用常规函数。 BOOST_LOCAL_FUNCTION 用于声明另一个函数范围内的本地函数。

    修改MyFC-G4.cc中的代码如下,应该可以编译

    #include <boost/local_function.hpp> // This library header.
    
    void foo()
    {
      int BOOST_LOCAL_FUNCTION(int x, int y) { // Local function.
            return x + y;
      } BOOST_LOCAL_FUNCTION_NAME(add)
    
      // You can only make use of add within foo
    }
    

    【讨论】:

    • 好吧,也许我想要的不是本地函数。我想从 python 调用一些 C/C++ 代码,但不想将它们写为 C++ 类。即我想要一些执行某些计算的代码返回一个创建的(新的)C++ 对象。我会更改相关提供的类,但它们不是我的代码,更改它们会出现问题。在一种情况下,我想从 python 调用的构造函数有一个枚举,我不知道如何从 python 2.7 传递一个枚举,似乎需要 python 3.4 看看我的其他问题之一
    • 好的,我想我已经解决了 - 谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多