CGo For C++ Standard Libraries

I am trying to get a variable value from a c++ code using cgo. For libraries ended in .hall works fine, but for libraries like <iostream>, <map>, <string> etc, I got the following error:

fatal error: iostream: No such file or directory
    4 | #include <iostream>
      |          ^~~~~~~~~~

Below my code:

package main

/*
#cgo LDFLAGS: -lc++
#include <iostream>
std::string plus() {
    return "Hello World!\n";
}
*/
import "C"
import "fmt"

func main() {

    a := Plus_go()
    fmt.Println(a)

}
func Plus_go() string {
    return C.plus()
}

I added the #cgo LDFLAGS: -lc++ flag because I saw this recommendation on an answer in stackoverflow at wrapper - How to use C++ in Go - Stack Overflow.

I am using VS Code (not VS Studio), windows 10, Go 1.18 (lastest version).

I ran the following commands go tool cgo -debug-gcc mycode.go to trace compiler execution and output:

$ gcc -E -dM -xc -m64 - <<EOF

#line 1 "cgo-builtin-prolog"
#include <stddef.h> /* for ptrdiff_t and size_t below */

/* Define intgo when compiling with GCC.  */
typedef ptrdiff_t intgo;

#define GO_CGO_GOSTRING_TYPEDEF
typedef struct { const char *p; intgo n; } _GoString_;
typedef struct { char *p; intgo n; intgo c; } _GoBytes_;
_GoString_ GoString(char *p);
_GoString_ GoStringN(char *p, int l);
_GoBytes_ GoBytes(void *p, int n);
char *CString(_GoString_);
void *CBytes(_GoBytes_);
void *_CMalloc(size_t);

__attribute__ ((unused))
static size_t _GoStringLen(_GoString_ s) { return (size_t)s.n; }

__attribute__ ((unused))
static const char *_GoStringPtr(_GoString_ s) { return s.p; }
#line 3 "C:\\Users\\Home\\OneDrive\\Desktop\\DevicesC++\\devices.go"


#include <iostream>
std::string plus() {
    return "Hello World!\n";
}

#line 1 "cgo-generated-wrapper"
EOF
C:\Users\Home\OneDrive\Desktop\DevicesC++\devices.go:5:10: fatal error: iostream: No such file or directory
    5 | #include <iostream>
      |          ^~~~~~~~~~
compilation terminated.
C:\Users\Home\OneDrive\Desktop\DevicesC++\devices.go:5:10: fatal error: iostream: No such file or directory
    5 | #include <iostream>
      |          ^~~~~~~~~~
compilation terminated.

According suggestion I also created a file cpp_code.cpp and instead of write the c++ code as a comment inside go code I called the file as follows:

package main

/*
#include "cpp_code.cpp"
*/
import "C"
import "fmt"

func Plus_go() string {
    return C.plus()
}
func main() {

    a := Plus_go()
    fmt.Println(a)

}

The cpp file:

#include <iostream>

std::string plus() {
    return "Hello World!\n";
}

Tree of files :

Folder/
├─ go_code.go/
├─ cpp_code.cpp/

The output still remain with the same error:

In file included from .\go_code.go:5:
./cpp_code.cpp:1:10: fatal error: iostream: No such file or directory
    1 | #include <iostream>
      |          ^~~~~~~~~~
compilation terminated.

First, it appears that your C++ code doesn’t use anything from iostream, so you shouldn’t need to include it. Maybe this is just a pared down example and your real C++ code needs to do I/O. Here are some links that may help:

@mje According this link cgo command - cmd/cgo - Go Packages seems CGo supports C++. I tried the example last link but for me do not worked:

c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: $WORK\b001\_x002.o: in function `_cgo_d32036b73474_Cfunc_print':
/tmp/go-build/cgo-gcc-prolog:49: undefined reference to `print'
collect2.exe: error: ld returned 1 exit status

The second link I could not reproduce the suggestion “Why not just put a build tag in your lib.cpp files?” because I do not know how to do that even after search about it.