Wrap your C++ code in C.

Create a header which is simply:

cwrapper.h:
void printSomething()

Then you can have a cwrapper.cpp instead of your wrapper.hpp:
#include <iostream>
extern "C" {


void printSomething() {
  printf("Hello World");
}
}

Your C++ code will produce a function named printSomething with C linkage
semantics, and Go can call that without issue. By hiding <iostream> from
CGO, you don't have it trying to find C++ headers.


On Thu, Apr 11, 2019 at 8:49 AM <aleksandar.valc...@gmail.com> wrote:

> Hi *,
>
> I have following C++ code:
>
> wrapper.hpp:
>
> #include <iostream>
>
> void printSomething() {
>   printf("Hello World");
> }
>
> And corresponding main.go:
>
> package main
>
> // #cgo CXXFLAGS: -I.
> // #cgo CFLAGS: -I.
> // #cgo LDFLAGS:
> // #include "wrapper.hpp"
> import "C"
>
> func main() {
>         C.print()
> }
>
> Unfortunately the result of "go build ." is:
>
> # _/home/avalchev/Sources/apt
> In file included from ./main.go:6:0:
> ./wrapper.hpp:1:20: fatal error: iostream: No such file or directory
>  #include <iostream>
>                     ^
> compilation terminated.
>
>
> Basically I'm trying to create debian's libapt-pkg wrapper, but the only
> workaround I tried (and it works) is to create static library, but this
> reduces portability a lot.
>
> Is there any way to make this work ?
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to