Skip to content

Instantly share code, notes, and snippets.

@mtvee
Created January 1, 2021 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvee/f41caba1ab080639fe6eec31828f34ff to your computer and use it in GitHub Desktop.
Save mtvee/f41caba1ab080639fe6eec31828f34ff to your computer and use it in GitHub Desktop.
quick a clean project setup with cmake and git submodules

Project setup

Quick and clean project setup with cmake and git submodules. We are using SFML and glm as examples of binary and header only dependencies. I've messed around with conan, hunter, vcpkg and CPM.cmake and I just get more of a headache than I had before so I just use this method. I've tested this in clion and visual studio.

The directories are setup thusly...

ROOT
    .gitignore
    CMakeLists.txt
    modules/.git-keep
    src/main.cpp

Get git sorted first

git init
git add .
git commit -a -m "initial mess"

Add sfml as a submodule

> git submodule add https://github.com/SFML/sfml modules/sfml

NOTE: be sure to add modules\sfml to .gitignore

CmakeLists.txt

cmake_minimum_required(VERSION 3.8)
project(Foo)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

set(SFML_STATIC_LIBRARIES TRUE)
add_subdirectory(modules/sfml)

add_executable(foo src/main.cpp)
target_link_libraries(foo sfml-system sfml-graphics)

GLM

glm is a header only lib so it's a bit different

git submodule add https://github.com/g-truc/glm modules/glm

NOTE: be sure to add modules\glm to .gitignore

then in CMakeLists.txt we just need to be able to find it

...
include_directories(${PROJECT_SOURCE_DIR}/modules/glm/)    

add_executable(foo src/main.cpp)
...

main.cpp

#include <SFML/Window.hpp>
#include <glm/glm.hpp>

int main()
{
glm::vec3 foo(0.f);
sf::Window window(sf::VideoMode(800, 600), "My window");

    // run the program as long as the window is open
    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        sf::Event event;
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
        }
    }

    return 0;
}

CmakeLists.txt

cmake_minimum_required(VERSION 3.8)

project(Foo)

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

set(SFML_STATIC_LIBRARIES TRUE)
add_subdirectory(modules/sfml)

include_directories(${PROJECT_SOURCE_DIR}/modules/glm/)

add_executable(foo src/main.cpp)
target_link_libraries(foo sfml-system sfml-graphics)

.gitignore

# ide junk
# clion
.idea/
cmake-build*/
# visual studio
.vs/
out/

# our stuff
build/
bin/
lib/

# ignore all submodule sources
modules/sfml
modules/glm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment