On Mon, Jul 20, 2020 at 12:28 PM Mahdi Hosseini <m.hosein...@gmail.com> wrote:
>
> Hi,
> I try to recompile Go for a new platform which is a s390x platform using 
> Clang instead of GCC. I can not work for C string in CGO. Apparently char in 
> unsigned by default on my platform and building even the simple CGO program 
> always fails with this error:
>
> /home/user/tmp/go-build468743286/b001/_cgo_gotypes.go:175:25: undefined: 
> _Ctype_char
>
> the code is:
>
> package main
>
> //#include <stdio.h>
> //char* callC() {
> // return "Calling C code!";
> //}
> import "C"
>
> import "fmt"
>
> func main() {
>         fmt.Println("Convert C String to Go String")
>         str := C.GoString(C.callC())
>         fmt.Println(str)
> }
>
> to overcome this I modified the gcc.go file in src/cmd/cgo and added this:
>
> if s == "uchar" {
>                 s = "char"
>             }
>             name := c.Ident("_Ctype_" + s)
> I don't now how CGO always convert my char* to *_Ctype_uchar instead of 
> *_Ctype_char.
> Anyone have a clue on this?

cgo is just using the debug information generated by the C compiler.
It expects to see debug info for the type "char".  For example, if I
compile this C file

#include <stdio.h>
char* callC() {
 return "Calling C code!";
}

with clang on my system and run "readelf --debug" on the resulting
object file, I see

 <1><48>: Abbrev Number: 4 (DW_TAG_base_type)
    <49>   DW_AT_name        : (indirect string, offset: 0x4c): char
    <4d>   DW_AT_encoding    : 6        (signed char)
    <4e>   DW_AT_byte_size   : 1

cgo will use this to define the type "C.char".

This is independent of whether char is signed or unsigned.  Given that
your code uses "char", it surprises me that there is no named entry
for it in the debug info.

Ian

-- 
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/CAOyqgcW495hYL-yjFB2dAx7z_A_HwEin9-nQKRG18-wvcKXgCg%40mail.gmail.com.

Reply via email to