This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.
It's hard to write software that runs correctly and efficiently. So once a program works in one environment, you don't want to repeat much of the effort if you move it to a different compiler or processor or operating system. Ideally, it should need no changes whatsoever. Kernighan & Pike - The Practice of Programming.
- 1. Windows Portability
- 2. macOS portability
- 2.1. Base SDK and Deployment Target
- 2.2. Multiple versions of Xcode
- 2.3. macOS ARM
- 2.4. macOS 32bits
- 3. Linux portability
- 4. Configurations
- 5. Installers
In Quick start and New project we used Visual Studio 2019 to generate the NAppGUI solution. We understand by portability the ability to compile and debug our programs on platforms other than those that were written, without touching a single line of code. We understand by platform the combination of a compiler and a CPU architecture. For example,
v142_x64 refers to Visual Studio 2019 and Intel 64bits. In (Figure 1) we see the different steps in the code migration process.
- Working copy: A copy of the project source code must exist on each machine. Normally this will be done through a version control system such as Subversion or Git, although it could also be updated from an external memory.
- CMake: CMake will create a build project (or solution) from the source code using the scripts in
/prjdirectory of the SDK distribution. This will be done fully automatically.
- Build: Using Visual Studio, Xcode or GCC the solution will be compiled and the libraries and executables included in it will be generated.
- Run/Debug: Binaries can already be executed and debugged on the target platform.
1. Windows Portability
We can use any version of Visual Studio from 2005 to compile under Windows (Table 1). As we saw on Quick start the first thing we have to do is launch CMake over the source code:
|Visual Studio 2019||v142_x64 (x86)||Vista|
|Visual Studio 2017||v141_x64 (x86)||Vista|
|Visual Studio 2015||v140_x64 (x86)||Vista|
|Visual Studio 2013||v120_x64 (x86)||Vista|
|Visual Studio 2012||v110_x64 (x86)||Vista|
|Visual Studio 2010||v100_x64 (x86)||XP|
|Visual Studio 2008||v90_x64 (x86)||XP|
|Visual Studio 2005||v80_x64 (x86)||XP|
- Where is the source code:
- Where to build the binaries:
The first time CMake will ask what version of Visual Studio we want to use (Figure 2) and that, obviously, we must have installed in the machine in question. The architecture selected by default can be changed from the same dialog (Figure 3).
Once we press
[Open Project] , we will have the project ready to compile. From Visual Studio
Build->Build Solution and debug
Debug->Start Debugging (Figure 4).
To change the Visual Studio version we have to clear the CMake cache
File->Delete Cacheor change the destination directory: Where to build the binaries. It is possible to have different solutions from the same code in several directories, each with its own version of Visual Studio.
Starting with Visual Studio 2010, a dissociation occurs between the editor and the compiler. The term Plaform Toolset identifies the compiler, that can be used with more modern IDEs. If we do not indicate anything, CMake will use the toolset included by default in each version of VS, but it can be changed from Optional toolset to use. For example, we can combine
Visual Studio 15 2017 with the VS2013 toolset
v120_xp (Figure 5).
1.1. Visual C++ Redistributable
By default, Visual Studio dynamically links the functions of the C library, which causes the
.exe may not work on machines that do not have the
VC++ DLLs (Figure 6). This forces applications to include a copy of
VCRUNTIME.dll, ... or install the famous Visual C ++ Redistributable packages to ensure the application can run smoothly.
NAppGUI uses a small set of the C library, and directly accesses the Windows API whenever possible (more details in Standard C library). For this reason, all applications created with NAppGUI perform a static link (option
/MT) of the stdlib functions that need, avoiding dependencies at the expense of slightly increasing (a few Kb) the size of the final executable. This ensures that applications will run smoothly on all Windows machines without the need for additional DLLs and without having to install the VC++ Redistributable.
NAppGUI applications do not require the Visual C++ Redistributable. Neither do they use the MFC "Microsoft Foundation Classes" nor the .NET platform.
1.2. WindowsXP support
From VS2012, the Platform Toolset generates executables that are not compatible with WindowsXP. If we want our applications to run on this system, we must select the alternative toolset finished in
v80, that do support XP (Figure 7).
Support for WindowsXP has been permanently removed in Visual Studio 2019. Platform Toolset v142_xp does not exist.
NAppGUI does not work on Windows prior to XP or with Visual Studio prior to 2005.
1.3. SSE support
With the Pentium III, Intel incorporated an additional set of instructions for floating-point operations called SSE Streaming SIMD Extensions. This allows optimizing mathematical calculations at the cost of losing compatibility, since applications that use SSE will not work on Pentium II or earlier models. In NAppGUI the toolset have been reserved
v90_x86 of Visual Studio to create applications compatible with older processors (Table 2). From
v100_x86, SSE2 will be used in all the toolset.
|v80_x86||x87 (no SSE)||Pentium II/AMD K6|
|v90_x86||SSE||Pentium III/AMD Duron|
|v100_x86||SSE2||Pentium IV/AMD Sempron|
|v110_x86||SSE2||Pentium IV/AMD Sempron|
SSE support is only disabled in 32-bit (x86) architectures. All 64-bit (x64) CPUs incorporate SSE2.
2. macOS portability
Compiling on Apple iMac, macBook and macMini follows the same philosophy as in Windows, we will only need CMake and Xcode from 3.2.6 version (Table 3). As we have already done in Windows, we open CMake and assign the directories:
|Xcode 12.2||sdk11_00_x64 (arm)||Big Sur|
|Xcode 9.4.1||sdk10_13_x64||High Sierra|
|Xcode 7.3.1||sdk10_11_x64||El Capitan|
|Xcode 5.1.1||sdk10_8_x64||Mountain Lion|
|Xcode 3.2.6||sdk10_6_x64 (x86)||Snow Leopard|
- Where is the source code:
- Where to build the binaries:
/Users/fran/nappgui_build. Directory where the generated code will go.
[Configure]. The first time CMake will ask us to select the compiler. Unlike Visual Studio, it only offers one option for Xcode (Figure 8). We accept, press
[Generate] and then
When opening the Xcode solution
(NAppGUI.xcodeproj), we see the different projects that compose it, including Die and Dice. We select Die in the upper left drop-down and then press
Product->Run (Figure 9). This will compile the program and launch it in debug mode, where we can establish breakpoints to inspect the stack and the value of the variables.
2.1. Base SDK and Deployment Target
Each year, Apple releases a new version of macOS, which comes with a new SDK and the Xcode update that includes the SDK.
Base SDK is the version included in each new major version of Xcode, which matches the latest version of the macOS system on the market.
Apple has a much more restrictive policy than Microsoft regarding the applications compatibility with earlier versions of the operating system. By default, a program compiled with SDK 10.14 (macOS Mojave) will not work in the immediately previous macOS High Sierra (Figure 10).
To avoid this problem, and for applications to work in older macOS, the Deployment Target parameter exists. When using it, a macro will be activated thatavoid the new features of the Base SDK. This will allow the program to run in old versions at the expense, of course, of not having access to the latest iMac features. You can select the Deployment Target through CMake options list, after pressing
[Configure] (Figure 11).
Xcode 8 considers Deployment Target to be less than 10.9 obsolete (Figure 12). Use Xcode 7 if you want support for Mac OSX 10.8 Mountain Lion and earlier.
2.2. Multiple versions of Xcode
We have already seen that CMake only offers an option for Xcode, although it is possible to have several versions on the same machine, each within its own
Xcode.app bundle. There will always be a default Xcode in the system (the most recent) but it can be changed using the
xcode-select -p /Applications/Xcode.app/Contents/Developer
sudo xcode-select -s /Applications/Xcode8.app/Contents/Developer
sudo xcode-select -r
You must press CMake
[Generate]every time you use
xcode-selectso that your project updates the compiler change.
2.3. macOS ARM
In November 2020 Apple launches its new line of desktop and laptop computers (iMac, macBook and macMini) based on the Apple M1 processor with ARM architecture (Figure 13). Although they are capable of running programs compiled for Intel x64 using the Rosetta 2 program (Figure 14), the ideal would be to compile our applications for the new architecture in order to optimize the executables as much as possible.
NAppGUI supports the Apple ARM architecture through the
sdk11_00_arm package. You just have to select the
arm64 option in
CMAKE_ARCHITECTURE (Figure 15).
You can compile M1 architecture from Intel x64 machines, but you will not be able to debug executables.
M1 architecture is only available for Big Sur system (macOS 11.0) and later.
2.4. macOS 32bits
Since the macOS High Sierra version, Apple has declared obsolete the architecture of 32 bits, issuing warnings to the users in the case of detecting executables
i386 (Figure 16). From Xcode 10, you can not compile in this architecture (Figure 17).
Support for 32-bit applications has definitely removed in macOS Catalina, which only allows 64-bit applications to run.
This makes some sense since all models of Intel iMac incorporate 64-bit processors, with the exception of a few 2006 models in white polycarbonate that mounted the 32-bit Intel Core Duo (Figure 18). These iMac admitted at most the Mac OSX 10.6 Snow Leopard, being fundamental requirement from 10.7 Lion, the have a 64-bit CPU. To compile without problems in 32bits you have to use, at most, Xcode 6 (Figure 19).
3. Linux portability
In the Linux world, the
gcc (Table 4) compiler and the
make tool are used to generate the binaries, but there is no "official" development environment. To carry out an elementary configuration of our team in Ubuntu, type the following commands:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Development tools sudo apt-get install gcc sudo apt-get install g++ sudo apt-get install make sudo apt-get install subversion sudo apt-get install cmake cmake-qt-gui // Development libraries sudo apt-get install libgtk-3-dev sudo apt-get install libglu1-mesa-dev freeglut3-dev mesa-common-dev sudo apt-get install libcurl4-openssl-dev // GTK Inspector (Ctrl+D when debugging) gsettings set org.gtk.Settings.Debug enable-inspector-keybinding true // Check system libraries version pkg-config --modversion gtk+-3.0 3.24.20 pkg-config --modversion libcurl 7.68.0
|GCC 9.3||gcc9_gtk3_x64||GTK 3.24.20||Ubuntu 20.04 LTS|
|GCC 7.5||gcc7_gtk3_x64||GTK 3.22.30||Ubuntu 18.04 LTS|
|GCC 5.4||gcc5_gtk3_x64||GTK 3.18.9||Ubuntu 16.04 LTS|
|GCC 4.8.2||gcc4_gtk3_x64||GTK 3.10.8||Ubuntu 14.04 LTS|
|GCC 8.3.0||gcc8_gtk3_arm||GTK 3.24.5||Raspbian 10 Buster|
|GCC 6.3.0||gcc6_gtk3_arm||GTK 3.22.11||Raspbian 9.1 Strech|
The work methodology does not change at all. We open CMake, select the directories, press
[Configure] and then
[Generate] (Figure 20).
In Linux we must use the CMake
Unix Makefiles generator, which will create a series of
Makefile scripts in
/home/fran/nappgui_build (or the binary directory that we have selected). These
Makefile contain the necessary commands to compile and link all the projects of the solution. We open a terminal:
1 2 3 4 5 6 7 8
cd /home/fran/nappgui_build make [ 15%] Building C object games/die/CMakeFiles/Die.dir/dgui.c.o [ 30%] Building C object games/die/CMakeFiles/Die.dir/die.c.o [ 45%] Building C object games/die/CMakeFiles/Die.dir/draw/ddraw.c.o [ 60%] Building C object games/die/CMakeFiles/Die.dir/resgen/all.c.o [ 75%] Linking CXX executable Debug/Die [ 95%] Built target Die
Once the compilation is finished, we can launch the executables directly from the terminal:
And if we are real hackers, we will be able to debug the programs directly from
gdb (Figure 21).
1 2 3
gdb ./die/Debug/Die (gdb) run ...
Unlike Windows and macOS, in Linux you can create user interfaces using different libraries (or toolkits) with GTK and Qt being the two most famous. NAppGUI uses GTK+3 for the graphical part in Linux (see Dependencies), although in the future it is possible that it also incorporates support for Qt, since it is the basis of the KDE desktop system, used in Kubuntu.GTK+3 is found naturally in Ubuntu and many other distributions, although to compile it is necessary to install the developer version.
3.2. Multiple versions of GCC
Although each Linux distribution incorporates a "canonical" version of GCC, it is possible to have several installed on the same machine and switch between them in a similar way as we did on the mac with
xcode-select. For this we will use the Linux command
update-alternatives. We assume we are on Ubuntu 18.04 LTS:
gcc --version gcc 7.5.0
sudo apt-get install gcc-6 g++-6
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 60 --slave /usr/bin/g++ g++ /usr/bin/g++-7 sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-6 50 --slave /usr/bin/g++ g++ /usr/bin/g++-6
1 2 3 4 5
sudo update-alternatives --set gcc /usr/bin/gcc-6 gcc --version gcc 6.5.0 g++ --version g++ 6.5.0
1 2 3 4 5
sudo update-alternatives --auto gcc gcc --version gcc 7.5.0 g++ --version g++ 7.5.0
3.3. Eclipse CDT
Working directly with the terminal will give us the highest level of freedom possible to coding. Just returning to the console and typing
make everything necessary will be re-compiled. However, the use of
GDB directly will be quite tedious, so we will make the leap to Eclipse CDT. This environment will allow us to program with a methodology similar to that of Visual Studio and Xcode: Situate breakpoints, inspect the stack and variables, search for files within the code directory, multiple editions, mass searches, etc.
It is not imperative to use Eclipse. You can choose the editor/IDE you prefer.
The only difference is that we must use the generator
Eclipse CDT4 - Unix Makefiles in CMake (Figure 22) that in addition to
Makefile will create the files
.project needed to import the project into Eclipse.
We open Eclipse and we do
File->Import->Existing Projects into Workspace. A dialog box will appear where we indicate the build directory that we have configured in CMake (
/home/fran/nappgui_build). Eclipse will open the project placing a tree with all the files on the left and we'll compile with
Project->Build All. At the time of debugging (Die in this case) we will create a profile from
Run->Debug Configurations->C/C++ Application. We press
[Search Project...] and select Die from the drop-down list. Finally we press
[Debug] to debug the application interactively (Figure 23).
Some interesting Eclipse CDT options under
- Run/Debug->Launching->Terminate and Relaunch while launching.
3.4. Linux 32bits
To compile 32bit applications from an Ubuntu 64bits system it is necessary to install the package
sudo apt-get install gcc-multilib
But now there are problems to perform cross-compilation that includes the GTK+ library, so it will not be possible to use the same development machine to generate in both architectures, as in Windows. Console applications or libraries that do not access GTK can be compiled in 32bits from a 64bit computer.
It is not possible to compile in 32bits from an Ubuntu system of 64bit applications that use GTK+3.
3.5. ARM architecture
The architecture ARM Advanced RISC Machine is the predominant in the market of embedded devices such as smartphones and tablets. Currently, NAppGUI does not offer support for the development of iOS/Android mobile applications, but for other types of boards that support versions of Linux ARM "desktop", such as the Raspberry PI with Raspbian. To carry our code to Raspberry Pi you have to follow the same steps as in Ubuntu Linux (Figure 24). Both distributions are based on Debian, so we have GCC, CMake and Eclipse directly through
An NAppGUI application can be compiled in three different configurations, depending on the level of debugging we need.
- Debug: Includes debug information in the binaries and code optimizations are not performed. It is the version for the developer.
- Release: The debug information is removed and all possible optimizations are made. It is the version for the user.
- ReleaseWithAssert: It is the Release version, but leaving the statements active Asserts. It is aimed at the end user, but in cases where it is necessary to obtain detailed information of possible anomalies, at the cost of a decrease in the overall performance of the program.
Both Visual Studio and Xcode are multi-configuration environments, that is, we can alternate between them directly from the editor itself. In Visual Studio we have a drop-down at the top of the editor (Figure 25).
In Xcode is a bit more hidden. We make
Product->Scheme->Edit Scheme. A pop-up window will appear. We select
Run->Info->Build Configuration (Figure 26).
Unix make does not support multiple configurations. This forces us to enter the property
CMAKE_BUILD_CONFIG (Figure 27) to set the configuration in CMake before generating the
Makefile. We will have to press again
[Generate] for the new configuration to take effect.
The last step, once a program has been created and refined, will be to create a package/installer that contains the executable and its possible dependencies, to distribute it comfortably to the final user. This packaging process has been automated in NAppGUI thanks to CPack, a utility included with CMake (Figure 28).
- Activate the
CMAKE_PACKAGEoption in the CMake panel.
- Set in
CMAKE_PACKAGE_PATHthe directory where the generated packages are to be saved.
- Select the type of packaging through the variable
CMAKE_PACKAGE_GENthat admits three values:
TGZ: Creates a compressed package
.tar.gzthat can be opened with any
.ziputility installed on the computer (Figure 29).
DragNDrop: (MacOS only). Create a
dmgdrive with a bundle
.appand a link to the
/Applications(Figure 30) folder.
NSIS: (Windows only). Create an installer using the Nullsoft scriptable install system (Figure 31) utility. It is necessary to have installed the software NSIS (only to create the installer). The user who receives the package does not need to have such software installed.
Each package includes a copy of the license with which we are going to distribute the application. The content of it can be edited from the file
/res/license.txt included in the resources directory of each project.
Packaging a program is time consuming. Activate
CMAKE_PACKAGEonly when we are going to distribute. During the development process it is advisable to deactivate the checkbox.