2026年7月26日 星期日

Library:static、shared與dynamically loaded in Linux

 

Static libraries :

    Static 程式庫用於靜態連結,把一堆 object 檔,用 ar (archiver) 包裝集合起來,檔名以 `.a' 結尾。

    優點:執行效能快,且因靜態連結,不易發生執行時找不到 library 或版本問題。

    缺點:檔案較大,維護度較低


Shared libraries :

    Shared library 程式執行起始時載入,而不是執行中用到才載入。連結階段需要有該程式庫才能進行連結。

    優點:維護彈性較好。

    soname:library 的名稱,例: libmylib.so.1 

    real name:實際放有library程式的檔案名稱,例:libmylib.so.1.0.0

    linker name:連結時的名稱,通常是不含版號,例: libmylib.so。


    使用範例:

        * Complie :

            # gcc -c -fPIC hello.c world.c

                -fPIC 產生 position-independent code。也可以用 -fpic 參數。


            # gcc -shared -Wl,-soname,libmylib.so.1 hello.o world.o -o libmylib.so.1.0.0

                -shared 表示要編譯成 shared library

                -Wl 用於參遞參數給linker,-soname與libmylib.so.1會被傳給linker處理。-soname 用來指名 soname 為 limylib.so.1

                library 會被輸出成 libmylib.so.1.0.0(real name), 若不指定 soname 的話,在編譯結連後的執行檔會以連時的library檔名為 soname,並載入他。否則是載入 soname 指定的 library 檔案。


        * 用 ln 來建立 soname 與 linker name 兩個檔案 :

            # ln -s libmylib.so1.0.0 libmylib.so.1


        * 使用 : 與 static library 同 :

            # gcc main_mylib.c libmylib.so 

            or

            # gcc main_mylib.c -L. -lmylib

            如果目錄下同時有 static 與 shared library 的話,會以 shared 為主,可以使用 -static 參數可以避免使用shared連結。


版本號:

第1碼:原介面有移除或改變,與舊版不相容時。

第2碼:有新增加介面,但相舊介面沒改變,所以與舊版本相容。

第3碼:用於程式內容的修正,介面完全沒有改變。


Dynamically loaded libraries :

    像 windows 所用的 DLL ,在使用到時才載入,編譯連結時不需要相關的 library。動態載入庫常被用於像 plug-ins 的應用.

    動態載入是透過一套 dl function來處理,預設 API如下 :

        void *dlopen(const char *filename, int flag);    //開啟載入 filename 指定的 library。

        void *dlsym(void *handle, const char *symbol);    //取得 symbol 指定的symbol name在library被載入的記憶體位址。

        int dlclose(void *handle);    //關閉dlopen開啟的handle。  

        char *dlerror(void);    //傳回最近所發生的錯誤訊息。


    使用方法:

    Compile:

        gcc test.c -ldl -o test

    執行:

        ./test


Ref:

[Linux 教學] 用gcc 自製 Library

Program Library HOWTO


Supermium(Chromium支援Window 7, XP)

 Chrome 已停止支援Windows 7,或更舊系統。


Supermium 是由 Shane Fournier 開發的自由及開放原始碼網頁瀏覽器。


介紹(From Wiki):

它是 Chromium 的一個分支,其主要特點是支援已不再受 Chromium 支援的舊版 Microsoft Windows,包括所有 Windows 10 之前的版本,最早可支援 Windows XP。


路徑:

https://github.com/win32ss/supermium

2026年7月22日 星期三

Linux Man(Manual) Page - (線上手冊)

 章節分類(Sections):

1. 可執行的程式或是 shell 指令

2. 系統呼叫(system calls,Linux 核心所提供的函數)

3. 一般函式庫函數

4. 特殊檔案(通常位於 /dev)

5. 檔案格式與協定,如 /etc/passwd

6. 遊戲

7. 雜項(巨集等,如 man(7)、groff(7))

8. 系統管理者指令(通常是管理者 root 專用的)

9. Kernel routines(非標準)


線上查詢:

    https://man7.org/linux/man-pages/index.html

Program部份:

    gcc : https://man7.org/linux/man-pages/man1/gcc.1.html

    g++: https://man7.org/linux/man-pages/man1/g++.1.html


參考網址:

    第六章、Linux 檔案與目錄管理 - 鳥哥 - 基礎學習篇 - CentOS 7.x

    善用 man 指令查詢 Linux 線上手冊(Man Page)


Visual Studio Code(VS Code) tutorial


1. install VS code

    Download the corresponding version

    https://code.visualstudio.com/download?_exp_download=fb315fc982

    sudo apt install ./<file>.deb

Ref:

    https://code.visualstudio.com/docs/setup/linux

    https://code.visualstudio.com/docs/setup/windows


2. install extension for C/C++ or other

    Start app

    Enter extensions and install: C/C++, C/C++ Extension Pack, C/C++ DevTools

Ref:

    https://code.visualstudio.com/docs/languages/cpp


3. first program(Hello World) and do debug

    Please check the under link.

    .vscode -- some setting file

        tasks.json (compiler build settings)

        launch.json (debugger settings)

        c_cpp_properties.json (compiler path and IntelliSense settings)

    Debug: 

        a. Open main.cpp in VS code.

        b. Add a breakpoint in source code

        c. Press the play button(in the right-up location),  select Debug C/C++ File in the drop-down

Ref:

    https://code.visualstudio.com/docs/cpp/config-linux


4. Debugging with Multiple Files

    Previous step will generate tasks.json. Modify task.json.

    Replease

    "${file}",

    to

    "file1.cpp",

    "file2.cpp",

Ref:

    http://antares.cs.kent.edu/~csi/blog/vscodemultiple.html



2026年7月21日 星期二

Install ImageMagick 7 in Ubuntu 22.04

Use source code

1.  Download the newest source code from official website: Source code(zip) or Source code(tar.gz)

https://github.com/ImageMagick/ImageMagick/releases

2. Decompress source codes and enter the folder

cd ImageMagick-7.1.2-27

3. Set build environment

./configure

4. build code

make

5. install ImageMagick

sudo make install

6. update dynamic shared library

sudo ldconfig /usr/local/lib



Ref:

https://imagemagick.org/install-source/#gsc.tab=0

https://www.ufans.top/index.php/archives/370/

2026年7月9日 星期四

Glad and GLFW in Ubuntu

 1. Check previous article to install GLFW


2. Download Glad(https://glad.dav1d.de)

C/C++, OpenGL, gl(Version 3.3) --> Generate --> Download Zip file


3. File tree

├── glad.c

├── include/

│   ├── KHR/

│   │   └── khrplatform.h

│   └── glad/

│       └── glad.h

└── Hello_Window.cpp


4. Hello_World.cpp(A Sample code)

------------------------------------------------------------------------

#include <glad/glad.h>

#include <GLFW/glfw3.h>

#include <iostream>


void framebuffer_size_callback(GLFWwindow* window, int width, int height) {

    glViewport(0, 0, width, height);

}


// Process escape key to close the window

void processInput(GLFWwindow *window) {

    if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {

        glfwSetWindowShouldClose(window, true);

    }

}


int main() {

    // 1. Initialize GLFW

    if (!glfwInit()) {

        std::cerr << "Failed to initialize GLFW" << std::endl;

        return -1;

    }


    // 2. Configure GLFW (Targeting OpenGL 3.3 Core Profile)

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);

    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

#ifdef __APPLE__

    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required for macOS

#endif


    // 3. Create the window object

    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Hello World", NULL, NULL);

    if (window == NULL) {

        std::cerr << "Failed to create GLFW window" << std::endl;

        glfwTerminate();

        return -1;

    }

    glfwMakeContextCurrent(window);

    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);


    // 4. Initialize GLAD before calling any OpenGL functions

    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {

        std::cerr << "Failed to initialize GLAD" << std::endl;

        return -1;

    }


    // 5. The Render Loop

    while (!glfwWindowShouldClose(window)) {

        // Input handling

        processInput(window);


        // Rendering commands (Clear screen with a dark greenish-blue color)

        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);

        glClear(GL_COLOR_BUFFER_BIT);


        // Swap buffers and poll window events

        glfwSwapBuffers(window);

        glfwPollEvents();

    }


    // 6. Clean up resources

    glfwTerminate();

    return 0;

}

------------------------------------------------------------------------


5. Build code

g++ Hello_World.cpp glad.c -I./include -lglfw -lGL -ldl -o Hello_Window