简体   繁体   中英

cmake with vcpkg on macOS cannot find header files

I installed vcpkg on macOS and I'm trying to build a simple library that depends on fmt , which I installed with vcpkg.

mylib.h

float add(float a, float b);

mylib.cpp

#include "mylib.h"
#include <iostream>
#include <fmt/core.h>

float add(float a, float b)
{
    fmt::print("Hello MYLIB, world!\n");
    return (a + b);
}

CMakeLists.txt contents:

cmake_minimum_required(VERSION 3.19.1)

project(MYLIB)

find_package(fmt REQUIRED)

add_library(mylib mylib.cpp)

Then

user@users-MacBook-Pro build % cmake -B . -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake -S ..              
-- The C compiler identification is AppleClang 12.0.0.12000032
-- The CXX compiler identification is AppleClang 12.0.0.12000032
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/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: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/user/mylib/build
user@users-MacBook-Pro build % make
Scanning dependencies of target mylib
[ 50%] Building CXX object CMakeFiles/mylib.dir/mylib.cpp.o
/Users/user/mylib/mylib.cpp:5:10: fatal error: 'fmt/core.h' file not found
#include <fmt/core.h>
         ^~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/mylib.dir/mylib.cpp.o] Error 1
make[1]: *** [CMakeFiles/mylib.dir/all] Error 2
make: *** [all] Error 2

What am I missing?

I tried the same on Windows and it works fine. On Windows though we run vcpkg integrate install which does not exist on macOS. Is this related to the problem?

You are using the variable CMAKE_TOOLCHAIN_FILE incorrectly. set(CMAKE_TOOLCHAIN_FILE... in CMakeLists.txt has no effect. The variable should be set on the command line, see the manuals CMAKE_TOOLCHAIN_FILE , Using vcpkg with CMake

cmake .. -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake

The file CMakeLists.txt is also wrong, find_package(fmt REQUIRED) is missing, that should download and install fmt by invoking vcpkg install fmt under the hood.

After all you should link your project with the lib

target_link_libraries(MYLIB PRIVATE fmt::fmt)

Looks like it's necessary to

include_directories(~/vcpkg/installed/x64-osx/include)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM