Search…

Build Thư Viện SDL2 (Simple DirectMedia Layer)

03/11/20203 min read
Hướng dẫn build thư viện SDL2 - Simple DirectMedia Layer) trên Linux và macOS.

Chuẩn bị source code cho SDL2

Vào trang web https://www.libsdl.org/download-2.0.php, tiến hành download file ở phần Source Code:, trong trang web có thể lựa chọn 1 trong 2 loại file nén sau, đều chứa nội dung như nhau:

  • SDL2-2.0.12.zip
  • SDL2-2.0.12.tar.gz

Sau khi download, giải nén được nội dung như sau:

SDL 2.0.5

Build thư viện SDL2

Mở Terminal, thực hiện các lệnh như dưới đây để build thư viện SDL2.

Chạy lên bên dưới để sinh ra makefile.

./configure

Sau khi có file makefile, tiến hành chạy lệnh sau để build mã nguồn C/C++.

make

Dưới đây là một số log khi build với lệnh log.

NguyenNghiasMBP:SDL2-2.0.5 nguyennghia$ make
/bin/sh build-scripts/mkinstalldirs build
mkdir -p -- build
/bin/sh build-scripts/updaterev.sh
  CC     build/SDL.lo
  CC     build/SDL_assert.lo
  CC     build/SDL_error.lo
  CC     build/SDL_hints.lo
  CC     build/SDL_log.lo
  CC     build/SDL_atomic.lo
/Users/nguyennghia/Downloads/SDL2-2.0.5/src/atomic/SDL_atomic.c:92:23: warning: 'OSAtomicCompareAndSwap32Barrier' is deprecated: first deprecated in macOS 10.12 - Use
      atomic_compare_exchange_strong() from  instead [-Wdeprecated-declarations]
    return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
                      ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libkern/OSAtomicDeprecated.h:518:9: note: 
      'OSAtomicCompareAndSwap32Barrier' has been explicitly marked deprecated here
bool    OSAtomicCompareAndSwap32Barrier( int32_t __oldValue, int32_t __newValue, volatile int32_t *__theValue );
        ^
/Users/nguyennghia/Downloads/SDL2-2.0.5/src/atomic/SDL_atomic.c:123:23: warning: 'OSAtomicCompareAndSwap64Barrier' is deprecated: first deprecated in macOS 10.12 - Use
      atomic_compare_exchange_strong() from  instead [-Wdeprecated-declarations]
    return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
                      ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/libkern/OSAtomicDeprecated.h:645:9: note: 
      'OSAtomicCompareAndSwap64Barrier' has been explicitly marked deprecated here
bool    OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue,
        ^
2 warnings generated.
  CC     build/SDL_spinlock.lo
  CC     build/SDL_audio.lo
  CC     build/SDL_audiocvt.lo
  CC     build/SDL_audiodev.lo
  CC     build/SDL_audiotypecvt.lo
  CC     build/SDL_mixer.lo
  CC     build/SDL_wave.lo
  CC     build/SDL_cpuinfo.lo
  CC     build/SDL_dynapi.lo
  CC     build/SDL_clipboardevents.lo
  CC     build/SDL_dropevents.lo
  CC     build/SDL_events.lo
  CC     build/SDL_gesture.lo
  CC     build/SDL_keyboard.lo
  CC     build/SDL_mouse.lo
  CC     build/SDL_quit.lo
  CC     build/SDL_touch.lo
  CC     build/SDL_windowevents.lo
  CC     build/SDL_rwops.lo
  CC     build/SDL_haptic.lo
  CC     build/SDL_gamecontroller.lo
  CC     build/SDL_joystick.lo
  CC     build/e_atan2.lo
  CC     build/e_log.lo
  CC     build/e_pow.lo
  CC     build/e_rem_pio2.lo
  CC     build/e_sqrt.lo
  CC     build/k_cos.lo
  CC     build/k_rem_pio2.lo
  CC     build/k_sin.lo
  CC     build/k_tan.lo

Nếu không có lỗi trong quá trình build nghĩa thư viện đã được build thành công.

Sau đó tiến hành gõ lệnh dưới đây để cài đặt.

make install

Thông thường sau khi chạy lệnh trên, các file lib của SDL sẽ nằm ở hai đường dẫn là:

  • usr/local/include/SDL/: đường dẫn chứa các file định nghĩa header của thư viện.
  • usr/local/lib/: đường dẫn chứa các file thư viện liên kết tĩnh (.a ở Linux và macOS) và thư viện liên kết động (.so ở Linux và .dylib ở macOS).

Kiểm tra việc cài đặt thư viện

Sau khi cài đặt thư viện, cần kiểm tra thư viện đã cài đặt thành công hay chưa.

Chuyển đến đường dẫn /usr/local/include và kiểm tra có tồn tại thư mục SDL2 hay không? Nếu có thì tiếp tục chuyển vào trong thư mục đó, sau đó liệt kê (lệnh ls) để xem những file header của thư viện SDL2.

NguyenNghiasMBP:SDL2 nguyennghia$ ls
SDL.h                   SDL_gamecontroller.h    SDL_opengl.h                    SDL_revision.h          SDL_test_fuzzer.h
SDL_assert.h            SDL_gesture.h           SDL_opengl_glext.h              SDL_rwops.h             SDL_test_harness.h
SDL_atomic.h            SDL_haptic.h            SDL_opengles.h                  SDL_scancode.h          SDL_test_images.h
SDL_audio.h             SDL_hints.h             SDL_opengles2.h                 SDL_shape.h             SDL_test_log.h
SDL_bits.h              SDL_joystick.h          SDL_opengles2_gl2.h             SDL_stdinc.h            SDL_test_md5.h
SDL_blendmode.h         SDL_keyboard.h          SDL_opengles2_gl2ext.h          SDL_surface.h           SDL_test_random.h
SDL_clipboard.h         SDL_keycode.h           SDL_opengles2_gl2platform.h     SDL_system.h            SDL_thread.h
SDL_config.h            SDL_loadso.h            SDL_opengles2_khrplatform.h     SDL_syswm.h             SDL_timer.h
SDL_cpuinfo.h           SDL_log.h               SDL_pixels.h                    SDL_test.h              SDL_touch.h
SDL_egl.h               SDL_main.h              SDL_platform.h                  SDL_test_assert.h       SDL_types.h
SDL_endian.h            SDL_messagebox.h        SDL_power.h                     SDL_test_common.h       SDL_version.h
SDL_error.h             SDL_mouse.h             SDL_quit.h                      SDL_test_compare.h      SDL_video.h
SDL_events.h            SDL_mutex.h             SDL_rect.h                      SDL_test_crc32.h        begin_code.h
SDL_filesystem.h        SDL_name.h              SDL_render.h                    SDL_test_font.h         close_code.h

Tiếp theo là chuyển đến đường dẫn /usr/local/lib/ và kiểm tra các lib của SDL2 đã được cài đặt chưa? Ví dụ trên macOS sẽ là các file sau:

ibSDL2-2.0.0.dylib
libSDL2.dylib
libSDL2.la
libSDL2_test.a
libSDL2main.a

Nếu thoả mãn hai điều kiện trên thì xem như build và cài đặt thành công thư viện SDL2.

* Xem nguồn bài viết tại: https://eitguide.net/huong-dan-build-thu-vien-sdl2-simple-directmedia-layer/

IO Stream

IO Stream Co., Ltd

30 Trinh Dinh Thao, Hoa Thanh ward, Tan Phu district, Ho Chi Minh city, Vietnam
+84 28 22 00 11 12
developer@iostream.co

383/1 Quang Trung, ward 10, Go Vap district, Ho Chi Minh city
Business license number: 0311563559 issued by the Department of Planning and Investment of Ho Chi Minh City on February 23, 2012

©IO Stream, 2013 - 2024