Cross-platform C SDK logo

Cross-platform C SDK

Generate NAppGUI binaries

❮ Back
Next ❯
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.

In Quick start we already saw how to download, compile and run the examples from the source code. In this chapter and the next, we'll dive deeper into the build process and cross-platform portability. The entire build system of NAppGUI centers around a single src/CMakeLists.txt (Listing 1) script. It will define a solution with several related projects, as well as their dependencies.

Listing 1: src/CMakeLists.txt
 
cmake_minimum_required(VERSION 2.8.12)
project(NAppGUI)

# NAppGUI Build Scripts
get_filename_component(ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR} PATH)
include(${ROOT_PATH}/prj/CMakeNAppGUI.cmake)

# Static libraries
staticLib("sewer" "sewer" "" NRC_NONE)
staticLib("osbs" "osbs" "sewer" NRC_NONE)
staticLib("core" "core" "osbs" NRC_NONE)
staticLib("geom2d" "geom2d" "core" NRC_NONE)
staticLib("draw2d" "draw2d" "geom2d" NRC_NONE)
staticLib("osgui" "osgui" "draw2d" NRC_NONE)
staticLib("gui" "gui" "draw2d" NRC_EMBEDDED)
staticLib("inet" "inet" "core" NRC_NONE)
staticLib("osapp" "osapp" "osgui;gui" NRC_NONE)

# Executables
desktopApp("Fractals" "demo/fractals" "osapp" NRC_EMBEDDED)
desktopApp("HelloWorld" "demo/hello" "osapp" NRC_EMBEDDED)
desktopApp("HelloCpp" "demo/hellocpp" "osapp" NRC_EMBEDDED)
...

generateSolution()

1. Generate static libraries

By default, the CMakeLists.txt will create the static version of the NAppGUI libraries. If you want to use NAppGUI externally in your projects, you just have to follow these steps:

 
git clone --depth 1 https://github.com/frang75/nappgui_src.git
cd nappgui_src

// Windows
cmake -S ./src -B ./build
cmake --build ./build --config Debug
cmake --install ./build --prefix ./install --config Debug

// macOS
cmake -G Xcode -S ./src -B ./build
cmake --build ./build --config Debug
cmake --install ./build --prefix ./install --config Debug

// Linux
cmake -S ./src -B ./build -DCMAKE_BUILD_CONFIG=Debug
cmake --build ./build -j 4
cmake --install ./build --prefix ./install --config Debug

In the install folder you will have the binaries and headers:

 
install
  |
  +-- inc
  |    |
  |    +-- core
  |   ...   |
  |         +-- array.h
  |        ...
  +-- lib
  |    |
  |    +-- v143_x64
  |         |
  |         +-- Debug
  |              |
  |              +-- core.lib
  |             ...
  +-- bin
       |
       +-- v143_x64
            |
            +-- Debug
                 |
                 +-- Bode.exe
                ...
  • In /install/inc you will find the header files of each library.
  • In /install/lib you will find the static libraries (.lib, .a).
  • In /install/bin you will find the sample executables.
  • v143_x64 identifies the compiler and architecture. See Compilers and IDEs for more information about supported compilers, platforms, and architectures.
  • Debug is one of three possible configurations: Debug, Release, ReleaseWithAssert. Configurations
If you don't want to compile the sample applications, remove the desktopApp lines from the script.

2. Generate dynamic libraries

To generate the dynamically linked versions (.dll, .so, .dylib) of NAppGUI, edit CMakeLists.txt, replacing the staticLib commands with dynamicLib . Once this is done, compile and install using cmake in the same way as in the static case.

 
cmake_minimum_required(VERSION 2.8.12)
project(NAppGUI)

# NAppGUI Build Scripts
get_filename_component(ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR} PATH)
include(${ROOT_PATH}/prj/CMakeNAppGUI.cmake)

# Dynamic libraries
dynamicLib("sewer" "sewer" "" NRC_NONE)
dynamicLib("osbs" "osbs" "sewer" NRC_NONE)
dynamicLib("core" "core" "osbs" NRC_NONE)
dynamicLib("geom2d" "geom2d" "core" NRC_NONE)
dynamicLib("draw2d" "draw2d" "geom2d" NRC_NONE)
dynamicLib("osgui" "osgui" "draw2d" NRC_NONE)
dynamicLib("gui" "gui" "draw2d" NRC_EMBEDDED)
dynamicLib("inet" "inet" "core" NRC_NONE)
dynamicLib("osapp" "osapp" "osgui;gui" NRC_NONE)

# Executables
desktopApp("Fractals" "demo/fractals" "osapp" NRC_EMBEDDED)
desktopApp("HelloWorld" "demo/hello" "osapp" NRC_EMBEDDED)
desktopApp("HelloCpp" "demo/hellocpp" "osapp" NRC_EMBEDDED)
...

generateSolution()

After installation:

  • In /install/inc you will find the header files of each library.
  • In /install/lib the symbol import libraries of the .dll (.lib) will be stored, only on Windows.
  • In /install/bin the dynamic libraries .dll, .so, .dylib will be stored together with the example executables.
  •  
    
    11-Dec-22  19:42    <DIR>          .
    11-Dec-22  19:42    <DIR>          ..
    11-Dec-22  19:42    <DIR>          res
    11-Dec-22  19:42           217,088 osgui.dll
    11-Dec-22  19:42            93,184 casino.dll
    11-Dec-22  19:41           119,808 osbs.dll
    11-Dec-22  19:42           241,152 core.dll
    11-Dec-22  19:42           100,864 osapp.dll
    11-Dec-22  19:42           118,784 inet.dll
    11-Dec-22  19:42           222,208 draw2d.dll
    11-Dec-22  19:42           250,880 gui.dll
    11-Dec-22  19:42           329,728 geom2d.dll
    11-Dec-22  19:41           229,376 sewer.dll
    11-Dec-22  19:42           462,336 DrawImg.exe
    11-Dec-22  19:42           188,416 DrawHello.exe
    11-Dec-22  19:42           129,024 DrawBig.exe
    11-Dec-22  19:42           483,840 GuiHello.exe
    11-Dec-22  19:42           138,240 HelloCpp.exe
    11-Dec-22  19:42           123,904 HelloWorld.exe
    11-Dec-22  19:42           132,096 Die.exe
    11-Dec-22  19:42           124,928 Dice.exe
    11-Dec-22  19:42           152,576 Col2dHello.exe
    11-Dec-22  19:42           126,464 Bricks.exe
    11-Dec-22  19:42           153,088 Products.exe
    11-Dec-22  19:42           159,744 Bode.exe
    11-Dec-22  19:42           127,488 Fractals.exe
    11-Dec-22  19:42           128,000 UrlImg.exe
    

If you are going to use these libraries in third-party projects, not generated using CMakeLists.txt, you must previously define these macros in order for the symbols to be imported correctly.

 
#define OSAPP_IMPORT
#define OSGUI_IMPORT
#define DRAW2D_IMPORT
#define GEOM2D_IMPORT
#define CORE_IMPORT
#define OSBS_IMPORT
#define SEWER_IMPORT
#define GUI_IMPORT
#define INET_IMPORT

3. More about CMakeLists.txt

NAppGUI simplifies the use of CMake by providing high-level functions, located in the /prj folder of the distribution. The CMakeLists.txt defines a solution where different libraries and related executables coexist through dependencies. After being processed by CMake this script will create, in the /build folder, the build projects for each platform (VisualStudio, Xcode, Make). Within the script we will work with essentially four commands:

In the following example we define a solution that contains a dynamic library and two applications, one desktop and one command line. Both make use of (depend on) said dynamic library and NAppGUI (static libraries) for the graphical interface and cross-platform support.

CMakeLists.txt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#------------------------------------------------------------------------------
# CMake build script
# Copyright (C) 2023 - PhysicsLab
# See LICENSE.txt for details
#------------------------------------------------------------------------------
cmake_minimum_required(VERSION 2.8.12)
project(PhysicsSimulator)

# NAppGUI Build Scripts
get_filename_component(ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR} PATH)
include(${ROOT_PATH}/prj/CMakeNAppGUI.cmake)

# NAppGUI static libraries
staticLib("sewer" "sewer" "" NRC_NONE)
staticLib("osbs" "osbs" "sewer" NRC_NONE)
staticLib("core" "core" "osbs" NRC_NONE)
staticLib("geom2d" "geom2d" "core" NRC_NONE)
staticLib("draw2d" "draw2d" "geom2d" NRC_NONE)
staticLib("osgui" "osgui" "draw2d" NRC_NONE)
staticLib("gui" "gui" "draw2d" NRC_EMBEDDED)
staticLib("osapp" "osapp" "osgui;gui" NRC_NONE)
staticLib("inet" "inet" "core" NRC_NONE)

# User dynamic library
dynamicLib("physics" "physics" "geom2d" NRC_NONE)

# Exes
desktopApp("PhysicsSim" "phsim" "osapp;physics" NRC_EMBEDDED)
commandApp("PhysicsTest" "phtest" "core;physics" NRC_NONE)

generateSolution()
  • Line 6: Minimum required version of CMake.
  • Line 7: Name of the project or solution.
  • Lines 10-11: Includes the NAppGUI CMake scripts, located in /prj.
  • Lines 14-22: Generate the static libraries that make up NAppGUI.
  • Line 25: Generates a dynamic library with the user's own functions.
  • Line 28: Generates a desktop application.
  • Line 29: Generates an application by command line.
  • Line 31: Processes the solution. This command should be the last one in the script.

4. Why nine independent libraries?

NAppGUI provides full cross-platform support at various levels. It is not necessary to create an application with a graphical interface to take advantage of the advantages it offers us in terms of code portability. We can develop powerful server-oriented command line back-end applications. Depending on the level of assistance that each project requires, we can choose to link these libraries. More information in NAppGUI API.

  • Sewer: Basic types, assertions, Unicode, math functions, wrapper on top of the C standard library.
  • Osbs: Operating system services. Portable API on files, directories, processes, threads, memory, etc.
  • Core: Commonly used non-graphical utilities. Memory auditor, data structures, strings, streams, regular expressions, resources, etc.
  • Geom2D: 2D geometry. Transformations, vectors, polygons, collisions, etc.
  • Draw2D: Portable vector drawing API, images and fonts. It can be used in command line applications, since it is possible to draw in memory and export to a file or transmit over the network.
  • osgui: Low-level access to the user interface elements (widgets or controls) of each operating system. It is not documented and it is not recommended to use it directly.
  • Gui: Composer of user interfaces. Use osgui to render.
  • OSApp: Implements the message loop of a desktop application. Only use it to build applications from scratch. If you only need to create windows in an existing application, gui will be the top-level dependency.
  • INet: Use it if your application is going to use Internet protocols like HTTP. Valid for command line or desktop applications.
❮ Back
Next ❯