Skip to content

Commit

Permalink
Change folder structure, move global set to targets
Browse files Browse the repository at this point in the history
Adding each file to the cmake file is no longer needed
update scripts and readme
add compile_commands.json support
  • Loading branch information
mortinger91 committed Feb 26, 2023
1 parent 506bda9 commit 00abad5
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ x64/*
*.user
*.db
*.suo
build/
build/
27 changes: 27 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"intelliSenseMode": "linux-clang-x64",
"compileCommands": "${workspaceFolder}/build/debug/compile_commands.json"
},
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"intelliSenseMode": "macos-clang-arm64",
"compileCommands": "${workspaceFolder}/build/debug/compile_commands.json"
}
],
"version": 4
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
{
"label": "build_cmake",
"type": "shell",
"command": "${workspaceFolder}/ProjectFolder/scripts/cmake_build.sh",
"command": "${workspaceFolder}/ProjectFolder/scripts/build_cmake.sh",
"problemMatcher": [],
"group": {
"kind": "build",
Expand Down
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ COPY ProjectFolder /ProjectFolder
WORKDIR /

# Running Cmake build script, all cmake files are generated
RUN ProjectFolder/scripts/cmake_build.sh
RUN ProjectFolder/scripts/build_cmake.sh

# Building the project using the build files generated by Cmake
RUN ProjectFolder/scripts/make_debug.sh

WORKDIR /ProjectFolder/build/debug
WORKDIR /build/debug/tests

# The command that is run by default when the container starts
CMD ["ctest", "--output-on-failure"]
Expand Down
33 changes: 13 additions & 20 deletions ProjectFolder/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,28 @@ cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
# Project
set(PROJECT_DESCRIPTION "Template CMake C++")
set(ONLINE_REPOSITORY "https://github.com/mortinger91/cpp-cmake-template")
project("cpp-cmake-template"
project(
"cpp-cmake-template"
DESCRIPTION ${PROJECT_DESCRIPTION}
HOMEPAGE_URL ${ONLINE_REPOSITORY})
HOMEPAGE_URL ${ONLINE_REPOSITORY}
)

include(cmake/StandardSettings.cmake)

set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set (CMAKE_POSITION_INDEPENDENT_CODE ON)

# C
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# C++
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include_directories (${CMAKE_SOURCE_DIR})

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/int")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/lib")

set(LIB_TARGET "Library")
set(SUBMODULES_TARGET "Submodules")
include_directories (${CMAKE_SOURCE_DIR}/src)

add_executable (${PROJECT_NAME} src/main.cpp)
add_subdirectory(src)
target_link_libraries(
${PROJECT_NAME}
PRIVATE
${PROJECT_NAME}_LIB
)

add_subdirectory(src/binary)
add_subdirectory(src/lib)
add_subdirectory(src/submodules)
add_subdirectory(tests)
6 changes: 3 additions & 3 deletions ProjectFolder/cmake/StandardSettings.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Default settings
#
# set(CMAKE_CXX_VISIBILITY_PRESET hidden)
# set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash

echo "Started cmake_build.sh, building CMAKE files..."
# cd ProjectFolder
echo "Started building CMAKE files..."
# Clearing build folder if it exists
rm -r build &> /dev/null
# This command also generates clang compile commands here:
# ProjectFolder/build/debug/compile_commands.json
cmake -SProjectFolder -Bbuild/debug -DCMAKE_BUILD_TYPE=Debug
echo "Finished building CMAKE files..."
echo "Finished building CMAKE files!"
39 changes: 39 additions & 0 deletions ProjectFolder/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Add any new directory that you add in ProjectFolder/src here
file(GLOB_RECURSE sources utility/*.cpp)
add_library(${PROJECT_NAME}_LIB ${sources})

target_compile_definitions(
${PROJECT_NAME}_LIB
PRIVATE
# Add compile definitions here
)

target_compile_features(
${PROJECT_NAME}_LIB
PRIVATE
c_std_11
cxx_std_17
)

target_compile_options(
${PROJECT_NAME}_LIB
PRIVATE
${DEFAULT_COMPILER_OPTIONS}
)

target_link_options(
${PROJECT_NAME}_LIB
PRIVATE
${DEFAULT_LINKER_OPTIONS}
)

target_link_libraries(
${PROJECT_NAME}_LIB
PRIVATE
# Add libraries to link to the binary here
)

set_target_properties(
${PROJECT_NAME}_LIB
PROPERTIES ENABLE_EXPORTS ON
)
31 changes: 0 additions & 31 deletions ProjectFolder/src/binary/CMakeLists.txt

This file was deleted.

23 changes: 0 additions & 23 deletions ProjectFolder/src/lib/CMakeLists.txt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <iostream>

#include "src/lib/utility.h"
#include "utility/utility.h"

int main()
{
Expand Down
Empty file.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion ProjectFolder/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
enable_testing()

# test_utility
add_executable(test_utility test_utility.cpp)
target_link_libraries(test_utility ${LIB_TARGET})
target_link_libraries(test_utility ${PROJECT_NAME}_LIB)
add_test(utility ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_utility)
2 changes: 1 addition & 1 deletion ProjectFolder/tests/test_utility.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "test.h"

#include "../src/lib/utility.h"
#include "utility/utility.h"

void testHelloWorld()
{
Expand Down
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Template for a multi platform CMake C++ project that includes:
<h2>Set up project:</h2>
Execute script:

```./ProjectFolder/scripts/cmake_build.sh```<br>
```./ProjectFolder/scripts/build_cmake.sh```<br>
or execute task "build_cmake" in VSCode.
<h2>Build and Run:</h2>
Execute script:
Expand All @@ -30,7 +30,6 @@ or execute task "run_tests" in VSCode.
To run tests in a Docker container:<br>
```docker compose down --volumes --rmi all```<br>
```docker compose up```
<h2>Add a new file:</h2>
- Create the new file in the desired folder.<br>
- Add file.cpp in the CMakeLists.txt file (the one in the same folder as the file).<br>
- If adding a new test file, also add add_test() in the tests/CMakeLists.txt file.
<h2>Notes:</h2>
- When creating a new folder in ProjectFolder/src<br>
also add its name in ProjectFolder/src/CMakeLists.txt<br>

0 comments on commit 00abad5

Please sign in to comment.