Keith Sloan
Keith Sloan

Reputation: 145

How do I get CMake to compile a source file containing Boost Local Functions

I am trying to build/compile some Boost Local_functions.

I have my CMakeLists.txt file that built a library for C++ functions that I wish to access from Python, but I also need/want some BOOST_LOCAL_FUNCTIONS defined but when I try and add them to my build I get the following error

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: note: previous declaration as ‘boost::scope_exit::detail::undeclared boost_local_function_auxXargsX’ BOOST_LOCAL_FUNCTION_AUX_DECL_ARGS_VAR;

My CMakeLists.txt file is

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 contains ( Its just a simple test at the moment )

#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)

Upvotes: 0

Views: 139

Answers (1)

Praetorian
Praetorian

Reputation: 109289

Your function isn't local, it's defined at namespace scope. If that's what you need, use a regular function instead. BOOST_LOCAL_FUNCTION is meant for declaring functions that are local to the scope of another function.

Modify the code within MyFC-G4.cc to the following and it should compile

#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
}

Upvotes: 1

Related Questions