On Thu, Apr 11, 2019 at 5:54 PM av <[email protected]> wrote:
> Hi *,
>
> I want to compile C++ code, because the library I need is C++ only.
>
> Here is my wrapper.hpp:
> #include <iostream>
>
> void print() {
> std::cout << "Xx";
> }
>
> And here is my main.go:
> package main
>
> // #cgo CXXFLAGS: -I.
> // #cgo CFLAGS: -I.
> // #cgo LDFLAGS:
> // #include "wrapper.hpp"
> import "C"
>
> func main() {
> C.print()
> }
>
> The output of "go build" is:
> In file included from ./main.go:6:0:
> ./wrapper.hpp:1:20: fatal error: iostream: No such file or directory
> #include <iostream>
> ^
> compilation terminated.
>
> I can make this work by using Makefile and static library, but this reduce
> portability significantly.
>
> Basically I want to create libapt-pkg wrapper, but I don't want to
> introduce another debian package for the wrapper.
>
> Do you have any idea how to make this work?
>
you'd need to export a C-only API in the header files you want to use
from/with cgo.
ie:
// wrapper.h
#ifdef __cplusplus
extern "C" {
#endif
void print();
#ifdef __cplusplus
}
#endif
and:
// wrapper.cc
#include <iostream>
#include "wrapper.h"
void print() {
std::cout << "Xx" << std::endl;
}
and, finally:
package main
// #include "wrapper.h"
import "C"
func main() {
C.print()
}
hth,
-s
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.