cmake-init icon indicating copy to clipboard operation
cmake-init copied to clipboard

Building on MacOS

Open rhzs opened this issue 2 years ago • 7 comments

Hi, I got error CMAKE_MAKE_PROGRAM is not set.

My env is MacOS 11.3.1.

CMake version:

❯ cmake --version
cmake version 3.23.1

CMake suite maintained and supported by Kitware (kitware.com/cmake).

Error in Root dir after cmake-init

❯ cmake --preset=dev
Preset CMake variables:

  BUILD_MCSS_DOCS="ON"
  CMAKE_BUILD_TYPE="Debug"
  CMAKE_CXX_CLANG_TIDY="clang-tidy;--header-filter=/Users/angel/Work/explore-cpp/idtaxes/*"
  CMAKE_CXX_CPPCHECK="cppcheck;--inline-suppr"
  CMAKE_CXX_EXTENSIONS="OFF"
  CMAKE_CXX_FLAGS="-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wshadow -Wformat=2 -Wundef -Werror=float-equal"
  CMAKE_CXX_STANDARD="20"
  CMAKE_CXX_STANDARD_REQUIRED="ON"
  CMAKE_TOOLCHAIN_FILE="/scripts/buildsystems/vcpkg.cmake"
  VCPKG_MANIFEST_FEATURES="test"
  idtaxes_DEVELOPER_MODE="ON"

CMake Error at /Applications/CMake.app/Contents/share/cmake-3.23/Modules/CMakeDetermineSystem.cmake:130 (message):
  Could not find toolchain file: /scripts/buildsystems/vcpkg.cmake
Call Stack (most recent call first):
  CMakeLists.txt:9 (project)


CMake Error: CMake was unable to find a build program corresponding to "Unix Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
-- Configuring incomplete, errors occurred!

Can anyone help me?

Thank you

rhzs avatar May 12 '22 15:05 rhzs

You need to install a compiler, a build system (default on Linux and MacOS are make as the error suggests) and you are also missing vcpkg.

vcpkg is assumed to exist at the path in the VCPKG_ROOT environment variable. If you look at the CI logs for this project, you can see the env variables setup for vcpkg:

Setting env variables:
  VCPKG_ROOT=/Users/runner/work/cmake-init/cmake-init/vcpkg

I do not have an Apple machine, so I can't really test things on my end regarding Xcode, if that is what you are using right now. In that case, what I suggest you do is edit CMakeUserPresets.json such that the dev preset uses the Xcode generator instead:

    {
      "name": "dev",
      "binaryDir": "${sourceDir}/build/dev",
      "inherits": "dev-unix",
      "generator": "Xcode",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": ""
      }
    },

I admit that an Xcode specific workflow is not really accomodated for by default, but I can't really be sure what I do is right due to me lacking the means to test them.

friendlyanon avatar May 12 '22 15:05 friendlyanon

@friendlyanon ,

thanks. I can get it working after configuring presets.

But, I have other issue. The build is failing when building with library.

❯ cmake --build --preset=dev
[ 20%] Building CXX object CMakeFiles/idtaxes_lib.dir/source/lib.cpp.o
[ 20%] Built target idtaxes_lib
[ 40%] Building CXX object CMakeFiles/idtaxes_exe.dir/source/main.cpp.o
[ 60%] Linking CXX executable idtaxes
ld: warning: dylib (/usr/local/Cellar/gcc/11.3.0/lib/gcc/11/libstdc++.dylib) was built for newer macOS version (11.5) than being linked (11.3)
Undefined symbols for architecture x86_64:
  "__ZN3fmt2v87vformatB5cxx11ENS0_17basic_string_viewIcEENS0_17basic_format_argsINS0_20basic_format_contextINS0_8appenderEcEEEE", referenced from:
      __ZN7libraryC2Ev in lib.cpp.o
      __ZN7libraryC1Ev in lib.cpp.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
make[2]: *** [idtaxes] Error 1
make[1]: *** [CMakeFiles/idtaxes_exe.dir/all] Error 2
make: *** [all] Error 2

Any idea how to fix?

rhzs avatar May 12 '22 16:05 rhzs

The most important information here is:

ld: warning: dylib (/usr/local/Cellar/gcc/11.3.0/lib/gcc/11/libstdc++.dylib) was built for newer macOS version (11.5) than being linked (11.3)

Like I said, I do not own an Apple machine, so I can't really research this myself sufficiently. What I could suggest is using AppleClang instead, as that appears to be the "native" compiler for MacOS.

friendlyanon avatar May 12 '22 16:05 friendlyanon

ok, thanks for your help @friendlyanon

rhzs avatar May 12 '22 16:05 rhzs

I'd like to keep this open, since the default output might not be the most ideal for Apple users and someone else might also face similar issues. I can't necessarily solve this myself, but hopefully someone might suggest changes related to this.

friendlyanon avatar May 12 '22 16:05 friendlyanon

@friendlyanon I can make it work, I switched back to Apple Clang / LLVM Clang instead of gcc. Not sure why gcc installation via Homebrew got the problem.

rhzs avatar May 12 '22 16:05 rhzs

How to make it work on MacOS 11.3

Prerequisites

  1. (optional) Homebrew, brew install llvm, then add to your env, if you don't want to use Apple Clang.
  2. vcpkg in env path. (i use vcpkg)
  3. cmake in env path.

Step by step compiling your project after cmake-init:

  1. vcpkg install in root directory (fetching fmt dependency and other dependencies)
1. Config CMAKE presets
{
    "version": 2,
    "cmakeMinimumRequired": {
        "major": 3,
        "minor": 2,
        "patch": 0
    },
    "configurePresets": [
        {
            "name": "dev-mode",
            "hidden": true,
            "cacheVariables": {
                "idtaxes_DEVELOPER_MODE": "ON",
                "VCPKG_MANIFEST_FEATURES": "test"
            }
        },
        {
            "name": "vcpkg",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
            }
        },
        {
            "name": "vcpkg-win64-static",
            "hidden": true,
            "cacheVariables": {
                "VCPKG_TARGET_TRIPLET": "x64-windows-static-md"
            }
        },
        {
            "name": "cppcheck",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_CXX_CPPCHECK": "cppcheck;--inline-suppr"
            }
        },
        {
            "name": "clang-tidy",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_CXX_CLANG_TIDY": "clang-tidy;--header-filter=${sourceDir}/*"
            }
        },
        {
            "name": "ci-std",
            "description": "This preset makes sure the project actually builds with at least the specified standard",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_CXX_EXTENSIONS": "OFF",
                "CMAKE_CXX_STANDARD": "20",
                "CMAKE_CXX_STANDARD_REQUIRED": "ON"
            }
        },
        {
            "name": "flags-unix",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_CXX_FLAGS": "-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wshadow -Wformat=2 -Wundef -Werror=float-equal"
            }
        },
        {
            "name": "flags-windows",
            "description": "Note that all the flags after /W4 are required for MSVC to conform to the language standard",
            "hidden": true,
            "cacheVariables": {
                "CMAKE_CXX_FLAGS": "/utf-8 /W4 /permissive- /volatile:iso /Zc:preprocessor /EHsc /Zc:__cplusplus /Zc:externConstexpr /Zc:throwingNew"
            }
        },
        {
            "name": "ci-unix",
            "generator": "Unix Makefiles",
            "hidden": true,
            "inherits": [
                "flags-unix",
                "ci-std"
            ],
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Release"
            }
        },
        {
            "name": "ci-win64",
            "inherits": [
                "flags-windows",
                "ci-std"
            ],
            "generator": "Visual Studio 17 2022",
            "architecture": "x64",
            "hidden": true
        },
        {
            "name": "coverage-unix",
            "binaryDir": "${sourceDir}/build/coverage",
            "inherits": "ci-unix",
            "hidden": true,
            "cacheVariables": {
                "ENABLE_COVERAGE": "ON",
                "CMAKE_BUILD_TYPE": "Coverage",
                "CMAKE_CXX_FLAGS_COVERAGE": "-Og -g --coverage -fkeep-inline-functions -fkeep-static-functions",
                "CMAKE_EXE_LINKER_FLAGS_COVERAGE": "--coverage",
                "CMAKE_SHARED_LINKER_FLAGS_COVERAGE": "--coverage",
                "CMAKE_MAP_IMPORTED_CONFIG_COVERAGE": "Coverage;RelWithDebInfo;Release;Debug;"
            }
        },
        {
            "name": "ci-coverage",
            "inherits": [
                "coverage-unix",
                "dev-mode",
                "vcpkg"
            ],
            "cacheVariables": {
                "COVERAGE_HTML_COMMAND": ""
            }
        },
        {
            "name": "ci-sanitize",
            "binaryDir": "${sourceDir}/build/sanitize",
            "inherits": [
                "ci-unix",
                "dev-mode",
                "vcpkg"
            ],
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Sanitize",
                "CMAKE_CXX_FLAGS_SANITIZE": "-O2 -g -fsanitize=address,undefined -fno-omit-frame-pointer -fno-common",
                "CMAKE_MAP_IMPORTED_CONFIG_SANITIZE": "Sanitize;RelWithDebInfo;Release;Debug;"
            }
        },
        {
            "name": "ci-build",
            "binaryDir": "${sourceDir}/build",
            "hidden": true
        },
        {
            "name": "ci-macos",
            "inherits": [
                "ci-build",
                "ci-unix",
                "dev-mode",
                "vcpkg"
            ]
        },
        {
            "name": "ci-ubuntu",
            "inherits": [
                "ci-build",
                "ci-unix",
                "clang-tidy",
                "vcpkg",
                "cppcheck",
                "dev-mode"
            ]
        },
        {
            "name": "ci-windows",
            "inherits": [
                "ci-build",
                "ci-win64",
                "dev-mode",
                "vcpkg",
                "vcpkg-win64-static"
            ]
        },
        {
            "name": "dev",
            "description": "",
            "displayName": "",
            "inherits": ["dev-mode", "vcpkg", "ci-macos"],
            "cacheVariables": {
                "CMAKE_BUILD_TYPE": "Debug"
            }
        }
    ],
    "buildPresets": [
        {
        "name": "dev",
        "configurePreset": "dev",
        "configuration": "Debug"
        }
    ],
    "testPresets": [
      {
        "name": "dev",
        "configurePreset": "dev",
        "configuration": "Debug",
        "output": {
          "outputOnFailure": true
        }
      }
    ]
}
  1. Prepare preset and build folder from the above preset config.
❯ cmake --preset=dev
Preset CMake variables:

  CMAKE_BUILD_TYPE="Debug"
  CMAKE_CXX_EXTENSIONS="OFF"
  CMAKE_CXX_FLAGS="-Wall -Wextra -Wpedantic -Wconversion -Wsign-conversion -Wcast-qual -Wshadow -Wformat=2 -Wundef -Werror=float-equal"
  CMAKE_CXX_STANDARD="20"
  CMAKE_CXX_STANDARD_REQUIRED="ON"
  CMAKE_TOOLCHAIN_FILE="/Users/angel/Work/explore-cpp/vcpkg/scripts/buildsystems/vcpkg.cmake"
  VCPKG_MANIFEST_FEATURES="test"
  idtaxes_DEVELOPER_MODE="ON"

-- Running vcpkg install
Detecting compiler hash for triplet x64-osx...
The following packages will be built and installed:
    catch2[core]:x64-osx -> 2.13.8#1 -- /Users/angel/Work/explore-cpp/vcpkg/buildtrees/versioning_/versions/catch2/c243585b5ba220e6aebcdb59dc59867afff7af6e
    fmt[core]:x64-osx -> 8.1.1#1 -- /Users/angel/Work/explore-cpp/vcpkg/buildtrees/versioning_/versions/fmt/602d9743b7957c9e82a06d0e81d58637c6df5222
  * vcpkg-cmake[core]:x64-osx -> 2022-04-12 -- /Users/angel/Work/explore-cpp/vcpkg/buildtrees/versioning_/versions/vcpkg-cmake/ef4a9463f802c1cd6f8b31e560529aeaee0faff4
  * vcpkg-cmake-config[core]:x64-osx -> 2022-02-06 -- /Users/angelWork/explore-cpp/vcpkg/buildtrees/versioning_/versions/vcpkg-cmake-config/24dc7dfc704406e9f745f033643dc25f56e4ca18
Additional packages (*) will be modified to complete this operation.
Restored 4 packages from /Users/angel/.cache/vcpkg/archives in 92 ms. Use --debug to see more details.
Starting package 1/4: vcpkg-cmake-config:x64-osx
Installing package vcpkg-cmake-config[core]:x64-osx...
Elapsed time for package vcpkg-cmake-config:x64-osx: 2.38 ms
Starting package 2/4: vcpkg-cmake:x64-osx
Installing package vcpkg-cmake[core]:x64-osx...
Elapsed time for package vcpkg-cmake:x64-osx: 4.168 ms
Starting package 3/4: fmt:x64-osx
Installing package fmt[core]:x64-osx...
Elapsed time for package fmt:x64-osx: 10.61 ms
Starting package 4/4: catch2:x64-osx
Installing package catch2[core]:x64-osx...
Elapsed time for package catch2:x64-osx: 12.61 ms

Total elapsed time: 2.045 s

The package fmt provides CMake targets:

    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)

The package catch2 provides CMake targets:

    find_package(Catch2 CONFIG REQUIRED)
    target_link_libraries(main PRIVATE Catch2::Catch2 Catch2::Catch2WithMain)

-- Running vcpkg install - done
-- The C compiler identification is AppleClang 12.0.5.12050022
-- The CXX compiler identification is AppleClang 12.0.5.12050022
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
CMake Warning (dev) in CMakeLists.txt:
  Policy CMP0087 is not set: Install CODE|SCRIPT allow the use of generator
  expressions.  Run "cmake --help-policy CMP0087" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: /Users/angel/Work/explore-cpp/idtaxes/build
  1. Build/Compile with preset
❯ cmake --build --preset=dev
[ 20%] Building CXX object CMakeFiles/idtaxes_lib.dir/source/lib.cpp.o
[ 20%] Built target idtaxes_lib
[ 40%] Building CXX object CMakeFiles/idtaxes_exe.dir/source/main.cpp.o
[ 60%] Linking CXX executable idtaxes
[ 60%] Built target idtaxes_exe
[ 80%] Building CXX object test/CMakeFiles/idtaxes_test.dir/source/idtaxes_test.cpp.o
[100%] Linking CXX executable idtaxes_test
[100%] Built target idtaxes_test

rhzs avatar May 12 '22 17:05 rhzs

I'll close this, since the generated projects build fine on MacOS. If you want to edit the project using Xcode, then you'll have to change the generator, but since Xcode is such a lackluster and barebones editor, I'm fine with leaving the default generator as Unix Makefiles for VSCode to get the compile_commands.json file and work properly.

friendlyanon avatar Dec 08 '23 14:12 friendlyanon