“加载C ++库时的“未定义符号”

问题描述 投票:0回答:1

我正在尝试在Go中使用Google/re2。>>

我执行这样的CPP功能:

extern "C" bool PartialMatch (std::string text, std::string pattern, int* number)
{
    return RE2::PartialMatch(text, pattern, number);
}

完整代码:pal.cpp

pal.h:

#include <string>
extern "C" bool PartialMatch (std::string text, std::string pattern, int* number);

然后,我将其编译为:g++ pal.cpp pal.h -fPIC -std=gnu++14 -ggdb -O0 -fdata-sections -D"NDEBUG=1" -D"RELEASE=1" -ffunction-sections -Wl,-gc-sections -shared "-lpthread" -Wl,-soname,"pal.so" -o "pal.so"

然后,我像这样使用它:

package main

/*
#cgo CFLAGS: -I .
#cgo LDFLAGS: -lstdc++
*/
import "C"
import (
   "fmt"
   "github.com/rainycape/dl"
)

func TestPal()  {
   var PluginObj    *dl.DL
   var PartialMatch func(string, string, *int) int
   pattern := "(ciao) me"
   text := "ciao me"
   var number int

   //var printC   func(word *C.char) bool
   fmt.Print("hello\n")
   PluginObj, err := dl.Open("../pal.so", 0x01000)
   if err == nil {
      err = PluginObj.Sym("PartialMatch", &PartialMatch)
      if err != nil {
         fmt.Print("plugin lookup failed: " + err.Error())
      } else {
         fmt.Println(PartialMatch(pattern, text, &number))  // Expecting "1"
      }
   } else {
      fmt.Print("error %v\n", err)
   }
   return
}
func main() {
   TestPal()
}

当我构建并运行此代码时,它输出以下错误:pal.so: undefined symbol: ZN3re23RE23Arg9parseintEPKcmPv

看起来是因为此行而发生错误:return RE2::PartialMatch(text, pattern, number);

不理解RE2::

因为当我删除PartialMatch函数并使用isPalindrome函数对其进行测试时,它可以正常工作!

extern "C" int isPalindrome(char* word)
{
    int ret = 1;

    char *p = word;
    int len = strlen(word);
    char *q = &word[len-1];

    for (int i = 0 ; i < len ; ++i, ++p, --q)
    {
        if (*p != *q)
        {
            ret = 0;
        }
    }

    return ret;
}

我该如何解决?

我正在尝试在Go中使用Google / re2。我制作了这样的CPP函数:extern“ C” bool PartialMatch(std :: string text,std :: string pattern,int * number){return RE2 :: PartialMatch(text,pattern,...

c++ c regex go cgo
1个回答
0
投票
此符号在libre2.so中定义。这意味着您还必须加载该库。为什么不使用re2简单链接go程序?
© www.soinside.com 2019 - 2024. All rights reserved.