greatscottgadgets / hackrf

low cost software radio platform
https://greatscottgadgets.com/hackrf/
GNU General Public License v2.0
6.6k stars 1.53k forks source link

Updated instructions to compile on Windows #1413

Open raffaeler opened 9 months ago

raffaeler commented 9 months ago

This is not a request. I just decided to post here the detailed and updated instructions to compile all the sources on a Windows machine. I added boring and obvious details which may anyway be useful for people not used to compile C++ sources on Windows.

I may create a pull-request, if Michael expresses the interest.

How to compile HackRF on Windows

The first requirement (for this guide) is to install Visual Studio.

No license is required because Microsoft provides the "build tools" version (command line compiler) or the "community edition" (Visual IDE and compiler) which are both free.

The second requirement is to install CMake from: https://cmake.org/download/. I tested my guide with the version 3.28.3. When installing, I recommend to select the option that adds CMake into the user's path environment variable.

Note: It's totally possible to use Visual Studio 2022 as well. You can also use the VS2022 IDE with the VS2019 C++ libraries. The Visual Studio installer gives you the possibility to install both.

After installing these two requirements, open a developer command prompt (a shortcut prepared by the Visual Studio installer). Be advised to open the command prompt only after completing the installation, as the environment variables are never updated in existing processes like the command prompt.

Required libraries

Download and extract the following libraries inside the c:\libs folder.

You can use any folder, just carefully change the root folder in the batch files as well.

Important: download the x64 version of all the dependencies

fftw does not provide a pre-built static library. This means that you will need to:

Create the stub lib for fftw

From the developer command prompt for Visual Studio 2019. Be advised to use the same version of the command prompt of the compiler that you are going to use.

lib /def:libfftw3f-3.def /MACHINE:X64

These are not required, but just in case:

lib /def:libfftw3-3.def /MACHINE:X64
lib /def:libfftw3l-3.def /MACHINE:X64

Clone the HackRF repository

The first step is to clone the sources from the GitHub repository.

git clone https://github.com/greatscottgadgets/hackrf

Then create the build folder inside the hackrf root folder:

cd hackrf\host
md build
cd build

The newly created build folder is used to host the CMake generated artifacts.

Now create the following two batch files inside the build folder.

Batch 1: mymake.cmd

cmake .. -G "Visual Studio 16 2019" -A "x64" -DCMAKE_BUILD_TYPE=Release -DLIBUSB_INCLUDE_DIR=c:\libs\libusb-1.0.27\include -DLIBUSB_LIBRARIES=c:\libs\libusb-1.0.27\VS2019\MS64\static\libusb-1.0.lib -DTHREADS_PTHREADS_INCLUDE_DIR=c:\libs\pthreads-w32-2-9-1-release\Pre-built.2\include -DTHREADS_PTHREADS_WIN32_LIBRARY=c:\libs\pthreads-w32-2-9-1-release\Pre-built.2\lib\x64\pthreadVC2.lib -DFFTW_INCLUDES=c:\libs\fftw-3.3.5-dll64 -DFFTW_LIBRARIES=c:\libs\fftw-3.3.5-dll64\libfftw3f-3.lib

This batch file does uses Visual Studio 2019 to compile the solution. The options are:

Option Meaning
-G "Visual Studio 16 2019" Use VS2019
-A "x64" Compile for x64
Previous CMake releases used Win64 string
-DCMAKE_BUILD_TYPE=Release Compile a Release version
-DLIBUSB_INCLUDE_DIR=c:\libs\libusb-1.0.27\include Include folder for libusb
-DLIBUSB_LIBRARIES=c:\libs\libusb-1.0.27\VS2019\MS64\static\libusb-1.0.lib Reference the libusb static library
-DTHREADS_PTHREADS_INCLUDE_DIR=c:\libs\pthreads-w32-2-9-1-release\Pre-built.2\include Include folder for pthreads
-DTHREADS_PTHREADS_WIN32_LIBRARY=c:\libs\pthreads-w32-2-9-1-release\Pre-built.2\lib\x64\pthreadVC2.lib Reference the pthreads static library
-DFFTW_INCLUDES=c:\libs\fftw-3.3.5-dll64 Include folder for fftw
-DFFTW_LIBRARIES=c:\libs\fftw-3.3.5-dll64\libfftw3f-3.lib Reference the fftw stub library

Batch 2: mycompile.cmd

msbuild hackrf.sln -p:Configuration=Release
copy .\libhackrf\src\Release\hackrf.dll .\hackrf-tools\src\Release
copy c:\libs\fftw-3.3.5-dll64\libfftw3f-3.dll .\hackrf-tools\src\Release

This batch does the following:

Compile

Run mymake.cmd to generate the CMake artifacts. Ensure you don't see any errors before proceding.

Finally run mycompile.cmd to compile the sources and copy the required libraries to the tools target folder.

Generated binaries

All the binaries are generated in these folders:

straithe commented 9 months ago

I suggest opening a PR to update the documentation!

raffaeler commented 9 months ago

I suggest opening a PR to update the documentation!

I can do it, but I saw the backlog and decided to wait for better times :)

martinling commented 9 months ago

It should be possible to do this with a bit less work!

It's possible to install the dependencies in one command using vcpkg:

vcpkg install --triplet=x64-windows libusb fftw3 pthreads

Unfortunately it still seems to be necessary to tell cmake where everything was installed, with something like:

cmake ..
    -DLIBUSB_INCLUDE_DIR=C:/vcpkg/installed/x64-windows/include/libusb-1.0 
    -DLIBUSB_LIBRARIES=C:/vcpkg/installed/x64-windows/lib/libusb-1.0.lib
    -DFFTW_INCLUDES=C:/vcpkg/installed/x64-windows/include
    -DFFTW_LIBRARIES=C:/vcpkg/installed/x64-windows/lib/fftw3f.lib
    -DTHREADS_PTHREADS_INCLUDE_DIR=C:/vcpkg/installed/x64-windows/include
    -DTHREADS_PTHREADS_WIN32_LIBRARY=C:/vcpkg/installed/x64-windows/lib/pthreadvc3.lib

(I'd like to get things tweaked so that cmake will discover these paths automatically if the relevant vcpkg variables are set.)

But then it should be possible to just cmake --build . --config Release

See also @dmaltsiniotis' work on #1391.

raffaeler commented 9 months ago

@martinling My first wish is changing all the libraries to be statically linked so that you don't have to bring dlls with you. That's not hard but I did not want to recompile fftw (apparently they do not provide static libs) or make changes to the cmake files.

Moji14 commented 8 months ago

These updated instructions are a very nice source of information for newcomers. Apart from this open issue, is that information part of any document? It seems to differ from https://hackrf.readthedocs.io/en/latest/

raffaeler commented 8 months ago

These updated instructions are a very nice source of information for newcomers. Apart from this open issue, is that information part of any document? It seems to differ from https://hackrf.readthedocs.io/en/latest/

I wrote it by myself. Years ago I had to build myself the dependencies. Also, previous versions of VS required different steps. Nowadays, all the major C++ compilers adhere to the recent standards and building x-plat has become simpler. I just hope this can be useful to the community.

FrankBolleri commented 1 day ago

Hello everyone and excuse me for the question... but isn't there somewhere hackrf.dll and the hackrf tools already compiled for Windows in the latest version (2024.02.1?), ready to be used? I would like to use them without having to create them myself with CMake etc... I would have expected to find them in the Releases Assets here in GitHub, but I didn't find them.

Thanks in advance Francesco

dmaltsiniotis commented 1 day ago

Hi @FrankBolleri, as it turns out, yes there is. You can download pre-compiled Windows binaries of the DLL and tools from the nightly build process. The most recent run is located here: https://github.com/greatscottgadgets/hackrf/actions/runs/11778239935 and the link to download is at the bottom of the page:

image

Cheers, -DM

FrankBolleri commented 1 day ago

Hi @FrankBolleri, as it turns out, yes there is. You can download pre-compiled Windows binaries of the DLL and tools from the nightly build process. The most recent run is located here: https://github.com/greatscottgadgets/hackrf/actions/runs/11778239935 and the link to download is at the bottom of the page:

image

Cheers, -DM

Thank you very much! Have a nice day!