Search code examples
c++cmakegithub-actionsexpat-parser

How to install expat and link with my c++ project on github windows runner


expat is a dependency of my project. I would like to build my project on github windows-2019 runner. I got an error Could NOT find EXPAT (missing: EXPAT_LIBRARY EXPAT_INCLUDE_DIR) (see line 26 at the Configure CMake section). Below is the mini example to reproduce the error.

main.cpp

#include <iostream>

int main() {
    std::cout << "hello\n";
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.25)
project(hello VERSION 1.0.0 LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

find_package(EXPAT MODULE REQUIRED)
set(Required_Libraries ${Required_Libraries} EXPAT::EXPAT)

add_executable(hello main.cpp)

build_windows.yml

name: Build Windows

on:
  workflow_dispatch:

jobs:
  build:
    name: Build Windows
    runs-on: windows-2019
    steps:
      - uses: actions/checkout@v3

      - name: "Install required packages"
        run: |
          vcpkg install expat
          vcpkg integrate install

      - name: Configure CMake
        run: |
          mkdir build
          cd build
          cmake ..

      - name: Build
        run: |
          cd build
          cmake --build . --config Release

Solution

  • Add to CMakeLists.txt

    # Set the path to your vcpkg installation
    set(CMAKE_TOOLCHAIN_FILE "${CMAKE_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake")
    
    set(CMAKE_PREFIX_PATH "${CMAKE_PREFIX_PATH};${CMAKE_SOURCE_DIR}/vcpkg/installed/x64-windows")