Search code examples
c++macoscmakegithub-actionsvcpkg

Problem building C++ binary using vcpkg and cmake in Github action macos-12


Works fine building on Linux and Windows.

But in macos I get:

/usr/local/bin/g++-11  -isysroot /Applications/Xcode_13.4.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/Calculator_Test.dir/src/speedtest.cpp.o CMakeFiles/Calculator_Test.dir/src/tests.cpp.o -o Calculator_Test  ../libCalculator.a ../vcpkg_installed/x64-osx/debug/lib/libCatch2d.a ../vcpkg_installed/x64-osx/debug/lib/manual-link/libCatch2Maind.a ../vcpkg_installed/x64-osx/debug/lib/libCatch2d.a 
Undefined symbols for architecture x86_64:
  "__ZN5Catch24translateActiveExceptionB5cxx11Ev", referenced from:
      __ZN5Catch9Benchmark9Benchmark3runINSt6chrono3_V212steady_clockEEEvv in speedtest.cpp.o
  "__ZN5Catch9Benchmark6Detail15analyse_samplesEdjN9__gnu_cxx17__normal_iteratorIPdSt6vectorIdSaIdEEEES8_", referenced from:
      __ZN5Catch9Benchmark6Detail7analyseINSt6chrono8durationIdSt5ratioILl1ELl1000000000EEEEN9__gnu_cxx17__normal_iteratorIPS7_St6vectorIS7_SaIS7_EEEEEENS0_14SampleAnalysisIT_EERKNS_7IConfigENS0_11EnvironmentISG_EET0_SN_ in speedtest.cpp.o

Running the following works fine and without error:

cmake --preset=${{ matrix.preset }} -B build -DCalculator_Test=1 -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 -DCMAKE_VERBOSE_MAKEFILE=1

"preset" is just setting the CMAKE_TOOLCHAIN_FILE to the one provided by vcpkg and the cmake generator to "Unix Makefiles"

From CMakeLists.txt used:

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

...
 
find_package(Catch2 CONFIG REQUIRED)

target_link_libraries(Calculator_Test
    PRIVATE
        Calculator
        Catch2::Catch2 Catch2::Catch2WithMain
)

But when compiling is when it fails

cmake --build build

My question is why does it fail and how to fix it?

Is it C++20 that is the problem or is it CMake, vcpkg or something I must do on macos. I'm no macos expert :(


Solution

  • Solved the problem by setting the environmental variables to the correct compiler before setting up vcpkg.

    In the github actions yml file:

          - name: Set C++/C compiler on macOs
            shell: bash
            run: echo "CC=gcc-11" >> $GITHUB_ENV; echo "CXX=g++-11" >> $GITHUB_ENV; cat "$GITHUB_ENV"
            if: runner.os == 'macOs'
    

    The build step (without specifying the c/c++ compiler):

    cmake --preset=${{ matrix.preset }} -B build -DCalculator_Test=1 -DCMAKE_VERBOSE_MAKEFILE=1
    

    My assumptions is that the linking of the dependency binaries failed earlier due to being compiled with a different compiler than the application itself.