文章目录
IDE CLion
安装
设置
- 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)
进入 IDEA 后, 右上角添加版本控制功能快捷操作
- 右键顶部标题栏 - 选择 Customize Toolbar …
- 在 Right 部分依次添加如下快捷操作
- 分隔符
- Main Menu - Git - VCS Group - Update Project
- Main Menu - Git - VCS Group - Commit…
- Main Menu - Git - VCS Group - Git - Push…
- Main Menu - Git - VCS Group - Git - Rollback…
- 分隔符
- 类似下面效果
插件
- Chinese (Simplified) Language Pack / 中文语言包
- Translation, 划词翻译
- CodeGlance2, 代码地图
- InLineError, 行内报错提示
- 通义灵码
MinGW
在 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
- 如果使用的是 CLion 自带的 MinGW, 则在 CLion 目录下找到
生成的 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 并重新编译
测试
特殊配置 使用 CLion 开发 C++ CUDA 应用
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).
- 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
在 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]
乱码问题可通过在 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]