02 重修C++之第一个程序

02 重修C++之第一个程序,第1张

02 重修C++之第一个程序 02 重修C++之第一个程序

上一节简单说明了环境搭建,现在我们来开始我们的第一个程序HelloWorld。

创建一个HelloWorld

首先新建一个工程目录 HelloWorld 在目录下新建两个文件夹 build src 。

HelloWorld/
├── build
└── src

在src下新建文件HelloWorld.cpp,并添加内容

#include 

using namespace std;

int main(int argc, const char** argv) {
   
    cout << "Hello World!" << endl;
    return 0;
}

接下来回到上层目录HelloWorld中,新建CMake的脚本文件CMakeLists.txt

# CMake minimum version requirements
cmake_minimum_required(VERSION 3.12)

# Support c++ 11
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
message(STATUS "optional:-std=c++11")


# Project Name
project(HelloWorld)

# Add compilation options
add_definitions("-Wall -g") #"-g" means allow using gdb debug
# or cmake with -DCMAKE_BUILD_TYPE=Debug

# include source files or head files
include_directories("./src") 

# Generate target file
add_executable(HelloWorld src/HelloWorld.cpp)

# Set up link library
target_link_libraries(HelloWorld)

这是一个比较简单的cmake脚本,我将会在以后的重修之路上更加详细地学习CMake地使用。

到此我们HelloWorld所需的所有文件都准备好了,目录结构如下:

HelloWorld/
├── build
├── src
│   └── HelloWorld.cpp
└── CMakeLists.txt
编译HelloWorld

接着前文,我们在shell中cd到build目录下执行 cmake .. 在看到成功提示后(无论成功与否在build目录下都会生成一堆文件,我们暂时不做分析(其实是我也不懂),如果成功的话,其中最重要的就是Makefile),执行 make (这个命令就是执行脚本Makefile),这会在build目录下看到一个名为HelloWorld的二进制文件,这个就是可执行文件,继续执行命令 ./HelloWorld ,运行程序就可以看到程序运行的效果了。(^C 是ctrl + c中断命令为了打分隔符提示用的,不是命令)。

[wangs7@localhost build]$ ---------------cmake------------------^C
[wangs7@localhost build]$ cmake ..
-- optional:-std=c++11
-- The C compiler identification is GNU 8.4.1
-- The CXX compiler identification is GNU 8.4.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wangs7/VSCode/HelloWorld/build
[wangs7@localhost build]$
[wangs7@localhost build]$ --------------make------------------^C
[wangs7@localhost build]$ make
Scanning dependencies of target HelloWorld
[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/src/HelloWorld.cpp.o
[100%] linking CXX executable HelloWorld
[100%] Built target HelloWorld
[wangs7@localhost build]$
[wangs7@localhost build]$ -------------run-------------------^C
[wangs7@localhost build]$ ./HelloWorld 
Hello World!
[wangs7@localhost build]$ 
调试HelloWorld

原则上HelloWorld过于简单,其实不需要调试,这里为了测试gdb基础功能,简单演示一下调试过程,如果gdb提示有缺少依赖文件,按照提示安装即可。

  • 启动HelloWorld with gdb
[wangs7@localhost build]$ gdb HelloWorld
GNU gdb (GDB) Red Hat Enterprise Linux 8.2-15.el8
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later 
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-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
.
Find the GDB manual and other documentation resources online at:
    .

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from HelloWorld...done.
(gdb) 
  • 设置断点 & 查看断点
(gdb) b main
Breakpoint 1 at 0x400845: file /home/wangs7/VSCode/HelloWorld/src/HelloWorld.cpp, line 7.
(gdb) info breakpoints 
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000400845 in main(int, char const**) 
                                                   at /home/wangs7/VSCode/HelloWorld/src/HelloWorld.cpp:7
  • 运行程序 & 查看调用栈
(gdb) r
Starting program: /home/wangs7/VSCode/HelloWorld/build/HelloWorld 

Breakpoint 1, main (argc=1, argv=0x7fffffffddc8) at /home/wangs7/VSCode/HelloWorld/src/HelloWorld.cpp:7
7           cout << "Hello World!" << endl;
(gdb) bt
#0  main (argc=1, argv=0x7fffffffddc8) at /home/wangs7/VSCode/HelloWorld/src/HelloWorld.cpp:7
(gdb) c
Continuing.
Hello World!
[Inferior 1 (process 12993) exited normally]
  • 退出 gdb
(gdb) q
[wangs7@localhost build]$ 

到此整个简单编译运行调试环境就已经搭建好了。

对于后续的C++学习我会持续更新,同时还会增加CMake和GDB使用方法学习。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4748929.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-08
下一篇 2022-11-08

发表评论

登录后才能评论

评论列表(0条)

保存