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