generated from AfonsoCMSousa/CPP-Template
Initial commit
This commit is contained in:
commit
90f66dedcd
10
.gitignore
vendored
Normal file
10
.gitignore
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
*.a
|
||||||
|
*.log
|
||||||
|
*.so
|
||||||
|
*.swp
|
||||||
|
|
||||||
|
/build/
|
||||||
|
/.cache/
|
||||||
|
/.DS_store
|
||||||
|
/.env
|
||||||
|
/imgui.ini
|
||||||
188
CMakeLists.txt
Normal file
188
CMakeLists.txt
Normal file
@ -0,0 +1,188 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
project(CPP_TEMPLATE VERSION 0.1.0 LANGUAGES C CXX)
|
||||||
|
|
||||||
|
# Set C++ standard
|
||||||
|
set(CMAKE_CXX_STANDARD 23)
|
||||||
|
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
# Set C standard
|
||||||
|
set(CMAKE_C_STANDARD 17)
|
||||||
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
set(CMAKE_C_EXTENSIONS OFF)
|
||||||
|
|
||||||
|
# Export compile commands for IDE support (clangd, etc.)
|
||||||
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
|
# Create symlink to compile_commands.json in project root for LSP
|
||||||
|
if(CMAKE_EXPORT_COMPILE_COMMANDS)
|
||||||
|
add_custom_target(symlink_compile_commands ALL
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E create_symlink
|
||||||
|
${CMAKE_BINARY_DIR}/compile_commands.json
|
||||||
|
${CMAKE_SOURCE_DIR}/compile_commands.json
|
||||||
|
COMMENT "Creating symlink to compile_commands.json in project root"
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Set output directories
|
||||||
|
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||||
|
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||||
|
|
||||||
|
# Build type defaults
|
||||||
|
if(NOT CMAKE_BUILD_TYPE)
|
||||||
|
set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "Build type" FORCE)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Options
|
||||||
|
option(ENABLE_SANITIZERS "Enable address and undefined behavior sanitizers" ON)
|
||||||
|
option(ENABLE_STATIC_ANALYSIS "Enable static analysis warnings" ON)
|
||||||
|
set(OUTPUT_NAME "" CACHE STRING "Name of the output executable (defaults to project name)")
|
||||||
|
|
||||||
|
# Gather source files (avoid GLOB_RECURSE - explicitly list files is better practice)
|
||||||
|
# But keeping it for template flexibility
|
||||||
|
file(GLOB_RECURSE PROJECT_SOURCES
|
||||||
|
${CMAKE_SOURCE_DIR}/source/*.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/source/*.c
|
||||||
|
)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE LIBRARY_SOURCES
|
||||||
|
${CMAKE_SOURCE_DIR}/libraries/*.cpp
|
||||||
|
${CMAKE_SOURCE_DIR}/libraries/*.c
|
||||||
|
)
|
||||||
|
|
||||||
|
# Don't glob headers from include/ as sources (they should only be included)
|
||||||
|
file(GLOB_RECURSE PROJECT_HEADERS
|
||||||
|
${CMAKE_SOURCE_DIR}/include/*.hpp
|
||||||
|
${CMAKE_SOURCE_DIR}/include/*.h
|
||||||
|
${CMAKE_SOURCE_DIR}/libraries/*.hpp
|
||||||
|
${CMAKE_SOURCE_DIR}/libraries/*.h
|
||||||
|
${CMAKE_SOURCE_DIR}/source/*.hpp
|
||||||
|
${CMAKE_SOURCE_DIR}/source/*.h
|
||||||
|
)
|
||||||
|
|
||||||
|
# Combine all sources
|
||||||
|
set(ALL_SOURCES ${PROJECT_SOURCES} ${LIBRARY_SOURCES})
|
||||||
|
|
||||||
|
# Determine executable name
|
||||||
|
if(OUTPUT_NAME)
|
||||||
|
set(EXECUTABLE_NAME ${OUTPUT_NAME})
|
||||||
|
else()
|
||||||
|
set(EXECUTABLE_NAME ${PROJECT_NAME})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Create executable
|
||||||
|
add_executable(${EXECUTABLE_NAME} ${ALL_SOURCES})
|
||||||
|
|
||||||
|
# Set target properties
|
||||||
|
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
|
||||||
|
OUTPUT_NAME ${EXECUTABLE_NAME}
|
||||||
|
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Include directories - use target-specific commands
|
||||||
|
target_include_directories(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
${CMAKE_SOURCE_DIR}/include
|
||||||
|
${CMAKE_SOURCE_DIR}/libraries
|
||||||
|
)
|
||||||
|
|
||||||
|
# Compiler-specific flags
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
/W4 # Warning level 4
|
||||||
|
/permissive- # Standards conformance
|
||||||
|
/Zc:__cplusplus # Correct __cplusplus macro
|
||||||
|
/Zc:inline # Remove unreferenced COMDAT
|
||||||
|
/WX- # Don't treat warnings as errors by default
|
||||||
|
)
|
||||||
|
|
||||||
|
if(ENABLE_STATIC_ANALYSIS)
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE /analyze)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# MSVC debug flags
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE /Zi /Od)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
else() # GCC/Clang
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Wpedantic
|
||||||
|
-Wshadow
|
||||||
|
-Wconversion
|
||||||
|
-Wsign-conversion
|
||||||
|
-Wuninitialized
|
||||||
|
-Wunused
|
||||||
|
-Werror=return-type
|
||||||
|
-Wcast-align
|
||||||
|
-Wformat=2
|
||||||
|
-Wnull-dereference
|
||||||
|
)
|
||||||
|
|
||||||
|
# Additional warnings for static analysis
|
||||||
|
if(ENABLE_STATIC_ANALYSIS)
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
-Wcast-qual
|
||||||
|
-Wdouble-promotion
|
||||||
|
-Wold-style-cast
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Sanitizers (Debug builds)
|
||||||
|
if(ENABLE_SANITIZERS AND CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
# Check if sanitizers are available
|
||||||
|
include(CheckCXXCompilerFlag)
|
||||||
|
check_cxx_compiler_flag("-fsanitize=address" HAS_ASAN)
|
||||||
|
check_cxx_compiler_flag("-fsanitize=undefined" HAS_UBSAN)
|
||||||
|
|
||||||
|
if(HAS_ASAN AND HAS_UBSAN)
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
-fsanitize=address,undefined,leak
|
||||||
|
-fno-omit-frame-pointer
|
||||||
|
-g
|
||||||
|
)
|
||||||
|
target_link_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
-fsanitize=address,undefined,leak
|
||||||
|
)
|
||||||
|
message(STATUS "Sanitizers enabled: address, undefined, leak")
|
||||||
|
else()
|
||||||
|
message(WARNING "Sanitizers requested but not available - skipping")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Optimization flags for Release
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Release")
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
-O3
|
||||||
|
-march=native
|
||||||
|
-DNDEBUG
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Debug flags
|
||||||
|
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||||
|
target_compile_options(${EXECUTABLE_NAME} PRIVATE
|
||||||
|
-O0
|
||||||
|
-g3
|
||||||
|
-ggdb
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
# Print configuration summary
|
||||||
|
message(STATUS "=== Configuration Summary ===")
|
||||||
|
message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
|
||||||
|
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
|
||||||
|
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
|
||||||
|
message(STATUS "C Standard: ${CMAKE_C_STANDARD}")
|
||||||
|
message(STATUS "Executable Name: ${EXECUTABLE_NAME}")
|
||||||
|
message(STATUS "Sanitizers: ${ENABLE_SANITIZERS}")
|
||||||
|
message(STATUS "Static Analysis: ${ENABLE_STATIC_ANALYSIS}")
|
||||||
|
message(STATUS "Compiler: ${CMAKE_CXX_COMPILER_ID} ${CMAKE_CXX_COMPILER_VERSION}")
|
||||||
|
message(STATUS "============================")
|
||||||
|
|
||||||
|
# Optionally, enable testing
|
||||||
|
# enable_testing()
|
||||||
|
# add_subdirectory(tests)
|
||||||
256
build.sh
Executable file
256
build.sh
Executable file
@ -0,0 +1,256 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Colors
|
||||||
|
RED='\033[0;31m'
|
||||||
|
GREEN='\033[0;32m'
|
||||||
|
YELLOW='\033[1;33m'
|
||||||
|
BLUE='\033[0;34m'
|
||||||
|
CYAN='\033[0;36m'
|
||||||
|
MAGENTA='\033[0;35m'
|
||||||
|
NC='\033[0m' # No Color
|
||||||
|
BOLD='\033[1m'
|
||||||
|
|
||||||
|
# Unicode symbols
|
||||||
|
CHECK="✓"
|
||||||
|
CROSS="✗"
|
||||||
|
ARROW="➜"
|
||||||
|
GEAR="⚙"
|
||||||
|
HAMMER="🔨"
|
||||||
|
ROCKET="🚀"
|
||||||
|
|
||||||
|
# Default values
|
||||||
|
BUILD_TYPE="Debug"
|
||||||
|
CLEAN_BUILD=false
|
||||||
|
VERBOSE=false
|
||||||
|
JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
|
||||||
|
|
||||||
|
# Print functions
|
||||||
|
print_header() {
|
||||||
|
echo -e "${BOLD}${BLUE}╔════════════════════════════════════════════════════════╗${NC}"
|
||||||
|
echo -e "${BOLD}${BLUE}║${NC} ${HAMMER} ${BOLD}${CYAN} C/C++ Project Builder${NC} ${BOLD}${BLUE}║${NC}"
|
||||||
|
echo -e "${BOLD}${BLUE}╚════════════════════════════════════════════════════════╝${NC}"
|
||||||
|
echo ""
|
||||||
|
}
|
||||||
|
|
||||||
|
print_separator() {
|
||||||
|
echo -e "${BLUE}────────────────────────────────────────────────────────${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_success() {
|
||||||
|
echo -e "${GREEN}${CHECK}${NC} $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_error() {
|
||||||
|
echo -e "${RED}${CROSS}${NC} $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_info() {
|
||||||
|
echo -e "${CYAN}${ARROW}${NC} $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
print_step() {
|
||||||
|
echo -e "${YELLOW}${GEAR}${NC} ${BOLD}$1${NC}"
|
||||||
|
}
|
||||||
|
|
||||||
|
show_progress() {
|
||||||
|
local duration=$1
|
||||||
|
local prefix=$2
|
||||||
|
local size=40
|
||||||
|
already_done() { for ((done=0; done<$elapsed; done++)); do printf "▓"; done }
|
||||||
|
remaining() { for ((remain=$elapsed; remain<$size; remain++)); do printf " "; done }
|
||||||
|
percentage() { printf "| %s%%" $(( (($elapsed)*100)/($size)*100/100 )); }
|
||||||
|
|
||||||
|
for (( elapsed=1; elapsed<=$size; elapsed++ )); do
|
||||||
|
printf "\r${CYAN}${prefix}${NC} [$(already_done)$(remaining)] $(percentage)"
|
||||||
|
sleep $(echo "scale=3; $duration/$size" | bc)
|
||||||
|
done
|
||||||
|
printf "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
echo -e "${BOLD}Usage:${NC} $0 [OPTIONS] <executable_name>"
|
||||||
|
echo ""
|
||||||
|
echo -e "${BOLD}Options:${NC}"
|
||||||
|
echo -e " -r, --release Build in Release mode (default: Debug)"
|
||||||
|
echo -e " -c, --clean Clean build directory before building"
|
||||||
|
echo -e " -v, --verbose Verbose make output"
|
||||||
|
echo -e " -j, --jobs <N> Number of parallel jobs (default: $JOBS)"
|
||||||
|
echo -e " -h, --help Show this help message"
|
||||||
|
echo ""
|
||||||
|
echo -e "${BOLD}Examples:${NC}"
|
||||||
|
echo -e " $0 myprogram"
|
||||||
|
echo -e " $0 -r -j8 myprogram"
|
||||||
|
echo -e " $0 --clean --release myprogram"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
# Parse arguments
|
||||||
|
POSITIONAL_ARGS=()
|
||||||
|
while [[ $# -gt 0 ]]; do
|
||||||
|
case $1 in
|
||||||
|
-r|--release)
|
||||||
|
BUILD_TYPE="Release"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-c|--clean)
|
||||||
|
CLEAN_BUILD=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-v|--verbose)
|
||||||
|
VERBOSE=true
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
-j|--jobs)
|
||||||
|
JOBS="$2"
|
||||||
|
shift 2
|
||||||
|
;;
|
||||||
|
-h|--help)
|
||||||
|
usage
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
POSITIONAL_ARGS+=("$1")
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
set -- "${POSITIONAL_ARGS[@]}"
|
||||||
|
|
||||||
|
# Check if executable name is provided
|
||||||
|
if [ -z "$1" ]; then
|
||||||
|
print_header
|
||||||
|
print_error "No executable name provided"
|
||||||
|
echo ""
|
||||||
|
usage
|
||||||
|
fi
|
||||||
|
|
||||||
|
EXECUTABLE_NAME=$1
|
||||||
|
|
||||||
|
# Start build process
|
||||||
|
print_header
|
||||||
|
|
||||||
|
# Build configuration info
|
||||||
|
print_info "Build Configuration:"
|
||||||
|
echo -e " ${BOLD}Executable:${NC} $EXECUTABLE_NAME"
|
||||||
|
echo -e " ${BOLD}Build Type:${NC} $BUILD_TYPE"
|
||||||
|
echo -e " ${BOLD}Jobs:${NC} $JOBS"
|
||||||
|
echo -e " ${BOLD}Clean Build:${NC} $CLEAN_BUILD"
|
||||||
|
echo ""
|
||||||
|
print_separator
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Clean build directory if requested
|
||||||
|
if [ "$CLEAN_BUILD" = true ] && [ -d "./build" ]; then
|
||||||
|
print_step "Cleaning build directory..."
|
||||||
|
rm -rf ./build
|
||||||
|
print_success "Build directory cleaned"
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Create build directory
|
||||||
|
if [ ! -d "./build" ]; then
|
||||||
|
print_step "Creating build directory..."
|
||||||
|
mkdir -p build
|
||||||
|
print_success "Build directory created"
|
||||||
|
else
|
||||||
|
print_info "Using existing build directory"
|
||||||
|
fi
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# CMake configuration
|
||||||
|
print_step "Configuring CMake..."
|
||||||
|
print_separator
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
cd ./build
|
||||||
|
|
||||||
|
CMAKE_CMD="cmake -DOUTPUT_NAME=$EXECUTABLE_NAME -DCMAKE_BUILD_TYPE=$BUILD_TYPE .."
|
||||||
|
|
||||||
|
if [ "$VERBOSE" = true ]; then
|
||||||
|
eval $CMAKE_CMD
|
||||||
|
else
|
||||||
|
eval $CMAKE_CMD > /dev/null 2>&1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $? -ne 0 ]; then
|
||||||
|
print_error "CMake configuration failed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_success "CMake configuration complete"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Compilation
|
||||||
|
print_step "Compiling project..."
|
||||||
|
print_separator
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
MAKE_CMD="make -j$JOBS"
|
||||||
|
if [ "$VERBOSE" = true ]; then
|
||||||
|
MAKE_CMD="$MAKE_CMD VERBOSE=1"
|
||||||
|
fi
|
||||||
|
|
||||||
|
START_TIME=$(date +%s)
|
||||||
|
|
||||||
|
if [ "$VERBOSE" = true ]; then
|
||||||
|
eval $MAKE_CMD
|
||||||
|
BUILD_RESULT=$?
|
||||||
|
else
|
||||||
|
eval $MAKE_CMD 2>&1 | tee build.log | while IFS= read -r line; do
|
||||||
|
if echo "$line" | grep -q "\[.*%\]"; then
|
||||||
|
printf "\r${CYAN}${ARROW}${NC} Compiling: %s" "$line"
|
||||||
|
elif echo "$line" | grep -qE "error:|Error|ERROR"; then
|
||||||
|
echo ""
|
||||||
|
print_error "$line"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
BUILD_RESULT=${PIPESTATUS[0]}
|
||||||
|
fi
|
||||||
|
|
||||||
|
END_TIME=$(date +%s)
|
||||||
|
BUILD_TIME=$((END_TIME - START_TIME))
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
if [ $BUILD_RESULT -ne 0 ]; then
|
||||||
|
print_error "Build failed!"
|
||||||
|
echo ""
|
||||||
|
print_info "Check build/build.log for details"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
print_success "Compilation complete (${BUILD_TIME}s)"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Copy executable
|
||||||
|
print_step "Copying executable to project root..."
|
||||||
|
|
||||||
|
if [ ! -f "./bin/$EXECUTABLE_NAME" ]; then
|
||||||
|
print_error "Executable not found: ./bin/$EXECUTABLE_NAME"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
cp ./bin/$EXECUTABLE_NAME ../
|
||||||
|
print_success "Executable copied"
|
||||||
|
echo ""
|
||||||
|
|
||||||
|
# Build summary
|
||||||
|
print_separator
|
||||||
|
echo -e "${BOLD}${GREEN}${ROCKET} Build Complete!${NC}"
|
||||||
|
print_separator
|
||||||
|
echo ""
|
||||||
|
echo -e "${BOLD}Summary:${NC}"
|
||||||
|
echo -e " ${BOLD}Executable:${NC} ./$EXECUTABLE_NAME"
|
||||||
|
echo -e " ${BOLD}Build Type:${NC} $BUILD_TYPE"
|
||||||
|
echo -e " ${BOLD}Build Time:${NC} ${BUILD_TIME}s"
|
||||||
|
echo -e " ${BOLD}Binary Location:${NC} ./build/bin/$EXECUTABLE_NAME"
|
||||||
|
|
||||||
|
# File size info
|
||||||
|
if command -v du &> /dev/null; then
|
||||||
|
SIZE=$(du -h "./$EXECUTABLE_NAME" | cut -f1)
|
||||||
|
echo -e " ${BOLD}Binary Size:${NC} $SIZE"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
print_info "Run with: ${BOLD}./$EXECUTABLE_NAME${NC}"
|
||||||
|
echo ""
|
||||||
0
include/.gitkeep
Normal file
0
include/.gitkeep
Normal file
0
include/include/.gitkeep
Normal file
0
include/include/.gitkeep
Normal file
0
libraries/.gitkeep
Normal file
0
libraries/.gitkeep
Normal file
0
libraries/libraries/.gitkeep
Normal file
0
libraries/libraries/.gitkeep
Normal file
7
source/main.cpp
Normal file
7
source/main.cpp
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::cout << "Hello, World!" << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user