简体   繁体   English

使用带有 go 的 swig 包装的 c++ 库

[英]Using a swig wrapped c++ library with go

I have managed to successfully wrap a large C++ library with SWIG .我已经成功地用SWIG包装了一个大型C++ I published themodule and can install it okay, but when I go to build it I get a ton of undefined reference errors to the functions contained within the generated c file.我发布了该模块并且可以安装它,但是当我 go 构建它时,我收到大量undefined reference错误,指向生成的c文件中包含的函数。

I placed the generated so lib file in my system lib directory, and placed the header files from the library into my system include directory, but this has not solved the problem.我把生成的so lib文件放在我的系统lib目录下,把库中的header文件放到我的系统include目录下,但是并没有解决问题。 I am guessing that I need a header file for wrapped c file as well so the interpreter can know what's in the so file, but given that SWIG didn't generate one, I don't know if I am missing anything else.我猜我还需要一个 header 文件来包装c文件,这样解释器就可以知道so文件中有什么,但是考虑到SWIG没有生成一个文件,我不知道我是否遗漏了其他任何东西。 I'm going to try to write my own header file to see if that resolves the errors, but even if it does, shouldn't that be something that SWIG would do automatically?我将尝试编写自己的 header 文件以查看是否可以解决错误,但即使可以, SWIG不应该自动执行此操作吗?

My source files are below我的源文件如下

/** file: golibraw.i **/
%module librawgo
%{
#define LIBRAW_LIBRARY_BUILD
/* Put headers and other declarations here */
#include "libraw/libraw.h"
#include "libraw/libraw_alloc.h"
#include "libraw/libraw_const.h"
#include "libraw/libraw_datastream.h"
#include "libraw/libraw_internal.h"
#include "libraw/libraw_types.h"
#include "libraw/libraw_version.h"
%}

%include "libraw/libraw.h"
%include "libraw/libraw_alloc.h"
%include "libraw/libraw_const.h"
%include "libraw/libraw_datastream.h"
%include "libraw/libraw_internal.h"
%include "libraw/libraw_types.h"
%include "libraw/libraw_version.h"
cmake_minimum_required(VERSION 3.14)

if ( ${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR} )
    message( FATAL_ERROR "In-source builds not allowed. Please make a new directory and run CMake from there. You may need to remove CMakeCache.txt." )
endif()

find_package(SWIG 4.0 COMPONENTS python OPTIONAL_COMPONENTS go)
if(SWIG_FOUND)
  message("SWIG found: ${SWIG_EXECUTABLE}")
  include (UseSWIG)
  if(NOT SWIG_go_FOUND)
    message(FATAL_ERROR "SWIG go bindings cannot be generated")
  endif()
else()
    message(FATAL_ERROR "SWIG NOT FOUND")
endif()

set(PROJECT_NAME librawgo)
project(${PROJECT_NAME})

SET (CMAKE_LIBRARY_OUTPUT_DIRECTORY
        ${CMAKE_SOURCE_DIR}/librawgo
        CACHE PATH
        "Single Directory for all"
    )

find_program( CLANG_TIDY NAMES clang-tidy)



set(${PROJECT_NAME}_sources ${CMAKE_SOURCE_DIR}/src/librawgo.i )


swig_add_library(${PROJECT_NAME} TYPE SHARED LANGUAGE go SOURCES ${${PROJECT_NAME}_sources})


if(CLANG_TIDY)
set_property(
    TARGET ${PROJECT_NAME}
    PROPERTY CXX_CLANG_TIDY "${CLANG_TIDY}")
endif()

    set_property(TARGET ${PROJECT_NAME} PROPERTY SWIG_COMPILE_OPTIONS -go -intgosize 64 -cpperraswarn)
    set_property(TARGET ${PROJECT_NAME} PROPERTY SWIG_USE_TARGET_INCLUDE_DIRECTORIES TRUE)
    set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 11)
    set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
    target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_SOURCE_DIR})

The reason is that Go compiler has no idea about the library librawgo.so .原因是 Go 编译器不知道库librawgo.so

Put the following line to the top of the C comment in librawgo.go :将以下行放在librawgo.goC评论的顶部:

#cgo LDFLAGS: -L<path/to/dir/with/librawgo.so> -lrawgo -lraw

This instruction tells the compiler to add this line to the linker flags.该指令告诉编译器将此行添加到 linker 标志中。

Sorry, I don't know how to modify your CMake and SWIG settings to generate this line automatically.抱歉,我不知道如何修改您的 CMake 和 SWIG 设置以自动生成此行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM