Example C99 CMake project that assumes CMake 3.10 or later, and prefers the
Ninja build tool with CMake's "Ninja Multi-Config"
option.
Recent versions of both CMake and Ninja executables, are easy to obtain for
Linux, macOS, FreeBSD & Windows, keeping examples and instructions simpler.
NOTE: The "Ninja Multi-Config"
generator, is only available from CMake 3.15.
The CMakeLists.txt
file is generator-agnostic, and the C code is portable
C99 code. Use any preferred generator if Ninja is not available, but the
supplied CMakeLists.txt
file requires CMake 3.10 or higher (prefer 3.17).
This example does not necessarily represent ‘best practices’, yet is still not truly minimal; for example, consider these optional features used:
DESCRIPTION
,VERSION
andLANGUAGES
are optional.- A more compliant preprocessor choice for MSVC is good, but not required.
- Setting strict C99 standard compliance is opinionated.
- Increased compiler warning level is opinionated.
- Setting project and executable names to project directory name!
- Using
GLOB
to ‘collect’ source and header files. - Executable in
‹project›/bin/Debug‖Release
(default isbuild
directory).
The last two points make it easy to re-use this CMakeLists.txt
file almost
verbatim in other simple C projects. Change a new project's DESCRIPTION
and
VERSION
. Keep the ‹project› directory name simple.
When calling cmake
without arguments, it looks in the current directory
for a CMakeLists.txt
file. Recommended steps will keep the ‹project›
directory as the current directory. CMake 3.17 example that uses "Ninja Multi-Config"
:
cd ‹project› #←make project dir current.
cmake -S . -B build -G "Ninja Multi-Config" #←create build files.
cmake --build build --config Release‖Debug #←build the project.
bin/Release‖Debug/‹project› #←run the executable.
An example that uses the default generator for your platform, and CMake 3.10:
cd ‹project› #←make project dir current.
mkdir build; cd build #←create &cd to build directory.
cmake .. -DCMAKE_BUILD_TYPE=Release‖Debug #←create build files.
cmake --build . #←build the project.
../bin/Release‖Debug/‹project› #←run the executable.
Optionally, one can install the executable (copy to some bin
directory). It
may be a good idea to first do a ‘clean rebuild’. The --prefix
argument,
must be something like $HOME
, or /usr/local
(omit the bin
part).
cmake --build build --config Release --clean-first
cmake --install --prefix $HOME --strip
The --strip
reduces the size of the executable somewhat. This is optional.
NOTE: On many systems, $HOME/bin
is always present, and first in PATH
. Also
popular, is $HOME/.local/bin
, in which case, use: --prefix $HOME/.local
.
Clean the ‹project› before copying or archiving, by recursively deleting any
bin
and build
directories inside. For example:
cd ‹project›
rm -rf bin build
cd ..
cp -r ‹project› ‹new-project›
The included .replit
file is for use with the Run button on repl.it
(See their documentation). If repl.it is not used, the .replit
,
and replenv
files can be deleted, and this whole section ignored.
In case this project is placed in a sub-directory of a ‘repl’ , e.g.: in
cmkcmin
, you can use this in the .replit
file:
run = "clear; cd cmkcmin; sh ./build.sh
Since repl.it provides only CMake 3.10, a later CMake (3.17.3) Linux binary can
be downloaded, and extracted to a directory in your ‘repl’. Amend
the PATH
in build.sh
. Something like this in the first command:
export PATH=$HOME/‹repl›/.local/bin:$PATH
Where ‹repl›
must be the name of the ‘repl’; and assuming that cmake
and
related programs can be found in ./.local/bin
.
The following commands assume that your ‘repl’ directory (‹repl›
) is the current
working directory. Replace ‹repl›
with your ‘repl’ name.
cd $HOME/‹repl›
mkdir .local; cd .local
export TD=cmake-3.17.3-Linux-x86_64
export TF=$TD.tar.gz
curl -LO https://github.com/Kitware/CMake/releases/download/v3.17.3/$TF
tar xvzf $TF && rm $TF
mv $TD/* . && rmdir $TD
rmdir -rf doc
> replenv cat <<EOT
export PATH=$PWD/.local/bin:$PATH
export MANPATH=$PWD/.local/man:$MANPATH
alias l='ls --color=auto -F'
alias ll='ls --color=auto --group-directories-first -AogF'
EOT
Every time the ‘repl’ environment is (re)loaded, source (.
) the replenv
to
set PATH
and aliases in the terminal.
. ./replenv
You ‘repl’ directory will persist, but not the virtual machine. It can reload
at any time due to bad connections, or other reasons. Thus the need for something
like the example replenv
created.
Create a new C repl on repl.it. In the terminal, run:
rm -rf * .*
git clone https://github.com/funvake/cmkc99min.git .
Notice the trailing period!
The editor component may crash. Reload the repl if necessary. Click the Run
button to run the project. The directory must be completely empty for git
to perform the clone.
This can all be avoided, if the option to clone from GitHub is used, when a
new repl is created. It will also automatically display the README.md
file.