본문 바로가기

Programming Languages/C++

[ C++ ] 오픈소스 GLFS 라이브러리 사용

# dnf -y install gcc-c++ git cmake
# dnf -y install wayland-protocols-devel wayland-devel libxkbcommon-devel libX11-devel xorg-x11-server-devel
# dnf -y install libXrandr-devel libXinerama-devel libXcursor-devel libXi-devel mesa-libGL-devel

mkdir -p /app/study/cpp/ch8

cd /app/study/cpp/ch8

git init

git branch -m main

cat <<'EOF' > /app/study/cpp/ch8/.gitignore
# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app

CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
CMakeUserPresets.json

build
[Bb][Uu][Ii][Ll][Dd]/
EOF

git submodule add https://github.com/glfw/glfw.git external/glfw

# CMake에 작성해 두어서 안해도 문제는 없음
git submodule update --init --recursive

cat <<'EOF' > /app/study/cpp/ch8/main.cpp
#include <iostream>
#include <GLFW/glfw3.h>

int main()
{
	GLFWwindow *window;

	if(!glfwInit())
	{
		fprintf( stderr, "Failed to initialize GLFW\n" );
		exit( EXIT_FAILURE );
	}
    
	window = glfwCreateWindow( 300, 300, "Gears", NULL, NULL );
	if(!window)
	{
		fprintf( stderr, "Failed to open GLFW window\n" );
		glfwTerminate();
		exit( EXIT_FAILURE );
	}

	while( !glfwWindowShouldClose(window) )
	{
		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwTerminate();

	return EXIT_SUCCESS;
}
EOF

cat <<'EOF' > /app/study/cpp/ch8/CMakeLists.txt
# 타 환경에서 빌드할 수 있는 최소 버전 명을 기입
cmake_minimum_required(VERSION 3.13.4)

project(testGLFW)

add_executable(${PROJECT_NAME} main.cpp)

# DOWNLOAD ALL THE SUBMODULES
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
    # Update submodules as needed
    option(GIT_SUBMODULE "Check submodules during build" ON)
    if(GIT_SUBMODULE)
        message(STATUS "Submodule update")
        execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive
                        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                        RESULT_VARIABLE GIT_SUBMOD_RESULT)
        if(NOT GIT_SUBMOD_RESULT EQUAL 0)
            message(FATAL_ERROR "git submodule update --init failed with ${GIT_SUBMOD_RESULT}, please checkout submodules")
        endif()        
    endif()
endif()

# CHECK ALL THE SUBMODULES
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/external/glfw/CMakeLists.txt")
    message(FATAL_ERROR "The glfw submodule was not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules")
endif()

add_subdirectory(external/glfw)

target_include_directories(${PROJECT_NAME} PUBLIC external/glfw/include)

target_link_directories(${PROJECT_NAME} PRIVATE external/glfw/src)

target_link_libraries(${PROJECT_NAME} glfw)
EOF

mkdir -p /app/study/cpp/ch8/build

cat <<'EOF' > /app/study/cpp/ch8/configure.sh
#! /bin/sh

# GLFW 문서를 작성하도록 하는것을 비활성화
# 빌드 시간을 단축시킴
cmake -DGLFW_BUILD_DOCS=OFF -S . -B build
EOF

cat <<'EOF' > /app/study/cpp/ch8/configure.sh
#! /bin/sh

# GLFW 문서를 작성하도록 하는것을 비활성화
# 빌드 시간을 단축시킴
cmake -DGLFW_BUILD_DOCS=OFF -S . -B build
EOF

cat <<'EOF' > /app/study/cpp/ch8/build.sh
#! /bin/sh

cd build ; make
EOF

cat <<'EOF' > /app/study/cpp/ch8/run.sh
#! /bin/sh

cd build ; ./testGLFW
EOF

chmod u+x /app/study/cpp/ch8/*.sh

/app/study/cpp/ch8/configure.sh

/app/study/cpp/ch8/build.sh

# 그래픽 환경에서 실행이되는데
# 도커 환경에서 Windows, Mac 호스트에 연결하여
# 실행하는 방법은 확인이 필요
/app/study/cpp/ch8/run.sh