Giter Club home page Giter Club logo

Comments (15)

mcastilho avatar mcastilho commented on May 22, 2024 2

@chennqqi

This is what I did to compile Windows 64-bit libyara.a. I did all compilation in a Ubuntu docker image using MinGW-w64

> docker run -t -i ubuntu:latest /bin/bash

And these are the exact commands I performed.

apt-get update
apt-get install build-essential git
apt-get install gcc-multilib gcc-mingw-w64 autoconf automake libtool libjansson-dev libmagic-dev libssl-dev
git clone --depth=50 https://github.com/VirusTotal/yara.git VirusTotal/yara
export CONFIGFLAGS=--host=x86_64-w64-mingw32
export CC=gcc
cd VirusTotal/yara
./bootstrap.sh
unset CC
./configure $CONFIGFLAGS
make
cd libyara/.libs
file libyara.a

Worked like a charm.... I was able to incorporate this libyara.a into this go-yara project

I had to do one modification and create a cgo_windows.go file with the following CGO instructions. I also copied the .H headers into a new ./include folder and the windows lib into a new ./lib folder

// +build windows

package yara

/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo amd64 LDFLAGS: -L${SRCDIR}/lib -lyara64
#cgo 386 LDFLAGS: -L${SRCDIR}/lib -lyara32
*/
import "C"
...

and modified the old cgo.go file to:

// +build !windows

package yara

/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo LDFLAGS: -lyara
*/
import "C"

Now I can build from my Sierra macOS both Darwin and Windows executables of my project that is using go-yara package with the following command to target Windows:

CC=/usr/local/Cellar/mingw-w64/4.0.6/bin/x86_64-w64-mingw32-gcc \
LD=/usr/local/Cellar/mingw-w64/4.0.6/bin/x86_64-w64-mingw32-ld \
  CGO_ENABLED=1 \
  GOOS=windows \
  CGO_CFLAGS=-I/usr/local/Cellar/mingw-w64/4.0.6/x86_64-w64-mingw32/include \
  CGO_LDFLAGS=-L/usr/local/Cellar/mingw-w64/4.0.6/x86_64-w64-mingw32/lib \
  go build --ldflags '-extldflags "-static"' .

And voilà !!! Works like a charm.... I have tested the binary on Windows 10 x64 and works very well... without any dependencies.

It took a while to figure it out. Hopefully this will help other people... Maybe we should move this instructions to the Readme ? Feedback ?

from go-yara.

hillu avatar hillu commented on May 22, 2024 1

@chennqqi Neither -I/usr/i686-w64-mingw32/include nor -L/usr/i686-w64-mingw32/lib should be necessary, the MinGW compiler includes those search paths by default.

from go-yara.

chennqqi avatar chennqqi commented on May 22, 2024

Both msys and msys2 work correctly.
Compile libyara by mingw-gcc.

from go-yara.

hillu avatar hillu commented on May 22, 2024

@mcastilho So you are still using the libyara32 and libyara64 generated through the VS project? Do you think that it would make sense to add a build tag, say vs?

#cgo windows,vs,i386 LDFLAGS: -lyara32
#cgo windows,vs,amd64 LDFLAGS: -lyara64

from go-yara.

mcastilho avatar mcastilho commented on May 22, 2024

No. I am not using the VIsual studio output. I don't think is compatible. I am using the binaries produced by the compilation under Ubunto using MingGW

from go-yara.

hillu avatar hillu commented on May 22, 2024

In that case, the resulting libraries should simply have the name libyara.a (without a 32 or 64 prefix), shouldn't they? Unless you have renamed them, of course.

from go-yara.

mcastilho avatar mcastilho commented on May 22, 2024

Yes. I did rename them

from go-yara.

hillu avatar hillu commented on May 22, 2024

Would you mind looking at my updated build/install instructions and tell me if they work for you?

from go-yara.

chennqqi avatar chennqqi commented on May 22, 2024

@mcastilho I did that under windows and linux.

  1. build static libyara and install to a libyara_path
    sudo yum install mingw32-gcc
    ./configure --build=i686-w64-mingw32 --without-crypto --disable-shared --disable-dependency-tracking --disable-magic --disable-cuckoo --without-crypto
    make && sudo make install prefix=/usr/i686-w64-mingw32/
    `
  2. build go-yara and link libyara_path
    i386-windows:
    CC=i686-w64-mingw32-gcc CGO_ENABLED=1 GOOS=windows GOARCH=386 CGO_CFLAGS=-I/usr/i686-w64-mingw32/include CGO_LDFLAGS='-L/usr/i686-w64-mingw32/lib -lyara' go install --ldflags '-extldflags "-static"' github.com/hillu/go-yara
    x64-linux:
    CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CGO_CFLAGS=-I/usr/include CGO_LDFLAGS='-L/opt/x86_64/lib -lyara -lm' go install --ldflags '-extldflags "-static"' github.com/hillu/go-yara
  3. build your app with link libyara_path
    x64-linux:
    CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CGO_LDFLAGS='-L/opt/x86_64/lib -L/usr/lib64' go build --ldflags '-s -w -extldflags "-static -lm"' YOURAPPPATH
    ...other environment.

May this help you. Happy chinese new year!

from go-yara.

valinorsystems avatar valinorsystems commented on May 22, 2024

I got error when I built go-yara:
could not determine kind of name for C.ERROR_INSUFICIENT MEMORY

I tried building with all of instructions, in all cases - this error. Tried on Debian and Ubuntu - same thing.
go version - 1.8

=============== [SOLVED] ====================

just correct in error.go: C.ERROR_INSUFICIENT_MEMORY to C.ERROR_INSUFFICIENT MEMORY ))) just add "F" letter

from go-yara.

hillu avatar hillu commented on May 22, 2024

oh, awesome, Fixing typos in YARA after 3.5 breaks the API...

from go-yara.

hillu avatar hillu commented on May 22, 2024

Closing the issue since everything discussed here has been resolved.

from go-yara.

N0body007 avatar N0body007 commented on May 22, 2024

x86_64-w64-mingw32-gcc

@chennqqi

This is what I did to compile Windows 64-bit libyara.a. I did all compilation in a Ubuntu docker image using MinGW-w64

> docker run -t -i ubuntu:latest /bin/bash

And these are the exact commands I performed.

apt-get update
apt-get install build-essential git
apt-get install gcc-multilib gcc-mingw-w64 autoconf automake libtool libjansson-dev libmagic-dev libssl-dev
git clone --depth=50 https://github.com/VirusTotal/yara.git VirusTotal/yara
export CONFIGFLAGS=--host=x86_64-w64-mingw32
export CC=gcc
cd VirusTotal/yara
./bootstrap.sh
unset CC
./configure $CONFIGFLAGS
make
cd libyara/.libs
file libyara.a

Worked like a charm.... I was able to incorporate this libyara.a into this go-yara project

I had to do one modification and create a cgo_windows.go file with the following CGO instructions. I also copied the .H headers into a new ./include folder and the windows lib into a new ./lib folder

// +build windows

package yara

/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo amd64 LDFLAGS: -L${SRCDIR}/lib -lyara64
#cgo 386 LDFLAGS: -L${SRCDIR}/lib -lyara32
*/
import "C"
...

and modified the old cgo.go file to:

// +build !windows

package yara

/*
#cgo CFLAGS: -I${SRCDIR}/include
#cgo LDFLAGS: -lyara
*/
import "C"

Now I can build from my Sierra macOS both Darwin and Windows executables of my project that is using go-yara package with the following command to target Windows:

CC=/usr/local/Cellar/mingw-w64/4.0.6/bin/x86_64-w64-mingw32-gcc \
LD=/usr/local/Cellar/mingw-w64/4.0.6/bin/x86_64-w64-mingw32-ld \
  CGO_ENABLED=1 \
  GOOS=windows \
  CGO_CFLAGS=-I/usr/local/Cellar/mingw-w64/4.0.6/x86_64-w64-mingw32/include \
  CGO_LDFLAGS=-L/usr/local/Cellar/mingw-w64/4.0.6/x86_64-w64-mingw32/lib \
  go build --ldflags '-extldflags "-static"' .

And voilà !!! Works like a charm.... I have tested the binary on Windows 10 x64 and works very well... without any dependencies.

It took a while to figure it out. Hopefully this will help other people... Maybe we should move this instructions to the Readme ? Feedback ?

I followed the above steps, but returned the error " /usr/bin/x86_64-w64-mingw32-ld: unable to find - lyara64 ". Could you help me solve it ?

from go-yara.

hillu avatar hillu commented on May 22, 2024

@Jacob-Dong The error

/usr/bin/x86_64-w64-mingw32-ld: unable to find - lyara64

tells you that the linker that is invoked for linking the final program or library does not find libyate64.a, libyara64.so or libyara64.dll in the search path for libraries (parameter -L, usually specified via LDFLAGS environment variable.)

If you built YARA from sources, it's most likely that libyara.a and libyara.so.* was generated, so the -lyara64 parameter would be wrong.

from go-yara.

N0body007 avatar N0body007 commented on May 22, 2024

lls you that the linker that is invoked for linking the final program or library does not find libyate64.a, libyara64.so or libyara64.dll in the search path for libraries (parameter -L, usually specified via LDFLAGS environment variable.)

If you built YARA from sources, it's most likely that libyara.a and libyara.so.* was generated, so the

Thanks for answering. Actually as you said, so I modified the " -lyara64 " of cgo_windows.go to "-lyara", then the error disappered.

from go-yara.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.