Programowanie w systemie UNIX/C



Specjalistyczny podręcznik Programowania w C

WstępEdytuj

  1. pierwszy przykład
    1. program z SDL
    2. Więcej o kompilacja
    3. Makra preprocessora = przetwarzanie kodu źródłowego programu przed jego kompilacją

obiektowe-cEdytuj

Objective-C – rozszerzenie języka C o możliwości obiektowe, wzorowane na Smalltalku. Objective-C przyjął drogę całkowicie odmienną od C++. Jest używany głównie w frameworku Cocoa w systemie OS X oraz w iOS.


kompilacja na Ubuntu [1]

Programowanie równoległeEdytuj

  1. CPU
    1. MIC = CPU z wieloma rdzeniami (SIMD, OpenMP) i koprocesor Xeon Phi
    2. pthreads = wątki
    3. GNU UPC
    4. ispc - Intel SPMD Program Compiler. An open-source compiler for high-performance SIMD programming on the CPU
  2. Procesor Cell i Sony PS3
  3. GPU
    1. CUDA
    2. OpenCL
    3. Cg

Biblioteki, moduły i pakietyEdytuj

Interfejs programistyczny aplikacji (ang. Application Programming Interface, API)Edytuj

  1. standardowe (libc, ...)
  2. pthreads
  3. glib wykorzystywane w GTk i Gnome


StandardyEdytuj

  • POSIX


Wskazówki POSIX dotyczące składni poleceń

numeryczneEdytuj

  1. GSL
  2. PARI (libpari)
  3. Anant -- Algorithmic 'n Analytic Number Theory by Linas Vepstas
  4. The multiple-digit approach
    1. GMP
    2. MPFR
    3. MPC
    4. Flint
    5. Arb
      1. arbcmath
  5. the multiple-component approach
    1. qd
      1. GQD = qd na GPU
  6. GPU
    1. CAMPARY = CudA Multiple Precision ARithmetic librarYMuller-Popescu-Tang presentation
  7. przenośne (ang. portable) = które nie wymagają instalacji:
    1. APR


"The multiple-digit approach can representamuch larger range of numbers, whereas the multiple-component approach has the advantage in speed."

GrafikaEdytuj

  1. Grafika[2]
    1. OpenGL oraz Glut, Glew, GLSL ....
    2. Gegl
    3. libgd = GD Graphics (Draw) Library : sudo apt-get install libgd-dev
    4. OpenCV
    5. libpng
    6. libvips
    7. openexr
    8. AOMediaCodec libavif Library for encoding and decoding .avif files ( HDR)
    9. Gdk-pixbuf Image loading library
    10. cairo, pixman
    11. Pango - is a library for laying out and rendering of text, with an emphasis on internationalization.
    12. GR is a universal framework for cross-platform visualization applications.
    13. plplot
    14. Heman is a C library of image utilities for dealing with height maps and other floating-point images.
  2. color
    1. rgb565 colors by Nic Newdigate
  3. GUI
    1. SDL
    2. gtk
    3. Glut
    4. IUP = Portable User Interface
    5. SFML = Simple and Fast Multimedia Library
    6. Nuklear - 1 file
    7. cefapi A simple example on how to use the C API in Chromium Embedded Framework created by Czarek Tomczak.
    8. Sokol: Simple STB-style cross-platform libraries for C and C++, written in C by Andre Weissflog ( floooh )
    9. Sokol GP. Minimal efficient cross platform 2D graphics painter in pure C using modern graphics API through the excellent Sokol GFX library. Sokol GP, or in short SGP, stands for Sokol Graphics Painter by Eduardo Bart edubart
    10. lcui
    11. webview
    12. raygui A simple and easy-to-use immediate-mode gui library


Wczytywanie obrazu z użyciem stb_image[3]

 

#include <stdint.h>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

int main() {
    int width, height, bpp;

    uint8_t* rgb_image = stbi_load("image.png", &width, &height, &bpp, 3);

    stbi_image_free(rgb_image);

    return 0;
}

Tworzenie obrazu z użyciem stb_image

 
#include <stdint.h>

#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#define CHANNEL_NUM 3

int main() {
    int width = 800; 
    int height = 800;

    uint8_t* rgb_image;
    rgb_image = malloc(width*height*CHANNEL_NUM);

    // Write your code to populate rgb_image here

    stbi_write_png("image.png", width, height, CHANNEL_NUM, rgb_image, width*CHANNEL_NUM);

    return 0;
}

dźwiękEdytuj

 
#include <soundio/soundio.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

static const float PI = 3.1415926535f;
static float seconds_offset = 0.0f;
static void write_callback(struct SoundIoOutStream *outstream,
        int frame_count_min, int frame_count_max)
{
    const struct SoundIoChannelLayout *layout = &outstream->layout;
    float float_sample_rate = outstream->sample_rate;
    float seconds_per_frame = 1.0f / float_sample_rate;
    struct SoundIoChannelArea *areas;
    int frames_left = frame_count_max;
    int err;

    while (frames_left > 0) {
        int frame_count = frames_left;

        if ((err = soundio_outstream_begin_write(outstream, &areas, &frame_count))) {
            fprintf(stderr, "%s\n", soundio_strerror(err));
            exit(1);
        }

        if (!frame_count)
            break;

        float pitch = 440.0f;
        float radians_per_second = pitch * 2.0f * PI;
        for (int frame = 0; frame < frame_count; frame += 1) {
            float sample = sin((seconds_offset + frame * seconds_per_frame) * radians_per_second);
            for (int channel = 0; channel < layout->channel_count; channel += 1) {
                float *ptr = (float*)(areas[channel].ptr + areas[channel].step * frame);
                *ptr = sample;
            }
        }
        seconds_offset = fmod(seconds_offset + seconds_per_frame * frame_count, 1.0);

        if ((err = soundio_outstream_end_write(outstream))) {
            fprintf(stderr, "%s\n", soundio_strerror(err));
            exit(1);
        }

        frames_left -= frame_count;
    }
}

int main(int argc, char **argv) {
    int err;
    struct SoundIo *soundio = soundio_create();
    if (!soundio) {
        fprintf(stderr, "out of memory\n");
        return 1;
    }

    if ((err = soundio_connect(soundio))) {
        fprintf(stderr, "error connecting: %s\n", soundio_strerror(err));
        return 1;
    }

    soundio_flush_events(soundio);

    int default_out_device_index = soundio_default_output_device_index(soundio);
    if (default_out_device_index < 0) {
        fprintf(stderr, "no output device found\n");
        return 1;
    }

    struct SoundIoDevice *device = soundio_get_output_device(soundio, default_out_device_index);
    if (!device) {
        fprintf(stderr, "out of memory\n");
        return 1;
    }

    fprintf(stderr, "Output device: %s\n", device->name);

    struct SoundIoOutStream *outstream = soundio_outstream_create(device);
    if (!outstream) {
        fprintf(stderr, "out of memory\n");
        return 1;
    }
    outstream->format = SoundIoFormatFloat32NE;
    outstream->write_callback = write_callback;

    if ((err = soundio_outstream_open(outstream))) {
        fprintf(stderr, "unable to open device: %s", soundio_strerror(err));
        return 1;
    }

    if (outstream->layout_error)
        fprintf(stderr, "unable to set channel layout: %s\n", soundio_strerror(outstream->layout_error));

    if ((err = soundio_outstream_start(outstream))) {
        fprintf(stderr, "unable to start device: %s\n", soundio_strerror(err));
        return 1;
    }

    for (;;)
        soundio_wait_events(soundio);

    soundio_outstream_destroy(outstream);
    soundio_device_unref(device);
    soundio_destroy(soundio);
    return 0;
}

DebugowanieEdytuj

  1. gdb
  2. backtrace
    1. libc backtrace [4]
    2. libunwind[5]
    3. Postmortem Debugging [6]
    4. nanolat [7]
    5. stck trace [8]
    6. Stack unwinding (stack trace) with GCC[9]

Dodatkowe materiałyEdytuj

ŹródłaEdytuj

  1. stackoverflow :compiling-objective-c-on-ubuntu-using-gcc
  2. stackoverflow question: graphics-library-in-c
  3. stackoverflow question: reading-an-image-file-in-c
  4. libc backtrace
  5. libunwind
  6. Postmortem Debugging by Stefan Wörthmüller
  7. nanolat by Kangmo Kim
  8. Catching Exceptions and Printing Stack Traces for C on Windows, Linux, & Mac BY: JOB VRANISH
  9. Stack unwinding (stack trace) with GCC
  10. Programowanie w języku C - Artur Pyszczuk