C++ 环境配置及HelloWorld

[TOC]

官网下载 x86_64-posix-seh

VSCode

配置参考

CSDN VS Code gcc配置C++环境(Mingw-w64)

C/C++ for Visual Studio Code

1
2
3
4
5
6
7
8
9
# cltr+shift+P 打开命令面板
# 1 输入C/C++ 选择编辑配置Ui
- c_cpp_properties.json
# 2 输入tasks 选择任务-配置默认构建任务
- tasks.json
# 3 输入launch,选择调试,打开launch.json
- launch.json
# 4 运行配置(auto gen)
- settings.json

运行:

​ 1、终端–运行生成任务(Ctrl+Shift+B)

​ 2、F5 调试(Ctrl+F5 运行)

Cmake

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 新建项目
# 插件市场安装 Cmake Tools

# 创建CmakeList.txt
--
cmake_minimum_required (VERSION 3.0.0)
project(Cmake_demo)

add_executable(demo, "main.cpp", "foo.cpp", "foo.h")
---

# ctrl+shift+P 打开命令面板,输入configure检索并执行“Cmake:Configure”命令
# 执行Configure,选择一个kit

Configure 完成的输出

VS窗口底部默认又一些按钮,可以点击`build`按钮,就可以构建目标all。

_GLIBCXX_USE_CXX11_ABI宏的作用

参考GCC提供的手册:

在 GCC 5.1 版本中,libstdc++ 引入了一个新特性,其中包括 std::string 和 std::list 的新实现。为了符合 C++11 标准,这些更改是必要的,该标准禁止 Copy-On-Write 字符串并要求列表跟踪其大小。

这样虽然符合了c++11的标注,旧版就无法兼容了。为了解决这个问题,对于旧版而言,GCC5.1添加了cxx11命名空间,GCC5.1或者说c++11规范下的string和list,实际上是std::cxx11::string和std::cxx11::list,所以我们一般的using namespace std就会变成形如using namespace std::cxx11的样子。也就是说,有旧版(c++03规范)的libstdc++.so,和新版(c++11规范)的libstdc++.so两个库同时存在。

为了避免两个库到底选择哪一个的麻烦,GCC5.1就引入了-D_GLIBCXX_USE_CXX11_ABI来控制编译器到底链接哪一个libstdc++.so,

-D_GLIBCXX_USE_CXX11_ABI=0 链接旧版库
-D_GLIBCXX_USE_CXX11_ABI=1 链接新版库

如果在CMakeLists里设置

add_definitions(-D _GLIBCXX_USE_CXX11_ABI=0)
1
Reference:

[1]. https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

ERR

‘’’’ The terminal process “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command g++ -g D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/ddz_move_star.cpp D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/main.cpp -I D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/ -o main.exe” terminated with exit code: 1.

  • Terminal will be reused by tasks, press any key to close it.

  • Executing task: g++ -g D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/main.cpp D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/ddz_move_star.cpp -I D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/ -o main.exe

C:\Users\Simon\AppData\Local\Temp\ccwu1HiS.o: In function main': D:/Qiansi/games/DDZ-QS-Server/AI_server_Star_call/main.cpp:22: undefined reference toddzmove_star::get_move_type(int*)’
collect2.exe: error: ld returned 1 exit status

  • The terminal process “C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command g++ -g D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/main.cpp D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/ddz_move_star.cpp -I D:\Qiansi\games\DDZ-QS-Server\AI_server_Star_call/ -o main.exe” terminated with exit code: 1.
  • Terminal will be reused by tasks, press any key to close it.
1
2
3
#include <ddz_move_star.h>
--->
#include "ddz_move_star.h"

ERR2

CMake Error: Error: generator : Ninja

Does not match the generator used previously: MinGW Makefiles

Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.

—> 删除缓存