3

TLDR I'm trying to make a simple python library that runs go code through pybind11 and CMake

I installed pybind11 through anaconda in a venv goscrapper IDE is VSCode

here are the steps I took

  1. create add.go
  2. run go build -o addlib.dll -buildmode=c-shared add.go to get addlib.dll and addlib.h
  3. create addlib.cpp
  4. Added the paths to pybind11.h, Python.h, and pybind11Config.cmake (I kept getting a CMake error: "Could not find a package configuration file provided by "pybind11" with any of the following names: pybind11Config.cmake")
  5. create CMakeLists.txt and ran cmake .. and cmake --build . in project_root/build directory using MSVC Developer Command Prompt for VS 2022

Here's my code

add.go

package main

import "C"
import "fmt"

//export Add
func Add(a int, b int) int {
    return a + b
}

//export Greet
func Greet() {
    fmt.Println("Hello world")
}

func main() { }

addlib.cpp

#include <pybind11/pybind11.h>

extern "C" {
    #include "addlib.h"
}

namespace py = pybind11;

int add(int a, int b) {
    return Add(a, b); 
}

void greet() {
    Greet();
}

PYBIND11_MODULE(Go_Pybind_practice_v2, m) {
    m.def("add", &add, "Add two integers");
    m.def("greet", &greet, "Print greeting from Go");
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                "C:\\dev\\go_pybind_practice_v3\\pybind11\\include\\pybind11", // for pybind11.h
                "C:\\Users\\accra\\.conda\\envs\\goscraper\\include", // for Python.h
                "C:\\Users\\accra\\.conda\\envs\\goscraper\\Library\\share\\cmake\\pybind11" // for pybind11Config.cmake
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "10.0.22621.0",
            "compilerPath": "cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64"
        }
    ],
    "version": 4
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(go_pybind_practice_v3)

set(pybind11_DIR "C:\\Users\\accra\\.conda\\envs\\goscraper\\Library\\share\\cmake\\pybind11")

find_package(pybind11 REQUIRED)

pybind11_add_module(go_pybind_practice_v3 addlib.cpp)
target_link_libraries(go_pybind_practice_v3 PRIVATE pybind11::module "C:\\dev\\go_pybind_practice_v3\\addlib.lib")

addlib.h

/* Code generated by cmd/cgo; DO NOT EDIT. */

/* package command-line-arguments */


#line 1 "cgo-builtin-export-prolog"

#include <stddef.h>

#ifndef GO_CGO_EXPORT_PROLOGUE_H
#define GO_CGO_EXPORT_PROLOGUE_H

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; ptrdiff_t n; } _GoString_;
#endif

#endif

/* Start of preamble from import "C" comments.  */




/* End of preamble from import "C" comments.  */


/* Start of boilerplate cgo prologue.  */
#line 1 "cgo-gcc-export-header-prolog"

#ifndef GO_CGO_PROLOGUE_H
#define GO_CGO_PROLOGUE_H

typedef signed char GoInt8;
typedef unsigned char GoUint8;
typedef short GoInt16;
typedef unsigned short GoUint16;
typedef int GoInt32;
typedef unsigned int GoUint32;
typedef long long GoInt64;
typedef unsigned long long GoUint64;
typedef GoInt64 GoInt;
typedef GoUint64 GoUint;
typedef size_t GoUintptr;
typedef float GoFloat32;
typedef double GoFloat64;
#ifdef _MSC_VER
#include <complex.h>
typedef _Fcomplex GoComplex64;
typedef _Dcomplex GoComplex128;
#else
typedef float _Complex GoComplex64;
typedef double _Complex GoComplex128;
#endif

/*
  static assertion to make sure the file is being used on architecture
  at least with matching size of GoInt.
*/
typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];

#ifndef GO_CGO_GOSTRING_TYPEDEF
typedef _GoString_ GoString;
#endif
typedef void *GoMap;
typedef void *GoChan;
typedef struct { void *t; void *v; } GoInterface;
typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;

#endif

/* End of boilerplate cgo prologue.  */

#ifdef __cplusplus
extern "C" {
#endif

extern __declspec(dllexport) GoInt Add(GoInt a, GoInt b);
extern __declspec(dllexport) void Greet();

#ifdef __cplusplus
}
#endif
1
  • 1
    And how did you arrive at the error mentioned in your question title? It looks like a python error but I don't see any python code. Also, your pybind module ends in v2 but your cmake target ends in v3... Commented Mar 10 at 10:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.