galudino / My-CLI-Playground

Personal sandbox/playground for command-line projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

My-CLI-Playground

About

This "playground" is for my own personal use,
for when I want to program C, C++, Objective-C/C++, or Swift in a command-line environment.

Table of contents

Prerequisites

This repository uses cmake. The CMakeLists.txt file requires 3.18.

make is also required for Unix Makefiles -
cmake will create the Makefiles for building your targets.

  • A C/C++ compiler, clang or gcc for C and C++ compilation
  • A Swift compiler, swiftc, for Swift compilation

Please note: Unix Makefiles do not support Swift.
Swift sources must be built using xcodebuild.

You will not be able to use the makebuilds or run scripts provided,
since those will be using Unix Makefiles.

Please see Xcode/xcodebuild users if you are interested in using Xcode.

If you would rather use another build system,
invoke % cmake -help in the Terminal to review the build systems available to you
on your platform.

(return to top)

How to use

There are some scripts provided in this repository:

  • ./add_target creates a subdirectory in src with a client source file, additional source file, header file (if applicable),and a local CMakeLists.txt file.
    It will also add the name of the subdirectory to the top-level CMakeLists.txt, and makebuilds will be run to update the build system.

  • ./remove_target does the opposite of ./add_target -
    it removes a specified subdirectory from src, as well as the its entry from the top-level CMakeLists.txt file in src.
    makebuilds will be run to update the build system.

  • ./makebuilds runs CMake and creates a ./build/make subdirectory, with the following configurations:

    • Debug
    • Release
    • MinSizeRel
    • RelWithDebInfo These configurations each have their own subdirectory in ./build/make.
  • ./run runs a target of your choice with a desired configuration.

(return to top)

Creating and using a new target

A target is a complete program.

Let's say we want to add a new target, called MyNewTarget, written in C++.

% ./add_target MyNewTarget cpp will create a new target named MyNewTarget, with C++ sources.

  • A new directory, ./src/MyNewTarget will be created, containing the following:
    • main.cpp, source file with main() function
    • header.hpp, header file for declarations, inline functions, directives, etc.
    • source.cpp, source file for implementation details
    • CMakeLists.txt, instructions local to this subdirectory
    • The top-level CMakeLists.txt in ./src will also be modified to add this new target.

Then,
cd into MyNewTarget and code away...

To build and run MyNewTarget, invoke % ./run MyNewTarget
This will run MyNewTarget in the Debug configuration.

You may specify one of the optional arguments after ./run MyNewTarget:
- Debug, i.e. ./run MyNewTarget Debug
- Release, i.e. ./run MyNewTarget Release
- MinSizeRel, i.e. ./run MyNewTarget MinSizeRel
- RelWithDebInfo
, i.e. ./run MyNewTarget RelWithDebInfo

(return to top)

Removing a target

You could remove MyNewTarget yourself, and revise the CMakeLists.txt file --
or you can run % ./remove_target MyNewTarget.

(return to top)

Script usage

add_target

./add_target [target-name] [language-file-extension]

  • Creates a new directory titled target-name to ./src/ and appends the directory to the top-level CMakeLists.txtin./src`.
  • i.e. ./add_target MyProgram cpp
  • Supported arguments for [language-file-extension]:
    • c
    • cpp
    • swift

remove_target

./remove_target [target-name]

  • Removes directory target-name from src and runs sed on ./src/CMakeLists.txt to remove the target-name subdirectory.

makebuilds

./makebuilds

  • Runs cmake and creates a build/make directory, with Debug, Release, MinSizeRel, and RelWithDebInfo subdirectories.
    The subdirectories above will have folders containing a Makefile for each target in this repository.

./run

./run [target-name] [configuration]

  • Runs make -C ./build/make/[configuration]/[target-name], then runs the executable for target-name.

(return to top)

Xcode/xcodebuild users

Swift sources are not supported by Unix Makefiles.
If you are on macOS, we can use xcodebuild instead to stay in the command line.

  • First, use cmake to create the Xcode project.
    • % cmake -S ./src -B ./build/xcode -G "Xcode"
  • Navigate to ./build/xcode
    • % cd ./build/xcode
  • We will now use xcodebuild to build our targets.
    • To build all targets in Debug mode:
      • % xcodebuild
    • To build all targets in a chosen configuration:
      • % xcodebuild -configuration [config-name]
        • [config-name] can be:
          • Debug
          • Release
          • MinSizeRel
          • RelWithDebInfo
    • To build a specific target in Debug mode:
      • % xcodebuild -target [target-name]
    • To build a specific target with a chosen configuration:
      • % xcodebuild -target [target-name] -configuration [config-name]

Your executable will be in ./build/xcode/[target-name]/[config-name]/

Invoke % xcodebuild -help for a complete list of valid options.

(return to top)

About

Personal sandbox/playground for command-line projects

License:MIT License


Languages

Language:C++ 67.5%Language:C 12.0%Language:CMake 10.0%Language:Shell 7.4%Language:Objective-C 1.6%Language:Objective-C++ 1.3%Language:Swift 0.3%