linux初步了解-01

本文详细介绍了在Linux环境下使用Visual Studio Code和CMake进行C/C++开发的过程,涉及常用指令、文件操作、vim编辑器、gcc编译器、CMake配置以及开发环境的搭建。适合初学者和进阶开发者参考。
摘要由CSDN通过智能技术生成

基于Vscode和CMake实现C/C++开发

linux常用指令

linux系统一切皆为文件:对于文件的操作有:创建文件,编辑文件,保存文件,关闭文件,重命名文件,删除文件,恢复文件。

  1. pwd;显示当前路径
  2. ls;列出当前路径下的全部文件
  3. mkdir;创建文件夹
  4. touch;创建文件
  5. rm;删除文件;删除文件夹时需要加 -r;-r表示递归复制
  6. cp;移动文件;移动文件夹时需要加 -r
  7. tree;树形结构
  8. mv;移动文件和文件夹,文件和文件夹重命名
  9. 增加了 检查列表 功能。
  10. crtl+l:实现命令行清屏幕。
  11. man:查看命令如何使用,用来查不会使用的命令;举例:man ls;help cd
  12. reboot:重启linux
  13. shutdown -h now;立即关机

文件编辑

vim具有程序开发的能力,也可以进行对文件进行简单的编辑。被称作编辑器之神。学会vim,可以在linux的世界中畅通无阻。一定要学会vim
gedit:linux系统下的纯文本编辑器;可视化比较好!读文件可以多用这个编辑器
nano:linux系统自带,不好用,gedit最简单。

开发环境搭建:

1、在linux系统下安装任何一个软件,首先要在终端执行这条指令

sudo apt update

理由:

sudo apt install build-essential gdb

确认是否安装成功的指令:

gcc --version //要输入的指令
gcc (Ubuntu 7.3.0-16ubuntu3) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
g++ --version //要输入的指令
g++ (Ubuntu 7.3.0-16ubuntu3) 7.3.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

gdb --version //要输入的指令
GNU gdb (Ubuntu 8.1-0ubuntu3) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".

安装cmake:

 sudo apt install cmake
cmake --version //要输入的指令
cmake version 3.10.2

第三讲:GCC编译器

1、linux开发C/C++一定要熟悉GCC
代码编译的整个过程:
linux系统中运行可执行文件:./test 运行当前文件夹下的test(这是一个可执行文件)

利用这条指令:

g++ test.cpp -o test

和这四条指令

g++ -E test.cpp  -o test.i
g++ -S test.i -o test.s
g++ -c test.s -o test.o
g++ test.o -o test

生成的可执行文件是一模一样的;所占内存大小一样;功能也是一样的。

2、g++重要编译参数
<1>-g 编译带调试信息的可执行文件

//产生带调试信息的可执行文件
g++ -g test.cpp -o test

<2> -O[n] :优化源代码 一般n = 2 可以优化自己写的一些比较低效的代码
<3>指定库文件:不理解;
<4> 指定头文件搜索目录
<5> 打印警告信息:

g++ wall test.cpp

<6>关闭警告信息:

g++ -w test.cpp

<7> -std=c++11设置编译器标准:看文档补充

g++ -std=c++11 test.cpp

第五讲 配置Vscode

1、vscode的安装,在官网下载安装包,用命令行安装
2、插件安装C/C++ ,Cmake,Cmake toos
3、Vscode快捷键

Vscode中C++实验:
好像不需要配置C++文件,利用g++进行编译链接;生成可执行文件,记得先保存再进行编译链接。
helloworld实验:

fanbing@fb:~/testcpp/5_3$ g++ helloworld.cpp -o helloworld
fanbing@fb:~/testcpp/5_3$ ls
helloworld  helloworld.cpp
fanbing@fb:~/testcpp/5_3$ ./helloworld 
hello world,linux!
fanbing@fb:~/testcpp/5_3$ 

交换两个数实验:

//第一条命令不懂
fanbing@fb:~/testcpp/5_3/5_3_2$ g++ main.cpp src/swap.cpp -Iinclude -o main
fanbing@fb:~/testcpp/5_3/5_3_2$ ls
include  main  main.cpp  src
fanbing@fb:~/testcpp/5_3/5_3_2$ ./main
m_a=10
m_b=20
m_a=20
m_b=10
fanbing@fb:~/testcpp/5_3/5_3_2$ ls -lh
总用量 28K
drwxr-xr-x 2 fanbing fanbing 4.0K Feb  6 19:52 include
-rwxr-xr-x 1 fanbing fanbing  14K Feb  6 20:19 main
-rw-r--r-- 1 fanbing fanbing  138 Feb  6 20:18 main.cpp
drwxr-xr-x 2 fanbing fanbing 4.0K Feb  6 19:53 src

第六讲 CMake

1、特点:CMake跨平台的编译工具,所有平台;C++项目标配

指令是大小写无关的,参数和变量是大小相关的;
基本语法格式:
4、在linux平台下使用CMake构建C/C++工程的流程如下:

a:手动编写CmakeList.txt
b.执行命令cmake PATH 生成 Makefile(PATH是顶层CMakeLists.txt所在的目录)
c.执行命令make进行编译

add_executable(helloworld_cmake helloworld.cpp)
g++ helloworld.cpp -o helloworld

上述两条指令的作用是相同的;

通过CMake的命令去生成Makefile

推荐使用:外部构建:两个点;内部构建会产生太多的中间文件

为什么要使用Cmake:在面对大项目时,g++写起来太复杂,用CMake更佳

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值