我在 macOS 上安装了 vcpkg,我正在尝试构建一个依赖于
fmt
的简单库,我是用 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内容:
cmake_minimum_required(VERSION 3.19.1)
project(MYLIB)
find_package(fmt REQUIRED)
add_library(mylib mylib.cpp)
然后
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
我错过了什么?
我在 Windows 上尝试了同样的方法,效果很好。在 Windows 上,我们运行
vcpkg integrate install
,而 macOS 上不存在。这与问题有关吗?
看来有必要
include_directories(~/vcpkg/installed/x64-osx/include)
您错误地使用了变量
CMAKE_TOOLCHAIN_FILE
。 CMakeLists.txt 中的 set(CMAKE_TOOLCHAIN_FILE ...
无效。应在命令行上设置变量,请参阅手册 CMAKE_TOOLCHAIN_FILE、在 CMake 中使用 vcpkg
cmake .. -DCMAKE_TOOLCHAIN_FILE=~/vcpkg/scripts/buildsystems/vcpkg.cmake
文件 CMakeLists.txt 也是错误的,缺少
find_package(fmt REQUIRED)
,应该通过在后台调用 fmt
来下载并安装 vcpkg install fmt
。
毕竟你应该将你的项目与库链接
target_link_libraries(MYLIB PRIVATE fmt::fmt)