简体   繁体   中英

Using a swig wrapped c++ library with go

I have managed to successfully wrap a large C++ library with SWIG . 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.

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. 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. 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?

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 .

Put the following line to the top of the C comment in librawgo.go :

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

This instruction tells the compiler to add this line to the linker flags.

Sorry, I don't know how to modify your CMake and SWIG settings to generate this line automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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