Created
July 2, 2025 11:05
-
-
Save danielRi/d3293af7232ad237c086cc7cc595d6dc to your computer and use it in GitHub Desktop.
Generate C++ Project for VSCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# C++ Project Setup Script | |
# Creates a complete C++ project structure with CMake, VSCode integration, and common configurations | |
# | |
# Usage: | |
# chmod +x setup_cpp_project.sh | |
# ./setup_cpp_project.sh # Creates project with default name "MyProject" | |
# ./setup_cpp_project.sh MeinProjektName # Creates project with custom name | |
set -e # Exit on any error | |
PROJECT_NAME=${1:-"MyProject"} | |
echo "Creating C++ project structure for: $PROJECT_NAME" | |
# Create directory structure | |
echo "Creating directories..." | |
mkdir -p src | |
mkdir -p include | |
mkdir -p lib | |
mkdir -p build | |
mkdir -p .vscode | |
# Create main.cpp | |
echo "Creating main.cpp..." | |
cat > src/main.cpp << 'EOF' | |
#include <iostream> | |
int main() { | |
std::cout << "Hello, World!" << std::endl; | |
return 0; | |
} | |
EOF | |
# Create CMakeLists.txt | |
echo "Creating CMakeLists.txt..." | |
cat > CMakeLists.txt << EOF | |
cmake_minimum_required(VERSION 3.20) | |
project($PROJECT_NAME VERSION 1.0.0 LANGUAGES CXX) | |
# Set C++ standard | |
set(CMAKE_CXX_STANDARD 20) | |
set(CMAKE_CXX_STANDARD_REQUIRED ON) | |
set(CMAKE_CXX_EXTENSIONS OFF) | |
# Set build type to Debug if not specified | |
if(NOT CMAKE_BUILD_TYPE) | |
set(CMAKE_BUILD_TYPE Debug) | |
endif() | |
# Compiler flags | |
set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra -Wpedantic") | |
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG") | |
# Include directories | |
include_directories(include) | |
include_directories(lib) | |
# Find all source files | |
file(GLOB_RECURSE SOURCES "src/*.cpp") | |
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp") | |
# Create executable | |
add_executable(\${PROJECT_NAME} \${SOURCES} \${HEADERS}) | |
# Link directories | |
link_directories(lib) | |
# Optional: Add threading support | |
find_package(Threads REQUIRED) | |
target_link_libraries(\${PROJECT_NAME} PRIVATE Threads::Threads) | |
# Set output directory | |
set_target_properties(\${PROJECT_NAME} PROPERTIES | |
RUNTIME_OUTPUT_DIRECTORY \${CMAKE_BINARY_DIR}/bin | |
) | |
# Enable testing | |
enable_testing() | |
# Optional: Add subdirectories for tests | |
# add_subdirectory(tests) | |
# Installation rules | |
install(TARGETS \${PROJECT_NAME} | |
RUNTIME DESTINATION bin | |
) | |
EOF | |
# Create CMakePresets.json | |
echo "Creating CMakePresets.json..." | |
cat > CMakePresets.json << 'EOF' | |
{ | |
"version": 3, | |
"configurePresets": [ | |
{ | |
"name": "default", | |
"displayName": "Default Config", | |
"description": "Default build using Unix Makefiles", | |
"generator": "Unix Makefiles", | |
"binaryDir": "${sourceDir}/build", | |
"cacheVariables": { | |
"CMAKE_BUILD_TYPE": "Debug", | |
"CMAKE_EXPORT_COMPILE_COMMANDS": "ON" | |
} | |
}, | |
{ | |
"name": "debug", | |
"displayName": "Debug", | |
"description": "Debug build", | |
"inherits": "default", | |
"cacheVariables": { | |
"CMAKE_BUILD_TYPE": "Debug" | |
} | |
}, | |
{ | |
"name": "release", | |
"displayName": "Release", | |
"description": "Release build", | |
"inherits": "default", | |
"cacheVariables": { | |
"CMAKE_BUILD_TYPE": "Release" | |
} | |
} | |
], | |
"buildPresets": [ | |
{ | |
"name": "default", | |
"configurePreset": "default" | |
}, | |
{ | |
"name": "debug", | |
"configurePreset": "debug" | |
}, | |
{ | |
"name": "release", | |
"configurePreset": "release" | |
} | |
] | |
} | |
EOF | |
# Create .clangd config | |
echo "Creating .clangd..." | |
cat > .clangd << 'EOF' | |
CompileDatabase: build | |
Index: | |
Background: Build | |
StandardLibrary: Yes | |
Completion: | |
AllScopes: Yes | |
Hover: | |
ShowAKA: Yes | |
InlayHints: | |
Enabled: Yes | |
ParameterNames: Yes | |
DeducedTypes: Yes | |
Diagnostics: | |
ClangTidy: | |
Add: | |
- readability-* | |
- performance-* | |
- modernize-* | |
- bugprone-* | |
Remove: | |
- modernize-use-trailing-return-type | |
- readability-braces-around-statements | |
EOF | |
# Create .gitignore | |
echo "Creating .gitignore..." | |
cat > .gitignore << 'EOF' | |
# Build directories | |
build/ | |
bin/ | |
obj/ | |
out/ | |
# CMake | |
CMakeCache.txt | |
CMakeFiles/ | |
CMakeScripts/ | |
Testing/ | |
Makefile | |
cmake_install.cmake | |
install_manifest.txt | |
compile_commands.json | |
CTestTestfile.cmake | |
_deps/ | |
# Visual Studio Code | |
.vscode/settings.json | |
.vscode/tasks.json | |
.vscode/launch.json | |
.vscode/extensions.json | |
.vscode/c_cpp_properties.json | |
# IDEs | |
.idea/ | |
*.swp | |
*.swo | |
*~ | |
# 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 | |
# Debug files | |
*.dSYM/ | |
*.su | |
*.idb | |
*.pdb | |
# Kernel Module Compile Results | |
*.mod* | |
*.cmd | |
.tmp_versions/ | |
modules.order | |
Module.symvers | |
Mkfile.old | |
dkms.conf | |
# OS generated files | |
.DS_Store | |
.DS_Store? | |
._* | |
.Spotlight-V100 | |
.Trashes | |
ehthumbs.db | |
Thumbs.db | |
# Temporary files | |
*.tmp | |
*.temp | |
*.log | |
EOF | |
# Create VSCode settings | |
echo "Creating VSCode configuration..." | |
cat > .vscode/settings.json << 'EOF' | |
{ | |
"cmake.configureOnOpen": true, | |
"cmake.buildDirectory": "${workspaceFolder}/build", | |
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools", | |
"clangd.arguments": [ | |
"--compile-commands-dir=build", | |
"--background-index", | |
"--clang-tidy" | |
], | |
"files.associations": { | |
"*.h": "c", | |
"*.hpp": "cpp", | |
"*.cpp": "cpp", | |
"*.cc": "cpp", | |
"*.cxx": "cpp" | |
} | |
} | |
EOF | |
# Create sample header file | |
echo "Creating sample header file..." | |
mkdir -p include/$PROJECT_NAME | |
cat > include/$PROJECT_NAME/sample.h << 'EOF' | |
#ifndef SAMPLE_H | |
#define SAMPLE_H | |
class Sample { | |
public: | |
Sample(); | |
~Sample(); | |
void doSomething(); | |
private: | |
int value; | |
}; | |
#endif // SAMPLE_H | |
EOF | |
# Create README | |
echo "Creating README.md..." | |
cat > README.md << EOF | |
# $PROJECT_NAME | |
A C++ project created with automated setup script. | |
## Build Instructions | |
### Prerequisites | |
- CMake 3.20 or higher | |
- C++20 compatible compiler (GCC 10+, Clang 10+, MSVC 2019+) | |
- Make | |
### Building | |
\`\`\`bash | |
# Configure | |
cmake --preset debug | |
# Build | |
cmake --build build | |
# Run | |
./build/bin/$PROJECT_NAME | |
\`\`\` | |
### Using VSCode | |
1. Install the CMake Tools extension | |
2. Install the clangd extension | |
3. Open the project folder | |
4. Use Ctrl+Shift+P and run "CMake: Configure" | |
5. Use Ctrl+Shift+P and run "CMake: Build" | |
## Project Structure | |
- \`src/\` - Source files (.cpp) | |
- \`include/\` - Header files (.h, .hpp) | |
- \`lib/\` - External libraries | |
- \`build/\` - Build output directory | |
- \`.vscode/\` - VSCode configuration | |
EOF | |
echo "" | |
echo "✅ C++ project '$PROJECT_NAME' has been created successfully!" | |
echo "" | |
echo "Project structure:" | |
echo "├── src/" | |
echo "│ └── main.cpp" | |
echo "├── include/" | |
echo "│ └── $PROJECT_NAME/" | |
echo "│ └── sample.h" | |
echo "├── lib/" | |
echo "├── build/" | |
echo "├── .vscode/" | |
echo "│ └── settings.json" | |
echo "├── CMakeLists.txt" | |
echo "├── CMakePresets.json" | |
echo "├── .clangd" | |
echo "├── .gitignore" | |
echo "└── README.md" | |
echo "" | |
echo "To get started:" | |
echo "1. cd into your project directory" | |
echo "2. Run: cmake --preset debug" | |
echo "3. Run: cmake --build build" | |
echo "4. Run: ./build/bin/$PROJECT_NAME" | |
echo "" | |
echo "Happy coding! 🚀" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment