Outils pour utilisateurs

Outils du site


blog

Go lang - err compilation pour windows - crosscompilation

$ env GOOS=windows go build main.go 
# github.com/mappu/miqt/qt
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qaccessible_64bit.go:6:33: undefined: QAccessible__RelationFlag
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qgraphicsitem_64bit.go:6:36: undefined: QGraphicsItem__Extension
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:6:28: undefined: KeyboardModifier
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:7:21: undefined: Modifier
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:8:23: undefined: MouseButton
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:9:34: undefined: WindowType
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:10:22: undefined: InputMethodQuery
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:11:18: undefined: InputMethodQuery
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:12:29: undefined: InputMethodHint
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:13:23: undefined: GestureType
../../pkg/mod/github.com/mappu/miqt@v0.13.0/qt/gen_qnamespace_64bit.go:13:23: too many errors
$ env CGO_ENABLED=1 GOOS=windows go build main.go 
# runtime/cgo
gcc: error: unrecognized command-line option ‘-mthreads’; did you mean ‘-pthread’?
...
$ env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ CGO_LDFLAGS="-lssp" go build -ldflags="-s -w" -o app1.exe main.go                                                                                                      
github.com/mappu/miqt/qt: exec: "i686-w64-mingw32-g++": executable file not found in $PATH
...
#sudo apt-get install gcc-multilib-i686-linux-gnu
sudo apt-get install gcc-mingw-w64-bootstrap
$ env GOOS=windows GOARCH=386 CGO_ENABLED=1 CC=i686-w64-mingw32-gcc CXX=i686-w64-mingw32-g++ CGO_LDFLAGS="-lssp" go build -ldflags="-s -w" -o app1.exe main.go
...
# github.com/mappu/miqt/qt
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qatomic.h:41,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/qrefcount.h:43,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/qbytearray.h:44,
                 from /usr/include/x86_64-linux-gnu/qt5/QtCore/QByteArray:1,
                 from gen_qxmlstream.cpp:1:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:45:12: fatal error: type_traits: No such file or directory
   45 | #  include <type_traits>
      |            ^~~~~~~~~~~~~
compilation terminated.

Test en 64 bits :

sudo apt-get install gcc-mingw-w64-x86-64-win32
sudo apt-get install g++-mingw-w64-x86-64-win32
rm /tmp/go-* -rf
go clean -cache -modcache -fuzzcache
env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_LDFLAGS="-lssp" go build -ldflags="-s -w" -o app1.exe main.go
...
3755312453/000657.o /tmp/go-link-3755312453/000658.o /tmp/go-link-3755312453/000659.o /tmp/go-link-3755312453/000660.o /tmp/go-link-3755312453/000661.o /tmp/go-link-3755312453/000662.o /tmp/go-link-3755312453/000663.o /tmp/go-link-3755312453/000664.o /tmp/go-link-3755312453/000665.o /tmp/go-link-3755312453/000666.o /tmp/go-link-3755312453/000667.o /tmp/go-link-3755312453/000668.o /tmp/go-link-3755312453/000669.o /tmp/go-link-3755312453/000670.o /tmp/go-link-3755312453/000671.o /tmp/go-link-3755312453/000672.o /tmp/go-link-3755312453/000673.o /tmp/go-link-3755312453/000674.o /tmp/go-link-3755312453/000675.o /tmp/go-link-3755312453/000676.o /tmp/go-link-3755312453/000677.o /tmp/go-link-3755312453/000678.o /tmp/go-link-3755312453/000679.o /tmp/go-link-3755312453/000680.o /tmp/go-link-3755312453/000681.o /tmp/go-link-3755312453/000682.o /tmp/go-link-3755312453/000683.o /tmp/go-link-3755312453/000684.o /tmp/go-link-3755312453/000685.o /tmp/go-link-3755312453/000686.o /tmp/go-link-3755312453/000687.o /tmp/go-link-3755312453/000688.o /tmp/go-link-3755312453/000689.o /tmp/go-link-3755312453/000690.o -lssp -lQt5Widgets -lQt5Gui -lQt5Core -lssp -lssp -Wl,-T,/tmp/go-link-3755312453/fix_debug_gdb_scripts.ld -Wl,--start-group -lmingwex -lmingw32 -Wl,--end-group -lkernel32                                                                                                                                                                                               
/usr/bin/x86_64-w64-mingw32-ld.bfd: cannot find -lQt5Widgets: No existe el fichero o el directorio                                                                                                                
/usr/bin/x86_64-w64-mingw32-ld.bfd: cannot find -lQt5Gui: No existe el fichero o el directorio                                                                                                                    
/usr/bin/x86_64-w64-mingw32-ld.bfd: cannot find -lQt5Core: No existe el fichero o el directorio                                                                                                                   
collect2: error: ld returned 1 exit status                                                                                                                                                                        
2026/05/03 17:32 · Jean-Baptiste

Go lang - template - gotemplate

// Source - https://stackoverflow.com/a/59083271
// Posted by wasmup, modified by community. See post 'Timeline' for change history
// Retrieved 2026-05-03, License - CC BY-SA 4.0
 
package main
 
import (
    "fmt"
    "os"
    "text/template"
)
 
func main() {
    name := "AlphaGo"
 
    fmt.Printf("I am %s\n", name)
 
    t := template.Must(template.New("my").Parse("I am {{.Name}}\n"))
    t.Execute(os.Stdout, struct{ Name string }{name}) // I am AlphaGo
 
    t2 := template.Must(template.New("my").Parse("I am {{.name}}\n"))
    t2.Execute(os.Stdout, map[string]string{"name": name}) // I am AlphaGo
 
    t3 := template.Must(template.New("my").Parse("I am {{.}}\n"))
    t3.Execute(os.Stdout, name) // I am AlphaGo
}

FIXME

2026/05/03 16:06 · Jean-Baptiste

Go lang - if x in list

Source : https://stackoverflow.com/questions/15323767/does-go-have-if-x-in-construct-similar-to-python

Since Go 1.18 or newer, you can use slices.Contains.

func main() {
        liste1 := []string{"Meow", "Waf", "Moo"}
 
        if !slices.Contains(liste1, "Waff") {
                fmt.Println("Aucun chien trouvé")
        } else {
                fmt.Println("Un chien a été trouvé")
        }
}

Before Go 1.18 there was no built-in operator. You needed to iterate over the array. You had to write your own function to do it, like this:

func stringInSlice(a string, list []string) bool {
    for _, b := range list {
        if b == a {
            return true
        }
    }
    return false
}

FIXME

2026/04/29 18:42 · Jean-Baptiste

Pascal Lazarus - Compilation croisée dans un conteneur

Création de l'image conteneur

git clone --depth=1 https://github.com/ChrisWiGit/lazarus-docker
cd lazarus-docker
podman build -t lazarus-docker .

Paramètre de compilation

cat > runenv.sh <<'EOF'
#! /bin/bash
cd /project
lazbuildl64 *.lpr && install -d build/linux64/ && mv project1 /project/build/linux64/
lazbuildw32 *.lpr && install -d build/win32/ && mv project1.exe /project/build/win32/
lazbuildw64 *.lpr && install -d build/win64/ && mv project1.exe /project/build/win64/
EOF
 
chmod +x runenv.sh 

Compilation

podman run -v $PWD:/project --rm localhost/lazarus-docker:latest /project/runenv.sh

Suppression des symboles et info de debug pour réduire la taille des fichiers :

strip build/*/*
2026/04/27 23:31 · Jean-Baptiste

script - subshells sous-shells - comportement déroutant de bash qu'un débutant doit connaître

Exemple variable globale

Exemple bash de problème d'affectation de variable globale

test_gvar1.sh

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
 
echo $(func_1)
echo $VAR
$ ./test_gvar1.sh
0
Hello
0

Alors que

test_gvar2.sh

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
 
func_1
echo $VAR
$ ./test_gvar2.sh
0
Hello
4

Dans le 1er exemple la variable n'est pas affectée par la fonction. En effet avec $(nom_de_fonction) bash lance un autre bash et copie la fonction, puis retourne le résultat au bash père.

De cette manière il est impossible par exemple d'implémenter un compteur qui compterait le nombre de fois que la fonction est appelée.

Bien sûr il est possible à la place de faire :

echo "Fonction func appelée à $(date --rfc-3339=second)" >> /tmp/trace.log

Mais ce n'est pas terrible

Voici une solution :

#! /bin/bash
 
VAR=0
 
func_1() {
    echo Hello
    VAR=4
}
 
echo $VAR
echo "${ func_1; }"
echo $VAR

Autre solution : Utiliser les pointeurs en bash !

Voir : https://stackoverflow.com/questions/75493732/how-to-modify-a-global-variable-within-a-function-and-return-a-boolean-in-bash

#! /bin/bash
 
VAR=0
 
func_1() {
    declare -n -x VAR_ref=$1
    VAR_ref=4
}
 
echo $VAR
func_1 VAR
echo $VAR
Exemple de commande exit qui semble ignorée

test_exit1.sh

#! /bin/bash
 
funcA() {
        echo A
        echo $(funcB)
}
 
 
funcB() {
        echo B
        exit 13
}
 
 
funcA
 
echo Suite du script

Il est possible de remplacer echo $(funcB) par echo ${ funcB; } mais la sortie de la fonction n'est pas affichée sur la sortie standard

$ ./test_exit1.sh
A
B
Suite du script
$ echo $?
0

Alors que :

test_exit2.sh

#! /bin/bash
 
funcA() {
        echo A
        funcB
}
 
 
funcB() {
        echo B
        exit 13
}
 
 
funcA
 
echo Suite du script
$ ./test_exit2.sh
A
B
$ echo $?
13

Exemple solution contournement

Une solution simple consiste à ajouter un trap et de remplacer la commande exit par la commande kill $$

test_exit3.sh

#! /bin/bash
 
trap 'exit 13' SIGUSR1
 
funcA() {
        echo A
        echo $(funcB)
}
 
 
funcB() {
        echo B
        # exit 13
        kill -s SIGUSR1 $$
}
 
 
funcA
 
echo Suite du script
$ ./test_exit3.sh
A
B

Autres

Affecter une variable sans créer de sous-shell

echo 'hello' | { read msg; echo "$msg"; }
2026/04/25 11:16 · Jean-Baptiste
blog.txt · Dernière modification : de 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki