dongshuogai2343
2019-07-12 09:59 阅读 228

Cgo找不到<iostream>之类的标准库

I'm trying to include C++ code in my Go code, but isn't recognized.

I first thought that it considers it as C code and try (and fail) to compile as such, but removing the include line actually gives me c++ error troubleshooting like this error: ‘cout’ is not a member of ‘std’ The code compiles correctly with g++.

I have tried to add the -lstdc++ LDLFLAG, and add the path to the lib in CXXFLAG but it doesn't change a thing.

I have made some other tests (and all fail) but this is the smallest one.

This is the c++ files

test.cpp

#include "test.hpp"
    int test() 
    {
        std::cout << "Hello, World! ";
        return 0;
    }

test.hpp 
#include <iostream>
int test() ;

And this is my go file

//#cgo CXXFLAGS: -I/usr/lib/
//#cgo LDFLAGS: -L/usr/lib/ -lstdc++
//#include "test.hpp"
import "C"

func main() {
    C.test()
}

I compile using go build but I have also tried to use env CGO_ENABLED CGO_CXXFLAGS="-std=c++11" go build (the env part is fish specific) and it returns the same error.

It's supposed to compile correctly, but instead I have iostream: No such file or directory.

EDIT : I tried to add CFLAGS: -x c++ as suggested in the comments, the compiler searches at the right place, but I get another error invalid conversion from ‘void*’ to ‘_cgo_96e70225d9dd_Cfunc_test(void*)::<unnamed struct>*’ [-fpermissive] and I don't know if it's related to this new flafg

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongzhong5833 dongzhong5833 2019-07-12 13:31

    cgo makes it very easy to wrap C with Go, but C++ is a bit different. You have to extern "C" the functions that you want to make a function-name in C++ have 'C' linkage, otherwise the linker won't see the function. So, the actual problem is in the C++ header file. If you can't change the C++ code because it's a library, you may have to write wrappers (example).

    This will compile:

    .
    ├── test.cpp
    ├── test.go
    └── test.hpp
    

    test.hpp

    #ifdef __cplusplus
    extern "C" {
    #endif
    
        int test();
    #ifdef __cplusplus
    }
    #endif
    

    test.cpp

    #include <iostream>
    #include "test.hpp"
    int test() {
        std::cout << "Hello, World! ";
        return 0;
    }
    

    test.go

    package main
    
    // #cgo CXXFLAGS: -I/usr/lib/
    // #cgo LDFLAGS: -L/usr/lib/ -lstdc++
    // #include "test.hpp"
    import "C"
    
    func main() {
        C.test()
    }
    

    Put the files in the same folder, run go build

    Hello, World!

    点赞 评论 复制链接分享
  • zhangcunli8499 zhangcunli8499 2020-09-16 18:03

    我现在也碰到了类似的情况:

    我现在在使用 cgo 调用 几个so,但是这几个so的头文件中 引入了 vector 等C++标准库数据结构;
    正常编译时,提示错误: error: vector: No such file or directory

    查了一些资料,尝试了 #cgo amd64 386 CFLAGS: -DX86=1 -xc++ 参数,结果编译的时候不报找不到文件错误了

    但是报如下错误:(如下错误是我调用了共享库中的一个接口报的错):
    unexpected type: (unsupported type ReferenceType)

        var nPDLLHandle C.int32_t
        ret := int32(C.DPSDK_Create(C.DPSDK_CORE_SDK_SERVER, &nPDLLHandle))   //这一行报错:unexpected type: (unsupported type ReferenceType),调用肯定是正确的。
    

    cgo 调用 C++ 的共享库,有什么解决办法吗? 必须把C++包装成C才能用吗?

    点赞 评论 复制链接分享

相关推荐