如何让 CMake 编译包含 Boost Local Functions 的源文件



我正在尝试构建/编译一些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) {//Local function. ^/usr/include/boost/local_function/aux_/macro/decl.hpp:53:9:注意:以前的声明为"boost::scope_exit::d etail::undeclare 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)

你的函数不是本地的,它是在命名空间范围内定义的。如果这是您需要的,请改用常规函数。 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
}

最新更新