-
Notifications
You must be signed in to change notification settings - Fork 1
Compiling with GCC
A portable and already compiled version of GCC and MinGW-w64 can be downloaded here: http://winlibs.com/
Unpack the archive somewhere. There's a folder with g++.exe. That's the c++ compiler. To use it if you have the folder open in windows explorer, press alt+d to highlight the address bar, type powershell
and press enter. A powershell window will open where you can enter the command.
Even better is to add the folder containing g++ to the PATH environment variable so you can call g++ from everywhere. How to change environment variables: https://superuser.com/questions/949560/how-do-i-set-system-environment-variables-in-windows-10
The program can be compiled with GCC 10.3.0 and MinGW-w64 with the following command.
g++ ExploreFractals.cpp -std=c++17 -s -static -m64 -O2 -ffast-math -lgdi32 -lcomdlg32 -D NDEBUG -o ExploreFractals.exe
Reasons for the parameters:
parameter | reason |
---|---|
-std=c++17 | c++ standard version 17, needed because I use a constexpr lambda |
-s | removes unnecessary things from the executable such as function names. It reduces file size by a lot. |
-static | includes every function in the executable so that it doesn't need DLLs |
-m64 | make it a 64-bit executable |
-O2 | optimize for speed (O2 gives a better result than O3 with my program) |
-ffast-math | make calculations faster at the cost of compliance with some floating point standards. Those standards don't matter for fractals because the exact bits are not important. |
-lgdi and -lcomdlg32 | libraries that should be linked to for the windows api functions used |
-D NDEBUG | compile as if #define NDEBUG is inserted as the first line. This disables debug mode. Debug mode performs many checks and gives more text output which reduces the speed of the program. |
Tip: add the parameter -march=native
for some extra speed if you don't need the program to work on older CPUs than the one you're currently using. With this parameter, GCC will freely use every instruction that your CPU has without any regard for compatibility with other CPUs. This is great for personal use.
Since version 9.0, compiling has become more difficult because the program uses nana, which is a GUI library that needs to be compiled separately and linked to. Here is a video showing the whole process: https://www.youtube.com/watch?v=viamXKOPxxY
There are 2 steps to compile ExploreFractals:
- compiling nana
- compiling ExploreFractals and linking to nana
Compiling ExploreFractals is easy once nana is working. It can be done with a single command:
g++ ExploreFractals.cpp -std=c++17 -s -static -m64 -O2 -ffast-math -fno-finite-math-only -I. -L. -lnana -lgdi32 -lcomdlg32 -D NDEBUG -o ExploreFractals.exe
First, how to compile nana:
Version 9 uses commit e52779a of Nana, which is this commit: https://github.com/cnjinhao/nana/tree/e52779aee4cd79e1b65cb59398ae28c2a87d72d0 To be safe you can download an exact commit. That is the nana source code.
Versions 10 and 11 use commit 23af07c: https://github.com/cnjinhao/nana/tree/23af07c5579724448aa3c1de6aca1a768c813fca
Compiling nana again comprises of 2 steps:
- generating a compilation script with cmake ( https://cmake.org/download/ )
- executing the compilation script
There are more ways to compile nana but this is how I do it.
Cmake is a program to generate compilation scripts for various compilers. Because nana provides a cmake file (called CMakeLists.txt), cmake can be used to generate a makefile for g++.
The file called CMakeLists.txt is the file that specifies what cmake should do. In cmake the location of the source code is the folder that contains this file. For example:
This configuration places the generated file in the subfolder cmakebuild. Click configure and choose this:
This allows you to choose the location of the compiler. Next choose the location of g++ for C++:
Then you have this:
Click Generate to generate the script. This script (called Makefile) is now in the "where to build the binaries" directory:
To execute it, simply execute the command mingw32-make
from that directory, for example in powershell:
Note: this command only works if the directory that contains mingw32-make.exe and g++.exe is in your PATH environment variable.
After the script is done, the result is the file libnana.a, which is the compiled nana library. To verify that it works you can copy it to the include directory of the nana source code and compile nana's hello world program, which is:
#include <nana/gui.hpp>
#include <nana/gui/widgets/label.hpp>
#include <nana/gui/widgets/button.hpp>
int main()
{
using namespace nana;
//Define a form.
form fm;
//Define a label and display a text.
label lab{fm, "Hello, <bold blue size=16>Nana C++ Library</>"};
lab.format(true);
//Define a button and answer the click event.
button btn{fm, "Quit"};
btn.events().click([&fm]{
fm.close();
});
//Layout management
fm.div("vert <><<><weight=80% text><>><><weight=24<><button><>><>");
fm["text"]<<lab;
fm["button"] << btn;
fm.collocate();
//Show the form
fm.show();
//Start to event loop process, it blocks until the form is closed.
exec();
}
Place that code in a file test.cpp so you have this:
Then you can compile it with this command:
g++ test.cpp -I. -L. -lnana -std=c++17 -lcomdlg32 -lgdi32
Reasons for the parameters:
parameter | reason |
---|---|
-I. | tell the compiler to look for included files in this directory (the dot) |
-L. | tell the compiler to look for libraries in this directory |
-lnana | link to libnana.a (.a files with a name that starts with lib can be linked like this) |
-std=c++17 | required because nana was compiled with c++17 and g++ uses c++14 by default |
-lcomdlg and -lgdi32 | libraries that are almost if not always required to create a windows GUI program |
This yields the program a.exe and if you execute it you see this:
This means that libnana.a works and compilation of nana is done.
Place libnana.a AND the nana include files in the same folder as the source code of ExploreFractals, like this:
Then you can compile ExploreFractals with this command:
g++ ExploreFractals.cpp -std=c++17 -s -static -m64 -O2 -ffast-math -fno-finite-math-only -I. -L. -lnana -lgdi32 -lcomdlg32 -D NDEBUG -o ExploreFractals.exe
Reasons for the parameters:
parameter | reason |
---|---|
-std=c++17 | c++ standard version 17, needed because I use a constexpr lambda |
-s | removes unnecessary things from the executable such as function names. It reduces file size by a lot. |
-static | includes every function in the executable so that it doesn't need DLLs |
-m64 | make it a 64-bit executable |
-O2 | optimize for speed (O2 gives a better result than O3 with my program) |
-ffast-math | make calculations faster at the cost of compliance with some floating point standards. Those standards don't matter for fractals because the exact bits are not important. |
-fno-finite-math-only | disable finite math only, enabled by ffast-math. This program must be able to do calculations that involve infinity. |
-I. | tell the compiler to look for included files in this directory (the dot) |
-L. | tell the compiler to look for libraries in this directory |
-lnana | link to libnana.a (.a files with a name that starts with lib can be linked like this) |
-lgdi and -lcomdlg32 | libraries that should be linked to for the windows api functions used |
-D NDEBUG | compile as if #define NDEBUG is inserted as the first line. This disables debug mode. Debug mode performs many checks and gives more text output which reduces the speed of the program. |