🚀 End goal: To create the same developer setup as Mac and Linux without having to rely on proprietary Microsoft Visual Studio (MSVC).
Note: You will need to run most commands as an Administrator in the Command Prompt.
Linux has apt-get by default
sudo apt-get <packageName>
Mac has Homebrew
brew install <packageName>
Windows has Chocolately
choco install <packageName>
Official Windows Terminal Instructions
The default Command Prompt sucks. 💩 It's like trying to build a house without power tools. We have technology.
Official Powershell Instructions
Set your default shell to Powershell. Don't use "Powershell Core"... that's a weird Microsoft project to try to get Linux and Mac users to switch to Powershell.
MinGW-64 is the Windows equivalent of GCC (for C) and G++ (for C++). It even comes with the gdb debugger! 😍
- GCC ➡️ GNU C Compiler
- G++ ➡️ GNU C++ Compiler
choco install mingw
choco install cmake
choco install make
Add cmake
as a command in your Environment Variables. There is a bug in the installer and it doesn't automatically create a command alias for you.
- Search for
Environment Variables
in Windows - Click on
Environment Variables
button - In the upper window (user variables) click on
New
- Variable name =
cmake
- Variable path =
C:\Program Files\CMake\bin
- Please verify this path on your own computer
- You will know this is the correct bin folder if it contains
cmake.exe
- Click
OK
andOK
to finish
Check that MinGW, Cmake, and Make are installed correctly in the Terminal:
g++ --version
>> g++.exe (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.
cmake --version
>> cmake version 3.21.3
make --version
>> GNU Make 4.3
Built for Windows32
You can even check that other GNU tools from MinGW are installed:
grep --version
>> grep (GNU grep) 3.0
gdb --version
>> GNU gdb (GDB for MinGW-W64 x86_64, built by Brecht Sanders) 10.2
bash --version
>> GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
If you forget where G++ is installed, use this:
which g++
>> /c/ProgramData/chocolatey/bin/g++
Refresh your Terminal environment variables with refreshenv
-
Download GCC 7.3.0 MinGW (SEH) - 64-bit
- Place this SFML folder somewhere safe like
C:\Users\yourname\Documents\
. Don't lose this folder! - We don't want the Visual C++ versions.
- The 32-bit version should work as well if you want to use that instead.
- Place this SFML folder somewhere safe like
-
We do NOT want to manually tell the GCC/G++ compiler where to find our SFML library. Doing this will suck because GCC/G++ and Cmake will not be able to find SFML by itself.
-
We WANT to add this SFML library as a
choco
package into our computer. -
Use this Github code to help add SFML as a
choco
package.- Download all files from this Github repository: https://github.com/jeanmimib/sfml-mingw64
git clone https://github.com/jeanmimib/sfml-mingw64.git
- Place this folder into a temporary place like
C:\Users\yourname\Documents\
. You can delete this folder afterwards. - In the terminal, go into this temporary
sfml-mingw64
folder. - Type this command. It will create a nupkg file.
choco pack
- Type this command. It will install SFML as a choco package that MingGW can easily find. The dot means "here", and since you're in the same folder as the nupkg file, choco will find it.
choco install sfml-mingw64 -s .
- Download all files from this Github repository: https://github.com/jeanmimib/sfml-mingw64
-
Check that all
choco
packages are installed correctly.choco list --local-only >> Chocolatey v0.11.2 cmake 3.21.3 cmake.install 3.21.3 make 4.3 mingw 11.2.0 sfml-mingw64 2.5.1
Refresh your Terminal environment variables with refreshenv
-
I use Visual Studio Code (VS Code), so you will need to find the equivalent in your IDE.
-
Open up your VS Code such that the working folder is your desired Project Home.
- You should see the
include
andsrc
folders. - There are no parent folders above this folder!
- You should see the
-
Install the C/C++ extension (by Microsoft) because we want all the coding power we can get.
-
Install the Cmake Tools extension (by Microsoft) if you want. This is optional because we have already installed Cmake into our system. This Cmake extension only works inside VS Code and automatically builds your project when you save your files. This extension does not
make
an.exe
file! -
‼️ Because I have the Cmake tools extension, it generates a/build
folder for me which is the same thing as/bin
.‼️ -
Open up the command palette with
ctrl + shift + p
. -
Select
edit configuration (ui)
in the search bar. -
Change the following configurations:
Configuration Name Setting Compiler Path C:/ProgramData/chocolatey/bin/g++.exe IntelliSense mode windows-gcc-x64 Include Path ${workspaceFolder}/** C:\Users\yourname\Documents\SFML-2.5.1\include C++ standard C++ 17 -
Here we have 2 include paths. (1) The project's include and (2) SFML's include that you stored in a safe place. If you eventually have more external libraries, add them in "Include Path" as a new line.
-
Your IDE IntelliSense should now be happy. The red lines should go away because now it can find the .hpp files.
We are not going to use any absolute file paths. This usually leads to errors because the compiler can't find stuff.
cmake_minimum_required(VERSION 3.10)
project(
App # Name of our application
VERSION 1.0 # Version of our software
LANGUAGES CXX) # Language that we are using
set(CMAKE_CXX_STANDARD 17)
include_directories("./include/")
find_package(SFML 2.5.1 COMPONENTS graphics window system REQUIRED)
add_executable(${PROJECT_NAME} ./src/App.cpp ./src/Draw.cpp ./src/Command.cpp ./src/main.cpp)
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-window sfml-system)
There are two ways to link .dll libraries in Windows 10.
If you want to distribute a prebuilt .exe program, you should include the .dll libraries within the program files. Otherwise, a normal user will never be able to find & link the .dll libraries. You either provide all the necessary .dll in one package, or you create a Windows installer (lmao).
-
Copy/paste all the MinGW .dll files into your project
/bin
or/build
folder which contains the prebuilt .exe program. -
For example, my .dll file location is:
C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\bin
. -
Use the search bar to filter by
.dll
and copy all. -
Paste
.dll
files
If you are a developer, you should let your computer automatically find the necessary .dll
so that you don't have to copy/paste .dll
every time to work on a new project.
-
Add the folder containing all the
.dll
libraries into your system PATH variable. -
Go into Environment Variables again. Edit the Path variable.
-
Add the location for MinGW64 .dll folder into your PATH variable. For example, my location is:
C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\bin
.
- Go into your project
/bin
or/build
folder in the Terminal - Type
cmake -G "MinGW Makefiles ..
in the Terminal - This will generate the makefiles for the MinGW compiler
- Type
make
in the Terminal - This will build your executible
- Pray that it works 🙏
- Try running the
App.exe
- Extra Notes:
- It is good practice to build outside of your source directory. This is why we tell cmake to target one directory up (..)
- After you run
cmake -G "MinGW Makefiles ..
one time, cmake will remember that you are using MinGW. So the next time you re-compile, you can just typecmake ..
- If you ever delete your makefiles, you will need to re-generate them
- VSCode Notes:
- The VSCode cmake extension can auto generate the makefiles for you if you set it up correctly in the configuration (see step 6 above). It generates each time you hit save.
- Restart your computer. Seriously, try it.
- Delete the VSCode settings (in the folder called
.vscode
), restart VSCode, and re-apply the settings. - Windows is stubborn, so restart anything you can think of.