640 字
3 分钟
CS205 Lab01
TIP

Welcome to my CS205 lecture notes! Because the lecture is not in English, I will try my best to translate it. And at the same time, the PPT,lab-file also use the English,I will write the English notes but not all.

NOTE

If you have a passion to konw more about the course, you can click the link below to learn more about the course. Read the repo.

MongxinChan
/
CPP
Waiting for api.github.com...
00K
0K
0K
Waiting...

Lab 1#

Task 1 Install WSL#

install wsl

wsl --install

In this task,I fail to install wsl. So I do this :

wsl -l -o
wsl --install -d Ubuntu-20.04

image-20250318003050449

That’s my successful examples.

DISM.exe /Online /Enable-Feature /FeatureName:VirtualMachinePlatform /All /NoRestart

image-20250318003517641

use the bash to start the system:

wsl -l -v
wsl -d Ubuntu-24.04

the username and password is

I donnot update in the markdown = =

image-20250318004410701

4png

we see that the Ubuntu-24.04 has been installed.

image-20250318004758417

sudo apt update
sudo apt install g++ -y

we use the commond to check their version up

gcc --version

image-20250318090019674

Task 2 Use the WSL in visual studio code#

Open the vscode(Downloaded)

7png

We use the Ctrl+Alt+O to command the system, and wait for a while to install the server of WSL

If the connection is successful you will see:

8png

9png

/mnt/d/Code/Cplusplus/SUST Cpp course/ is a file path, typically used in a Linux system to indicate a directory within a mounted Windows file system. Here’s a breakdown of its components:

  • /mnt/d: In Linux, /mnt is a mount point directory commonly used for mounting external storage devices or partitions. /mnt/d indicates that the D drive from a Windows system is mounted here.

image-20250404170927050

image-20250404171355890

The step called Compile

image-20250404171509663

We create the .exe post-name-file. The step called Linking

image-20250404171802256

The default output executable file is called “a.exe”(Windows) or “a.out”(Unix and Mac OS) if you don’t specify the name in compiling and linking step.

image-20250404171945336

You need to use g++ to compile C++ program. The -o option is used to specify the output file name.

image-20250404172756641

Task# 4Terminal Output#

printf(format-control-string, other-arguments) format-control-string describes the output format, which consists of conversion specifiers, field widths, precisions and literal characters with percent sign(%).

Picture

Task# 5 Execrise#

We couldnot directly compile the main.cpp if we only compile the main.cpp advoke g++ -o main main.cpp && ./main

image-20250404173852592

  1. 方法

    • 编译 add.cpp 文件生成对象文件

      g++ -c add.cpp -o add.o
    • 编译 main.cpp 文件生成对象文件

      g++ -c main.cpp -o main.o
    • 链接两个对象文件生成可执行文件:

      g++ main.o add.o -o myprogram.exe
      gcc -c add.cpp -o add.o
      gcc -c main.cpp -o main.o
      gcc main.o add.o -o myprogram -lstdc++
  2. Cmake

  • 步骤 1: 创建 CMakeLists.txt 文件

    在你的项目根目录下创建一个名为 CMakeLists.txt 的文件。这个文件将包含构建项目的指令。

    cmake_minimum_required(VERSION 3.10)
    
    # 项目名称
    project(MyProject)
    
    # 设置 C++ 标准
    set(CMAKE_CXX_STANDARD 11)
    set(CMAKE_CXX_STANDARD_REQUIRED True)
    
    # 添加源文件
    add_executable(myprogram main.cpp add.cpp)
  • 步骤 2: 创建构建目录

    在项目根目录下创建一个名为 build 的目录,用于存放构建文件。

    mkdir build
    cd build
  • 步骤 3: 运行 CMake

    build 目录中运行 cmake 命令,指定源代码目录。

    cmake

    这个命令会读取 CMakeLists.txt 文件,并生成适合你的系统的构建文件(如 Makefile 或 Visual Studio 解决方案文件)。

  • 步骤 4: 编译项目

    使用生成的构建文件编译项目。

  • 如果你使用的是 Makefile(通常是在 Unix-like 系统上):

    make
  • 如果你使用的是 Visual Studio(在 Windows 上):

    打开生成的解决方案文件,并在 Visual Studio 中构建项目。

completed

CS205 Lab01
https://loners.site/posts/cs205-lab01/
作者
Mongxin Chan
发布于
2025-03-18
许可协议
CC BY-NC-SA 4.0