Khi làm việc với C, nếu có một số hàm nào đó bạn thường xuyên sử dụng và không muốn viết đi viết lại nhiều lần, hãy tạo ra một file thư viện chứa các hàm đó. Bài viết này sẽ giúp bạn làm điều đó!

During work with C, if you have some function often use and don’t want write multiple times, you just create a file library contain functions. This post will help you do it.

Nội dung – Table of content
Tạo file thư viện – Create library file
Dùng thư viện tự tạo – Using self library

Tạo file thư viện – Create library file

Để tạo được file thư viện, các bạn làm không khác gì với việc bạn viết code bình thường. Bạn tạo file với đuôi là .h và viết các hàm bạn muốn vào. Ví dụ để tạo thư viện mylibrary.h chứa các hàm tính giai thừa factorial, hàm hoán vị swap, hàm sắp xếp nhanh quicksort.

To create the library file, you make same the code normal. You create file type .h contain functions. Example create library mylibrary.h contain funtions factorial, swap and quicksort.

long factorial(int n) {
     
    int i;
    long result = 1;
    for (i = 2; i <= n; i++){
        result *= i;
    }
     
    return result;
}
 
void swap(int *a, int *b){
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
 
void quickSort(int *a, int l, int r) {
    srand(time(NULL)); 
    int key = a[l + rand() % (r-l+1)]; 
    int i = l, j = r;
  
    while(i <= j) {
        while(a[i] < key) i++;      
        while(a[j] > key) j--;     
        if(i <= j) {
            if (i < j)
                swap(&a[i], &a[j]);
            i++;
            j--;
        }
    }
 
    if (l < j) quickSort(a, l, j); 
    if (i < r) quickSort(a, i, r);
}

Dùng thư viện tự tạo – Using self library

Bây giờ chúng ta chỉ việc dùng thư viện đó. Việc dùng cũng cần lưu ý, có 2 cách dùng.
Cách 1: Nếu bạn để thư viện vừa tạo cùng thư mục với các file code khác thì trong lời gọi thư viện sẽ là #include “mylibrary.h”. Trong trường hợp này mỗi khi bạn dùng thư viện bạn cần copy file này cùng các file code.

Cách 2: Nếu bạn không muốn rắc rối như cách 1, hãy copy file vừa tạo vào /usr/include với Linux, trên windows thì copy vào thư mục chứa các thư viện, đối với dev-C là ở C:\Program Files\Dev-Cpp\MinGW32\include. Khi này bạn dùng như mọi thư viện khác bằng cách #include <mylibrary.h> .

Ở file minh họa dưới đây mình để cùng thư mục.

Now we just use library. There are two ways to use:

Case 1: You create library in same forder with code file, You call it by #include “mylibrary.h”. In case, every time you use it, you have to copy it into the same forder with code file.

Case 2: If you don’t want trouble same case 1, you can copy library file into /usr/include with Linux, on windows you copy into forder contain libraries, with dev-C, it is C:\Program Files\Dev-Cpp\MinGW32\include. And you just use it same libraries by #include <mylibrary.h> .

In code below, I put them in same forder.

#include <stdio.h>
#include "mylibrary.h"
 
int main (int argc, char *argv[])
{
 
    printf("5! = %ld\n",    factorial(5));
 
    int i, arr[] = { 40, 10, 100, 90, 20, 25 };
    quickSort(arr, 0, 5);
    printf("after sort, array is: \n");
    for (i=0; i<6; i++)
        printf ("%d ", arr[i]);
    printf("\n");
     
    return 0;
}
create library in C