Outils pour utilisateurs

Outils du site


tech:err_go_lang_et_langage_c

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
tech:err_go_lang_et_langage_c [2026/04/21 21:07] – créée Jean-Baptistetech:err_go_lang_et_langage_c [2026/04/21 21:37] (Version actuelle) Jean-Baptiste
Ligne 4: Ligne 4:
 # Err Go lang et langage C # Err Go lang et langage C
  
 +Voir aussi :
 +* [[Inclure du C dans Go lang]]
  
 ## Err: could not determine what C.factorial refers to ## Err: could not determine what C.factorial refers to
Ligne 60: Ligne 62:
  
  
-FIXME+ 
 +## 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()` 
 + 
 +~~~go 
 +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 
 +~~~bash 
 +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  
 +~~~bash 
 +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.1776798473.txt.gz · Dernière modification : de Jean-Baptiste

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki