Python和C++11用于Swig:如何启用C++11

3
我将使用swig来将共享库与Python一起使用。我对C++不是很熟练,但是我已经用一个简单的对象(在C++中)进行了第一次测试,并且效果很好。
现在我尝试使其能够运行在已经在Linux(和Windows)下编译的大项目中。但我无法让它正常工作。
以下是我尝试过的内容:
我的Swig的CMakeLists:
project(elecswig)

include_directories(${COREALPI_DIR}/include)
include_directories(/usr/include/c++/4.9)

find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
if (NOT MSVC)
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif()

find_package(PythonLibs)
include_directories(${PYTHON_INCLUDE_PATH})

set_source_files_properties(src/elec.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties(src/elec.i PROPERTIES SWIG_FLAGS "-includeall")

link_directories(${COREALPI_LIBRARY})

swig_add_module(elecswig python src/elec.i)

swig_link_libraries(elecswig
    ${PYTHON_LIBRARIES}
    elec
)

我需要为调用带有命名空间的 .h 的 Swig 的 elec.i 进行翻译。

#define __linux__ 1
#define __GNUC__ 4

%module elecswig
%{
    #include "ca/elec/model/modelvoltagedrop.h"
%}

%include "ca/elec/model/modelvoltagedrop.h"

当我尝试编译以获取我的Python库时,出现以下错误:

/usr/include/c++/4.9/bits/c++0x_warning.h:32: Error: CPP #error "This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.". Use the -cpperraswarn option to continue swig processing.
/usr/include/c++/4.9/cstring:41: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/string:38: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/stringfwd.h:39: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/memoryfwd.h:48: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/stl_algobase.h:59: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/functexcept.h:39: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/cpp_type_traits.h:37: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/ext/type_traits.h:34: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/move.h:33: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/concept_check.h:35: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/stl_iterator_base_types.h:64: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/cwchar:41: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/allocator.h:46: Error: Unable to find 'bits/c++allocator.h'
/usr/include/c++/4.9/bits/localefwd.h:39: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/bits/localefwd.h:40: Error: Unable to find 'bits/c++locale.h'
/usr/include/c++/4.9/iosfwd:38: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/cctype:41: Error: Unable to find 'bits/c++config.h'
[...]
/usr/include/c++/4.9/cstdlib:41: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/utility:68: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/ctime:41: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/ctime:42: Error: Unable to find 'time.h'
/usr/include/c++/4.9/iomanip:38: Error: Unable to find 'bits/c++config.h'
/usr/include/c++/4.9/numeric:60: Error: Unable to find 'bits/c++config.h'
elecswig/CMakeFiles/_elecswig.dir/build.make:53: recipe for target 'elecswig/elecPYTHON_wrap.cxx' failed
make[2]: *** [elecswig/elecPYTHON_wrap.cxx] Error 1
CMakeFiles/Makefile2:75: recipe for target 'elecswig/CMakeFiles/_elecswig.dir/all' failed
make[1]: *** [elecswig/CMakeFiles/_elecswig.dir/all] Error 2
Makefile:75: recipe for target 'all' failed
make: *** [all] Error 2

他似乎找不到很多.h文件,但我主要遇到了C++11错误。

我尝试在CMake中设置标志,但似乎没有任何变化。

如果您能帮助我,那么请?

如果需要,我可以提供更多信息。

谢谢


如果你需要明确地编写 include_directories(/usr/include/c++/4.9),那么你的编译器/cmake设置可能存在问题。 - Flexo
我无法重现你的问题,我的CMake文件几乎与那个被指出的更改相同,而且它可以正常工作。考虑到其他错误,我认为你的编译器本身可能以某种奇怪的方式出现了故障。 - Flexo
奇怪的是,我可以在不使用swig的情况下编译这个项目(使用c++11标志)...那么为什么我使用swig时他告诉我呢?我在另一台机器上尝试了一下,结果出现了同样的错误。 - Algorys
制作一个完整的最小示例以重现问题,然后其他人可以重现该问题。我无法从您发布的内容中重现您的问题。 - Flexo
2个回答

2
经过数小时的测试,我找到了它无法正常工作的原因。在 CMakeLists 文件中:
set_source_files_properties(src/elec.i PROPERTIES SWIG_FLAGS "-includeall")

无论使用哪种编译器,请包含所有库和依赖项。如果你同时有2个版本的libstdc++和gcc / g ++,它似乎无法找到好的库来链接/使用。

如果我将其移除,CMake就可以胜任了。我不再有“无法找到”或“c++11”问题。

我必须使用g++-5才能使其工作(4.9似乎缺少一些c++11特性),并添加正确的命名空间和包含文件。

现在我可以在Python中访问我的共享库。


这个答案没有解释如何正确地为SWiG模块启用C++11支持。使用“-std=c++11”是一个不适用于所有情况的解决方法。cmake有一种通用机制来要求C++11,通过设置目标属性。鉴于问题的标题,一个好的答案应该指定如何通用地解决这个问题。 - Erik Schnetter

0
记录一下,为了回答这篇文章标题中的问题(与原始作者实际遇到的问题不同):
请使用此声明,而不是设置CMAKE_CXX_FLAGS
set_property(TARGET ${SWIG_MODULE_elecswig_REAL_NAME} PROPERTY CXX_STANDARD_REQUIRED 11)

我已经尝试了你的解决方案,但是在另一个项目中,cmake仍然会警告... ${SWIG_MODULE_elecswig_REAL_NAME} 对我来说似乎是空的。 - Algorys
你称之为 SWIG_MODULE_elecswig_REAL_NAME 是什么? - Algorys

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接