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.
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
orgcc
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.
There are some scripts provided in this repository:
-
./add_target
creates a subdirectory insrc
with a client source file, additional source file, header file (if applicable),and a localCMakeLists.txt
file.
It will also add the name of the subdirectory to the top-levelCMakeLists.txt
, andmakebuilds
will be run to update the build system. -
./remove_target
does the opposite of./add_target
-
it removes a specified subdirectory fromsrc
, as well as the its entry from the top-levelCMakeLists.txt
file insrc
.
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.
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 withmain()
functionheader.hpp
, header file for declarations, inline functions, directives, etc.source.cpp
, source file for implementation detailsCMakeLists.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
You could remove MyNewTarget
yourself, and revise the CMakeLists.txt
file --
or you can run % ./remove_target MyNewTarget
.
./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 [target-name]
- Removes directory
target-name
fromsrc
and runssed
on./src/CMakeLists.txt
to remove thetarget-name
subdirectory.
./makebuilds
- Runs
cmake
and creates abuild/make
directory, withDebug
,Release
,MinSizeRel
, andRelWithDebInfo
subdirectories.
The subdirectories above will have folders containing aMakefile
for each target in this repository.
./run [target-name] [configuration]
- Runs
make -C ./build/make/[configuration]/[target-name]
, then runs the executable fortarget-name
.
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]
- To build all targets in
Your executable will be in ./build/xcode/[target-name]/[config-name]/
Invoke % xcodebuild -help
for a complete list of valid options.