Windows C/C++ CLion 开发环境搭建

博文目录


IDE CLion

安装

官网
官方全版本下载

CLion-2021.2.3.exe 或 CLion-2021.2.3.win.zip, 从 2021.3 起, IDE Eval Reset 不再可用, 复制评估 key 的方式从 2021.2.3 起不再可用

设置

  • File | Settings | Appearance & Behavior | System Settings - Project
    • 取消勾选 Reopen projects on startup
    • Open project in 选择 New Window
    • Default project directory 选择为 CLion 分配的工作空间目录, 如 C:\mrathena\develop\workspace\clion
  • File | Settings | Editor | General | Code Completion
    • 取消勾选 Match case
  • File | Settings | Editor | General | Editor Tabs
    • Appearance - Tab placement 选 Left
    • Closing Policy - Tab limit 填 100
  • File | Settings | Editor | Font
    • Font 选 Courier New
    • Size 填 16
  • File | Settings | Build, Execution, Deployment | Toolchains
    • 添加编译器 MinGW, 有捆绑的用捆绑的, 否则自行下载配置 (点击 Environment 后面的 download)

MinGW

MinGW与Clion下载安装及使用详解

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在 SourceForge 的跳转页, 往下翻, 直到 MinGW-W64 Online Installer

  • win32: Windows
  • posix: Linux / Unix / Mac OS 等
  • seh: 新的, 性能好的, 不支持 32 位. 64 位系统选 seh
  • sjlj: 旧的, 稳定性好, 支持 32 位

解压到合适目录

在这里插入图片描述

如何在 UTF-8 编码下在控制台正常输出中文

  • 参考 这里
  • 双击 Shift 键, 搜索 Registry 并打开, 取消 run.processes.with.pty 选项的勾选
  • 也可以在 main 最前面加 setbuf(stdout, NULL) 来关闭输出缓存, 避免因上面的修改导致的一些其他问题

生成的 exe 运行时报错, 缺失 libgcc_s_seh-1.dll / libstdc+±6.dll

  • 将编译器 MinGW 的 bin 路径配置到 Path 环境变量, 这两个 dll 在该路径中
    • 如果使用的是 CLion 自带的 MinGW, 则在 CLion 目录下找到 bin\mingw\bin 路径并配置到 Path

生成的 exe 在没有 MinGW 环境的机器上如何正常运行

  • 将依赖的 dll 一同拷贝到 exe 目录下, 可以借助 dependencies 工具, 参考 这里
  • 尝试过在 CMakeLists.txt 中添加如下配置, 都不行
    • set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
    • set(CMAKE_EXE_LINKER_FLAGS "-static")
    • set(CMAKE_EXE_LINKER_FLAGS -static-libgcc -static-libstdc++)
    • set(CMAKE_EXE_LINKER_FLAGS -static

在命令行 CMD 中运行 exe 时如何正常输出中文

  • 暂时只能将 cpp 源文件编码转换成 GBK 并重新编译

插件

  • Manage Plugin Repositories 添加知了插件源 plugins.zhile.io
    • 安装 IDE Eval Reset 插件, 记得勾选 Logout when reset 和 Auto reset before per restart, 注意, 2021.3 起不再可用
  • Chinese (Simplified) Language Pack / 中文语言包
  • Translation, 划词翻译
  • CodeGlance2, 代码地图
  • InLineError, 行内报错提示

测试

在这里插入图片描述

特殊配置 使用 CLion 开发 C++ CUDA 应用

Set up the CUDA compiler

On Windows, CUDA projects can be developed only with the Microsoft Visual C++ toolchain. Check the toolchain settings to make sure that the selected architecture matches with the architecture of the installed CUDA toolkit (usually, amd64).

Microsoft Visual C++

  • Install Visual Studio 2013, 2015, 2017, or 2019 on your system.
  • In CLion, go to File | Settings | Build, Execution, Deployment | Toolchains.
  • Click Icons general add and select Visual Studio from the list of toolchain templates.
  • Check the Environment field. CLion will attempt to automatically detect the installed Visual Studio distribution. If the detection fails, set the path to Visual Studio manually.
  • If required, specify the Architecture (x86, amd64, x86_arm, or another), Platform (store, uwp, onecore, or leave it blank), and Version. To build your project for the selected architecture, CLion will call the script to configure the environment with the specified parameters.
    • If the version of your compiler toolset is earlier than the version of your Visual Studio installation, pass it in the Version field via the vcvars_ver flag, for example, -vcvars_ver=14.16.
  • Wait until the tools detection is finished
    在这里插入图片描述

CLion 开发 CUDA 应用, 工具链必须使用 Visual Studio. 安装 Visual Studio 时必须选择 C++ 桌面开发里的 MSVC 和 Windows SDK

我的 CLion 是 2021.2.3, 最高支持 VS 2019, 所以我下载并安装了 VS 2019 社区版

Visual Studio 较旧的下载 - 2019、2017、2015 和以前的版本
Visual Studio Installer 2019.exe

CLion配置visual studio(msvc)和JOM多核编译

在这里插入图片描述
在这里插入图片描述

在 Toolchains 里创建 Visual Studio 并设置成默认(点到第一个), 确保默认的测试编译能成功通过(无法通过的例子见下面弯路说明)

在这里插入图片描述

CMake 里要确认 Toolchains 与当前使用的 Toolchains 对应

注意不要走如下弯路

先说结论, MSVC 和 Windows SDK 都得安装

我根据经验, 觉得配置 Visual Studio 工具链应该只需要用到 MSVC 编译器, 所以最开始就只安装了 MSVC. 结果在配置 Visual Studio 工具链的时候, 怎么都无法通过测试验证. 同时使用该工具链的任何工程做 Reload CMake Project 操作都会报相同的错. 因为输出信息乱码而不知道具体内容, 尝试百度错误代码也没百出来, 无奈只能先解决乱码问题

在这里插入图片描述

C:\mrathena\develop\clion-2021.2.3\bin\cmake\win\bin\cmake.exe -DCMAKE_BUILD_TYPE=Debug -DCMAKE_DEPENDS_USE_COMPILER=FALSE -G "CodeBlocks - NMake Makefiles" C:\mrathena\develop\workspace\clion\test
-- The C compiler identification is MSVC 19.29.30146.0
-- The CXX compiler identification is MSVC 19.29.30146.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/mrathena/develop/visual.studio-2019/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe
-- Check for working C compiler: C:/mrathena/develop/visual.studio-2019/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - broken
CMake Error at C:/mrathena/develop/clion-2021.2.3/bin/cmake/win/share/cmake-3.20/Modules/CMakeTestCCompiler.cmake:66 (message):
  The C compiler

    "C:/mrathena/develop/visual.studio-2019/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: C:/mrathena/develop/workspace/clion/test/cmake-build-debug/CMakeFiles/CMakeTmp
    
    Run Build Command(s):nmake -f Makefile /nologo cmTC_0a29f\fast && 	C:\mrathena\develop\visual.studio-2019\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\nmake.exe  -f CMakeFiles\cmTC_0a29f.dir\build.make /nologo -L                  CMakeFiles\cmTC_0a29f.dir\build
    Building C object CMakeFiles/cmTC_0a29f.dir/testCCompiler.c.obj
    	C:\mrathena\develop\clion-2021.2.3\bin\cmake\win\bin\cmake.exe -E cmake_cl_compile_depends --dep-file=CMakeFiles\cmTC_0a29f.dir\testCCompiler.c.obj.d --working-dir=C:\mrathena\develop\workspace\clion\test\cmake-build-debug\CMakeFiles\CMakeTmp --filter-prefix="" -- C:\mrathena\develop\visual.studio-2019\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\cl.exe @C:\Users\mrathena\AppData\Local\Temp\nmDFDE.tmp
    Linking C executable cmTC_0a29f.exe
    	C:\mrathena\develop\clion-2021.2.3\bin\cmake\win\bin\cmake.exe -E vs_link_exe --intdir=CMakeFiles\cmTC_0a29f.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\mrathena\develop\visual.studio-2019\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\cmTC_0a29f.dir\objects1.rsp @C:\Users\mrathena\AppData\Local\Temp\nmE07B.tmp
    RC Pass 1: command "rc /fo CMakeFiles\cmTC_0a29f.dir/manifest.res CMakeFiles\cmTC_0a29f.dir/manifest.rc" failed (exit code 0) with the following output:
    绯荤粺鎵句笉鍒版寚瀹氱殑鏂囦欢銆侼MAKE : fatal error U1077: 鈥淐:\mrathena\develop\clion-2021.2.3\bin\cmake\win\bin\cmake.exe鈥�: 杩斿洖浠g爜鈥�0xffffffff鈥�
    Stop.
    NMAKE : fatal error U1077: 鈥淐:\mrathena\develop\visual.studio-2019\VC\Tools\MSVC\14.29.30133\bin\HostX64\x64\nmake.exe鈥�: 杩斿洖浠g爜鈥�0x2鈥�
    Stop.
    
    

  

  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


-- Configuring incomplete, errors occurred!
See also "C:/mrathena/develop/workspace/clion/test/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "C:/mrathena/develop/workspace/clion/test/cmake-build-debug/CMakeFiles/CMakeError.log".

[Finished]

Clion编译输出乱码问题解决方案

乱码问题可通过在 Help | Edit Custom VM Optisions… (不存在则点击 Create) 末尾添加如下两个参数解决. 添加后重启 CLion

-Dconsole.encoding=UTF-8
-Dfile.encoding=UTF-8

在这里插入图片描述

根据描述找到一篇 文章 解决了这个问题, 在 CMake 里配置参数 -G "Visual Studio 16 2019". 有用, 但是又出了一个新的报错. 这次输出内容里涉及到 Windwos SDK, 让我联想到 VS Install 里面的可选安装 Windows 10 SDK, 同时我也想起来看到过有 文章 里就只安装 MSVC 和 Windows SDK. 试了一下同时安装这两项内容. 试了一下, 两项都装果真就直接成了

C:\mrathena\develop\clion-2021.2.3\bin\cmake\win\bin\cmake.exe -G "Visual Studio 16 2019" C:\mrathena\develop\workspace\clion\test
-- Selecting Windows SDK version  to target Windows 10.0.22621.
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:2 (project):
  No CMAKE_C_COMPILER could be found.



CMake Error at CMakeLists.txt:2 (project):
  No CMAKE_CXX_COMPILER could be found.



-- Configuring incomplete, errors occurred!
See also "C:/mrathena/develop/workspace/clion/test/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "C:/mrathena/develop/workspace/clion/test/cmake-build-debug/CMakeFiles/CMakeError.log".

[Finished]

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CLion配置C/C++环境的方法可以按照以下步骤进行操作: 1. 打开CLion,并创建一个新的C/C++项目。 2. 在项目设置中,选择"File" -> "Settings"。 3. 在设置窗口中,选择"Build, Execution, Deployment" -> "Toolchains"。 4. 点击"+"按钮,选择你的C/C++编译器,并设置编译器的路径。 5. 点击"Apply"保存设置。 这样,你就成功配置CLion的C/C++环境。你可以使用CLion来编写和调试C/C++代码了。 另外,如果你想在C++中使用Python的Matplotlib库进行绘图,你可以使用开源项目lava/matplotlib-cpp为C++提供的接口。这个接口使得你可以在C++中使用Python的Matplotlib库进行绘图。你需要在C++代码中包含相应的头文件,并按照接口的使用方式进行调用。同样地,如果你想使用MathGL库进行统计图形的绘制,你可以在C++代码中包含相应的头文件,并按照MathGL库的调用方式进行绘图操作。另外,还有一些其他的C++图表库可供选择,如QtiPlot和Qt+Echarts等。这些库都提供了C++接口,可以方便地在C++中进行图表绘制。 希望这些信息对你有帮助!\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [C++可视化和图表库](https://blog.csdn.net/kupe87826/article/details/126911034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值