从零开始的c++学习 1:[配置篇]

4 minute read

Published:

记录一下c++学习的步骤 : base on Course ‘CS205 C/C++ Program Design’ in 2021 Fall at Southern University of Science and Technology. | 视频

week01 配置环境: WSL + g++ + VScode

安装WSL

step1 : Install WSL on Windows 10/11

# (管理员身份)打开powershell
wsl --install
  • [小问题] 如果遇到 无法解析服务器名称或地址 的错误, 可以尝试 改一下DNS or 开魔法 or steam++.

step2 : Check requirements for running WSL 2

  • 检查win10版本 : To update to WSL 2, you must be running Windows 10.
    • For x64 systems: Version 1903 or higher, with Build 18362 or higher.
select `Windows logo key` + `R`, type `winver`, select OK

Step 3 : Enable Virtual Machine feature

# (管理员身份)打开powershell
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Step3.5 : 重启电脑

Step 4 : 更新内核 Download the Linux kernel update package

(x86版) https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi 下载完, 运行。

Step 5 : Set WSL 2 as your default version

# (管理员身份)打开powershell
wsl --set-default-version 2

Step 6 : install your Linux distribution of choice

  1. 打开 ` Microsoft Store , 搜索 Linux` ;
    1. (关了魔法才打得开)
    2. 安装完才看到是 22.04.2 LTS ; 教程选的是 20.04 LTS ; 对于学习而已 应该影响不大
  2. 找到image-20230425153930578
  3. 启动, 根据提示 输入 username, password
  • [小问题] 我等了一会儿没反应,按了回车才出现下一步…
  1. Check the version of WSL, make sure the version is 2.

     # (opt. 管理员身份)打开powershell
     wsl -l -v
    

WSL安装GCC

Step 1 : 改一下apt源

参考 : https://mirrors.sustech.edu.cn/help/ubuntu.html#introduction

# (ubuntu terminal)
sudo cp -a /etc/apt/sources.list /etc/apt/sources.list.bak

sudo sed -i "s@http://.*archive.ubuntu.com@https://mirrors.sustech.edu.cn@g" /etc/apt/sources.list
sudo sed -i "s@http://.*security.ubuntu.com@https://mirrors.sustech.edu.cn@g" /etc/apt/sources.list 

sudo apt-get update

Step 2 : 更新apt 并 安装GCC

# (ubuntu terminal)
sudo apt update
sudo apt install g++ -y
#or
sudo apt update;sudo apt install g++ -y

step2.5: check GCC version

# (ubuntu terminal)
gcc --version
#or
g++ --version

安装VScode 并配置环境

Step 1 : 上官网下对应系统版本的安装包安装 : Visual Studio Code - Code Editing. Redefined

Step 2 : 安装插件

To install them, you can search c++ and wsl respectively.

  1. C/C++ plugin
  2. Remote - WSL plugin –> (版本好像更新了, 名字只叫 WSL) 目前使用 V0.77.0

Step 3 : 将VScode 切换 WSL system

  1. 点击 VScode 界面左下角 的按钮 ,悬停会显示 ‘open a Remote Window’

  2. 在出现的选项框中选择 connect to WSL

至此,所以配置就已经完成,可以开始创建c/c++的程序了。

tips :

  • WIN + E 打开文件资源管理器,可以在左边导航栏看到linux下所有文件

c/c++处理流程: Compile –> link –> run

hello world 体验卡

//创建 helloworld.cpp
#include <iostream>
using namespace std;

int main(){
    cout << "hello world!" << endl;
    return 0;
}
# 先编译 再连接
g++ -c helloworld.cpp 
g++ helloworld.o -o hello
# 编译并连接
g++ helloworld.cpp -o hello

##run
./hello

命令行参数

//argument.cpp
#include <iostream>
using namespace std;
int main(int argc, char * argv[]){
    for (int i = 0; i < argc; i++)
        cout << i << ": " << argv[i] << endl;
}
# (ubuntu terminal)
./argument dsakjo kodspa kodasko
0: ./argument
1: dsakjo
2: kodspa
3: kodasko

# 可以利用命令行输入参数来达到我们想要的效果

编程练习1 : Write a program to initialize three variables which equal to 0.1, 0.2, 0.3, then print them with two decimal points.

//answer  filename : ex01.cpp
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
    float a = 0.1;
    float b = 0.2;
    float c = 0.3;
    cout.setf(cout.showpoint);
    cout.precision(2);
    cout << a <<endl;
    cout << b <<endl;
    cout << c <<endl;
}
# 执行
g++ -c ex01.cpp    # -->ex01.o
g++ ex01.o -o ex01 # -->ex01
./ex01             # result

编程练习2 : Copy the following code into 3 files, and compile them together to an executable file. Find the bugs if there are some.

  1. Compile main.cpp
  2. Compile add.cpp
  3. Link the two object files
//main.cpp
#include <iostream>
#include "Add.h"
int main()
{
int num1 = 2147483647;
int num2 = 1;
int result = 0;
result = add(num1, num2)
cout << The result is  << result << endl;
return 0;
}

//add.h
#pragma once
int add(int n1, int n2);

//add.cpp
#include "add.h"
int Add(int number1, int number2);
{
return n1 + n2;
}
//answer:
//main.cpp
#include <iostream>
#include "add.h"
using namespace std;

int main()
{
int num1 = 2147483647;
int num2 = 1;
int result = 0;
result = add(num1, num2); //;-->;
cout << "The result is " << result << endl; //""-->""
return 0;
}

//add.h
#pragma once
int add(int n1, int n2);

//add.cpp
#include "add.h"

int add(int number1, int number2) // Add()-->add()  and  no ;
{
return number1 + number2;
}
# 执行
g++ -c main.cpp          # -->main.o
g++ -c add.cpp      	 # -->add.o
g++ ex01.o add.o -o main # -->main
./main                   # result