简体   繁体   English

cmake 在 macOS 上用 vcpkg 找不到 header 文件

[英]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.我在 macOS 上安装了 vcpkg,我正在尝试构建一个依赖于fmt的简单库,我用 vcpkg 安装了它。

mylib.h mylib.h

float add(float a, float b);

mylib.cpp 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: CMakeLists.txt 内容:

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.我在 Windows 上进行了同样的尝试,效果很好。 On Windows though we run vcpkg integrate install which does not exist on macOS.尽管我们在 Windows 上运行vcpkg integrate install ,但它在 macOS 上不存在。 Is this related to the problem?这与问题有关吗?

You are using the variable CMAKE_TOOLCHAIN_FILE incorrectly.您错误地使用了变量CMAKE_TOOLCHAIN_FILE 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 set(CMAKE_TOOLCHAIN_FILE... in CMakeLists.txt 无效。变量应在命令行上设置,请参阅手册CMAKE_TOOLCHAIN_FILE将 vcpkg 与 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.文件 CMakeLists.txt 也是错误的, find_package(fmt REQUIRED) ,应该通过调用vcpkg install fmt下载并安装fmt

After all you should link your project with the lib毕竟你应该将你的项目与 lib 链接起来

target_link_libraries(MYLIB PRIVATE fmt::fmt)

Looks like it's necessary to看来有必要

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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