Outils pour utilisateurs

Outils du site


tech:err_go_lang_et_langage_c

Err Go lang et langage C

Err: could not determine what C.factorial refers to

# command-line-arguments
./maint.go:25:43: could not determine what C.factorial refers to

Solution

Remplacer

package main
 
// #include <stdio.h>
// #include <stdlib.h>
//
// uint factorial(uint N) {
//     int fact = 1, i;
//
//     // Loop from 1 to N to get the factorial
//     for (i = 1; i <= N; i++) {
//         fact *= i;
//     }
//
//     return fact;
// }
 
import "C"

Par

package main
 
// #include <stdio.h>
// #include <stdlib.h>
//
// uint factorial(uint N) {
//     int fact = 1, i;
//
//     // Loop from 1 to N to get the factorial
//     for (i = 1; i <= N; i++) {
//         fact *= i;
//     }
//
//     return fact;
// }
import "C"

Il ne faut pas d'espace entre le bloque du code C et import "C"

Err: cannot use i (variable of type int) as _Ctype_uint value in argument to (_Cfunc_factorial)

Solution

Il faut caster. Ici dans notre exemple avec C.uint()

package main
 
// #include <stdio.h>
// #include <stdlib.h>
//
// uint factorial(uint N) {
//     int fact = 1, i;
//
//     // Loop from 1 to N to get the factorial
//     for (i = 1; i <= N; i++) {
//         fact *= i;
//     }
//
//     return fact;
// }
import "C"
import "fmt"
 
func main() {
	for i := range 10 {
		//fmt.Printf("i: %.3d;, fact: %.6d\n", i, C.factorial(i))
		fmt.Printf("i: %.3d;, fact: %.6d\n", i, C.factorial(C.uint(i)))
		i++
	}
}

Err: go: no Go source files

Compilation croisée avec du code Go contenant du C

$ env GOOS=windows go build main.go 
go: no Go source files

Solution

$ env CGO_ENABLED=1 GOOS=windows go build main.go 
# runtime/cgo
gcc: error: unrecognized command-line option ‘-mthreads’; did you mean ‘-pthread’?
# runtime/cgo
gcc: error: unrecognized command-line option ‘-mthreads’; did you mean ‘-pthread’?

Installer les deps pour compiler

sudo apt-get install gcc-multilib gcc-mingw-w64

Lancer la compilation

$ env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc go build main.go                                                                    
# command-line-arguments                                                                                                                                                                                          
./main.go:22:43: could not determine what C.factorial refers to                                                                                                                                                   
cgo:                                                                                                                                                                                                              
x86_64-w64-mingw32-gcc errors for preamble:                                                                                                                                                                       
./main.go:6:2: error: unknown type name 'uint'; did you mean 'int'?                                                                                                                                               
    6 | // uint factorial(uint N) {                                                                                                                                                                               
      |  ^~~~                                                                                                                                                                                                     
      |  int                                                                                                                                                                                                      
./main.go:6:17: error: unknown type name 'uint'; did you mean 'int'?
    6 | // uint factorial(uint N) {
      |                 ^~~~
      |                 int

Remplacer les unsigned int (uint) par des int

sed -i.bak -e 's/uint/int/g' main.go
env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CXX=x86_64-w64-mingw32-g++ CC=x86_64-w64-mingw32-gcc go build main.go

C'est tout de même problématique

tech/err_go_lang_et_langage_c.txt · Dernière modification : de Jean-Baptiste

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki