Skip to content

Instantly share code, notes, and snippets.

@hareendranmg
Last active July 3, 2023 07:19
Show Gist options
  • Save hareendranmg/bd30a19277efbb0270b55939c0db0902 to your computer and use it in GitHub Desktop.
Save hareendranmg/bd30a19277efbb0270b55939c0db0902 to your computer and use it in GitHub Desktop.
Basic CPP Project creation python script
#!/usr/bin/env python3
import sys
import os
def convert_to_pascal_case(name):
words = name.split('_')
capitalized_words = [word.capitalize() for word in words]
return ''.join(capitalized_words)
def create_cpp_project(name):
# Convert project name to PascalCase
project_name = convert_to_pascal_case(name)
# Create project directory
os.mkdir(project_name)
os.chdir(project_name)
# Create main.cpp
main_cpp_content = '''#include<iostream>
using namespace std;
int main()
{
cout << "Hi Mom :)" ;
return 0;
}
'''
with open('main.cpp', 'w') as main_cpp_file:
main_cpp_file.write(main_cpp_content)
# Create CMakeLists.txt
cmake_lists_content = '''cmake_minimum_required(VERSION 3.16.3)
set(CMAKE_CXX_COMPILER "clang++")
project({0})
add_executable(${{PROJECT_NAME}} main.cpp)
'''.format(project_name)
with open('CMakeLists.txt', 'w') as cmake_lists_file:
cmake_lists_file.write(cmake_lists_content)
# Create configure.sh
configure_content = '''#! /bin/sh
cmake -DBUILD_TESTS=FALSE -S . -B out/build
'''
with open('configure.sh', 'w') as configure_file:
configure_file.write(configure_content)
# Create build.sh
build_content = '''#! /bin/sh
cd out/build ; make
'''
with open('build.sh', 'w') as build_file:
build_file.write(build_content)
# Create run.sh
run_content = '''#! /bin/sh
cd out/build ; ./{0}
'''.format(project_name)
with open('run.sh', 'w') as run_file:
run_file.write(run_content)
# Create clean.sh
clean_content = '''#! /bin/sh
rm -rf out/build
'''
with open('clean.sh', 'w') as clean_file:
clean_file.write(clean_content)
# Create .gitignore
gitignore_content = '''build/
.vscode/
main
makefile
'''
with open('.gitignore', 'w') as gitignore_file:
gitignore_file.write(gitignore_content)
print(f'C++ project "{project_name}" created successfully.')
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Please provide a project name.')
sys.exit(1)
project_name = sys.argv[1]
create_cpp_project(project_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment