Hello, I tried your example and I got "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" Windows 10, VS Code.
Have you some idea why? Em segunda-feira, 15 de abril de 2019 às 05:51:31 UTC-3, Sebastien Binet escreveu: > On Thu, Apr 11, 2019 at 5:54 PM av <aleksanda...@gmail.com> 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 golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f20f8906-cbdf-4828-8056-a910d23cdd58n%40googlegroups.com.