clion 使用 vcpkg 进行c++环境管理(macOS、Windows)
如何使用Clion使用vcpkg进行有效的c++管理(已测试macOS平台及Windows平台),参考Clion的官方资料进行整合,会发现没法直接使用的问题,因此参考使用双vcpkg环境的方式(也可以单环境,剔除clion默认下载的哪个就可以,或者用哪个配置环境变量也可以):https://www.jetbrains.com/help/clion/package-management.html
提前准备环境
- 有效的编译器(gcc\clang\msvc)
- 安装visual studio(windows可选)
- ninja (macOS及Linux平台必选)
参考流程正常安装vcpkg
打开vcpkg管理器
View – Tool Windows – vcpkg
这个步骤最好启动一下魔法
按照顺序点+新建vcpkg环境 – 直接OK,会默认从官方github地址下载vcpkg
切换到 Manifest Mode,创建vcpkg.json ,这个模式下相当于创建一个配置单,之后使用命令就可以识别这个配置单进行安装。
试一下,点击搜索,输入自己想要找的包名,就可以正常使用了。但实际上这样操作并没有生效内容,文件没有下载,也没有导入成功。
独立安装
参考官方项目链接https://github.com/microsoft/vcpkg
Windows扩展正式使用
打开clion的控制台,输入vcpkg,发现没有命令,在安装了Visual Studio之后,输入”Developer PowerShell for VS 2022″ 打开vs的控制台
小贴士:
如果出现
无法加载文件 C:\Users\****\profile.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参
阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 3
+ . ‘C:\Users\***\Documents\WindowsPowerShell\profile.ps1’
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess可以输入Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 修改策略即可正常加载
输入Get-Command vcpkg | Out-String -Width 200找到默认的vcpkg安装位置,并对该位置创建环境变量
之后任意打开控制台即可使用vcpkg,并回到clion,测试一个包,我这里使用nlohmann-json包测试,输入nlohmann进行搜索(别搜索json,太多名字有json的包了),点击add
添加完成后,vcpkg.json自动更新,打开clion的控制台输入vcpkg install 进行安装,等待一段时间后即安装成功。
提示需要手动把包丢入cmake
Stored binaries in 1 destinations in 87.5 ms.
Elapsed time to handle nlohmann-json:x64-windows: 3.5 s
nlohmann-json:x64-windows package ABI: 73840e243b7ee9b3e8da9186302a33d97954e73241bfb4e4e2cda5e9adf011cb
Total install time: 3.5 s
The package nlohmann-json provides CMake targets:
find_package(nlohmann_json CONFIG REQUIRED)
target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json)
The package nlohmann-json can be configured to not provide implicit conversions via a custom triplet file:
set(nlohmann-json_IMPLICIT_CONVERSIONS OFF)
For more information, see the docs here:
https://json.nlohmann.me/api/macros/json_use_implicit_conversions/
修改cmake,注意粘贴target_link_libraries的时候,需要和项目名称一致
然后喜提Cmake找不到这个包(即使已经下载下来了)
需要创建一个新的Cmake环境
此时进行编译,CMake识别,注意CMake识别了之后,cpp代码部分还没有识别,此时错误的部分会有一个cmake reload图标,点击后刷新(部分时候需要重启IDE),即可刷新。此时编译可以正常生成
一份测试代码:
#include <iostream>
#include <nlohmann/json.hpp>
// 使用 nlohmann::json 别名为 json,方便后续使用
using json = nlohmann::json;
int main() {
// 创建一个 JSON 对象
json j;
// 手动赋值数据
j["id"] = "123";
j["name"] = "John Doe";
j["age"] = 30;
j["is_student"] = false;
j["hobbies"] = {"reading", "coding", "hiking"};
// 嵌套对象
j["address"] = {
{"city", "New York"},
{"zipcode", "10001"},
{"country", "USA"}
};
// 将 JSON 对象输出为字符串
std::cout << "JSON output:\n" << j.dump(4) << std::endl;
// 读取 JSON 数据
std::string name = j["name"];
int age = j["age"];
bool is_student = j["is_student"];
std::string city = j["address"]["city"];
// 输出读取的数据
std::cout << "\nParsed data:\n";
std::cout << "Name: " << name << "\n";
std::cout << "Age: " << age << "\n";
std::cout << "Is Student: " << std::boolalpha << is_student << "\n";
std::cout << "City: " << city << "\n";
// 修改 JSON 数据
j["age"] = 31; // 更新年龄
j["hobbies"].push_back("gaming"); // 添加新爱好
// 输出修改后的 JSON
std::cout << "\nUpdated JSON:\n" << j.dump(4) << std::endl;
return 0;
}
macOS 扩展正式使用
熟悉vcpkg命令和模式
manifest 模式
Manifest 模式是vcpkg引入的一种新的包管理方式,利用vcpkg.json
文件来管理项目依赖。通过这种方式,项目的依赖项和版本信息被明确地定义在一个配置文件中,允许vcpkg为项目自动安装所有必要的依赖项。Manifest 模式更适合于需要管理多个包版本或者不同环境配置的场景。
vcpkg new --application
vcpkg add port fmt
直接install模式
直接安装模式是vcpkg的传统安装方式,在这种模式下,你直接通过命令安装一个或多个包。这种方式适用于单个包的快速安装,或者你不需要版本控制的简单项目。
vcpkg install fmt
BUG及解决
和anaconda环境包同名之后发生环境冲突
以fmt为例,cmake使用vcpkg的包回合anaconda的一个同名的fmt包冲突,此时需要屏蔽anaconda环境识别才能正常进行cmake编译
可以在CMake命令中手动指定vcpkg路径
set(CMAKE_TOOLCHAIN_FILE "/path/to/vcpkg/scripts/buildsystems/vcpkg.cmake" CACHE STRING "Vcpkg toolchain file")
也可以在Clion中自定义环境变量
Settings > Build, Execution, Deployment > CMake
选择项目的配置(如 Debug
或 Release
),在 Environment 中设置环境变量,例如
PATH=/path/to/vcpkg:/usr/local/bin:/usr/bin # 将vcpkg路径放在前面
0 条评论