Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated instructions to compile on Windows #1413

Open
raffaeler opened this issue Feb 11, 2024 · 6 comments
Open

Updated instructions to compile on Windows #1413

raffaeler opened this issue Feb 11, 2024 · 6 comments
Labels
documentation documentation improvement or addition

Comments

@raffaeler
Copy link

raffaeler commented Feb 11, 2024

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:

  • generate the stub .lib (see next paragraph)
  • always bring the dll file within the folder of the exe tools.

##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:

  • Compiles all the source files:
    • hackrf.lib, hackrf.dll, hackrf_static.lib
      • The static library is not used by default, therefore we need to copy the hackrf.dll to the tools target binary folder
    • All the exe tools
  • Copy the hackrf.dll into the tools target binary folder
  • Copy the libfftw3f-3.dll into the tools target binary folder

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:

  • hackrf\host\build\libhackrf\src\Release

  • hackrf\host\build\hackrf-tools\src\Release

@raffaeler raffaeler added the documentation documentation improvement or addition label Feb 11, 2024
@straithe
Copy link
Member

I suggest opening a PR to update the documentation!

@raffaeler
Copy link
Author

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
Copy link
Member

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
Copy link
Author

@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
Copy link

Moji14 commented Feb 20, 2024

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation documentation improvement or addition
Projects
None yet
Development

No branches or pull requests

5 participants
@martinling @raffaeler @straithe @Moji14 and others