How to Create a Clang C Project Using vcpkg and CMake
Prerequisites
Make sure the following tools are installed on your system:
Required Tools
- CMake (>= 3.15)
- vcpkg
- Clang (or your preferred C compiler)
Windows Installation Tips
You can use Scoop or Winget to install dependencies:
Using Scoop
scoop install cmake llvm vcpkg
If
scoopis not installed yet, follow instructions at: https://scoop.sh/
Using Winget
winget install Kitware.CMake
winget install LLVM.LLVM
winget install Microsoft.Vcpkg
Step-by-Step Setup
Initialize a New vcpkg Project
make a project folder and move inside it.
mkdir vcpkg-project && cd vcpkg-project
Initialize the vcpkg project
vcpkg new --application
This will generate the basic folder structure for your application.
Create CMakeLists.txt and CMakePresets.json
Use this preconfigured template:
Get the template files:
https://gist.github.com/marcuwynu23/ac99687142e176d6b024fe7d796907e6
Includes:
- Clang setup
- vcpkg integration
- Recursive source file gathering
- Clean and documented structure
Configure the Project
Create src then create file .cpp or c
mkdir src && touch src\main.c # or src\main.cpp
you need also to update the CMakelists.txt in this part:
file(GLOB_RECURSE SOURCES "src/*.c")
This command will run CMake and automatically trigger vcpkg installation for any missing dependencies.
cmake --preset=default
Build the Project
cmake --build build
adding dependencies
if you want to add dependency for example fmt you use this vcpkg add port <dependency>:
vcpkg add port fmt
then you need to run
cmake --preset=default
then find in log this section:
The package fmt provides CMake targets:
then get the below part like this:
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt)
# Or use the header-only version
find_package(fmt CONFIG REQUIRED)
target_link_libraries(main PRIVATE fmt::fmt-header-only)
then add it in CMakelists.txt in Linking External Libraries (via vcpkg) section:
after adding you can now run:
cmake --build build
how to remove dependencies
To remove depedency is by commentout or removing the part that you add in CMakelists.txt Linking External Libraries (via vcpkg) section:
then remove in vcpkg.json:
"dependencies": [
"fmt" // remove this
]
after that, run the cmake --preset=default and cmake --build build
Done!
You now have a functioning Clang C project with modern CMake and vcpkg integration. 🎉