Reputation: 4014
When I ran this code, I expected to printing result like A: 4, B: 89
.
But actually, Does not display nothing.
Why this program does not display result to stdout?
main.go:
package main
/*
#include "c.h"
*/
import "C"
import (
"unsafe"
)
type S struct {
A int
B int
}
func main() {
s := &S{A: 4, B: 89}
pass_to_c := (*C.S)(unsafe.Pointer(s))
C.gostruct(pass_to_c)
}
c.h
#include <stdio.h>
#include <stdlib.h>
typedef struct {
long int A;
long int B;
} S;
extern void gostruct(S *struct_s) {
printf("A: %ld, B: %ld\n", struct_s->A, struct_s->B);
}
Upvotes: 2
Views: 2778
Reputation: 71
Adding "console": "integratedTerminal"
to the vscode launch.json
solved my problem.
Upvotes: 2
Reputation: 2483
I run program in LiteIDE, not show c printf output.
But run the same program in terminal, then c printf output displayed.
Upvotes: 1
Reputation: 4014
Thanks for comments.
I can got expected result with below codes
main.go:
package main
/*
#include "c.h"
*/
import "C"
import (
"unsafe"
)
type S struct {
A int64 // 64bit int
B int64 // 64bit int
}
func main() {
s := &S{A: 4, B: 89}
pass_to_c := (*C.S)(unsafe.Pointer(s))
C.gostruct(pass_to_c)
}
c.h:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
long long int A; // 64bit int
long long int B; // 64bit int
} S;
extern void gostruct(S *struct_s) {
printf("{A: %lld, B: %lld}\n", struct_s->A, struct_s->B);
}
I suppose struct field must use same type between languages. In question code, struct fields type are not same. (C struct: 32bit int, Go struct: 64bit int)
In answer code, struct field is same between language. (both struct: 64bit int)
Note that My architecture is darwin/amd64
Upvotes: 2