C++学习日记 | LAB 1 Environment configuration 环境配置

本文介绍了如何在Windows10中使用WindowsSubsystemforLinux(WSL)和VisualStudioCode(VSCode)开发C++,包括下载安装、设置环境及解决链接依赖问题。还提供了习题6.2的步骤和注意事项,强调了变量类型、取值范围和函数一致性的重要性。
摘要由CSDN通过智能技术生成

资料来源:南科大 于仕琪 C/C++ Program Design
LINK:CPP/week01 at main · ShiqiYu/CPP · GitHub


本节内容

  • Download and install GCC on Windows 10 (Based on Windows Subsystem for Linux)
  • Download and install the editor (VSCode)
  • Compile, link and run C/C++ programs
  • Set output format

收获

  • 在Linux下开发C++比较快捷,因此连用WSL和VScode是一个很好的选择。
  • 多个相互链接的CPP,编译时,确保将依赖的文件放在主程序的左边。否则编译时会出现没有编译出所调用的.o文件而报错。
    正确编译命令:
g++ -c main.cpp add.cpp
g++ -o main main.cpp add.cpp

部分习题笔记

习题6.2

Copy the following code into 3 files, and compile them together to an executable file. Find the bugs if there are some.
Step 1: Compile main.cpp
Step 2: Compile add.cpp
Step 3: Link the two object files.
在这里插入图片描述

Note

  • 注意变量类型对应取值范围
  • 注意分号
  • 注意函数名称要一致

Answer

main.cpp

#include <iostream>
#include "add.h"

using namespace std;
int main()
{
    long num1 = 2147483647;
    long num2 = 1;
    long result = 0;

    result = add(num1, num2);
    cout << "The result is" << result << endl;
    return 0;
}

add.h

#pragma once

long add(long n1, long n2);

add.cpp

#include "add.h"

long add(long number1, long number2)
{
    return number1 + number2;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

电子异术家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值