Works like charm. Thanks a lot.
Here is my code:
aptwrap.h
#ifdef __cplusplus
extern "C" {
#endif
void print();
#ifdef __cplusplus
}
#endif
aptwrap.cpp
#include "aptwrap.h"
#include <iostream>
#include <apt-pkg/cachefile.h>
#include <apt-pkg/pkgcache.h>
#ifdef __cplusplus
extern "C" {
#endif
void print() {
std::cout << "XXXX " << std::endl;
pkgInitConfig(*_config);
pkgInitSystem(*_config, _system);
pkgCacheFile cache_file;
pkgCache* cache = cache_file.GetPkgCache();
for (pkgCache::PkgIterator package = cache->PkgBegin(); !package.end();
package++) {
std::cout << package.Name() << std::endl;
}
}
#ifdef __cplusplus
}
#endif
main.go
package main
// #cgo CXXFLAGS: -I.
// #cgo CFLAGS: -I.
// #cgo LDFLAGS: -lapt-pkg
// #include "aptwrap.h"
import "C"
func main() {
C.print()
}
Thanks
On Thursday, April 11, 2019 at 7:51:42 PM UTC+3, Marcin Romaszewicz wrote:
>
> 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 <[email protected] <javascript:>>
> 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 [email protected] <javascript:>.
>> 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 [email protected].
For more options, visit https://groups.google.com/d/optout.