ubuntu c 调试 ubuntu系统安装软件
其实ubuntu c 调试的问题并不复杂,但是又很多的朋友都不太了解ubuntu系统安装软件,因此呢,今天小编就来为大家分享ubuntu c 调试的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!
ubuntu下怎么编程c++
我也正在学习ubuntu系统下的程序设计,以前都是在Windows下的vs中编写,很方便,啥都有,可是转到ubuntu下后两眼一抹黑啥也不情况了,经过一段时间的摸索,还是知道一些的了。和你分享,希望共同进步。
1.首先ubuntu系统自带了C编译器gcc,C++编译器g++,你看下你的系统中有没有安装,如果没有在命令终端(ctrl+T)下输入:sudo apt-get install gcc
就可以安装gcc编译器了。
2.当初我也很纳闷用什么写程序呢,vs中定义好的环境很适合写代码,可ubuntu的用什么的,经查询请教知道,用vim或者emacs,一个叫做神的编辑器,一个叫做编辑器之神,当然都是说这两个编辑器都很厉害,各有所长,就看自己的喜好了,我用的是vim编辑器。
3在终端中输入VIM就能打开vim编辑器,至于vim怎么操作,里面的命令很多,简单的一两句话也说不清楚,建议你找本书好好看看。编写好了一个文件后保存为123.cpp
4一定要保证你的终端是在你保存的文件的目录下,不然每次你输入都要写上全部目录路径。
g++-0 123 123.cpp
意思是将123.cpp这个源文件使用g++编译器编译,编译后的输出是123可执行文件
5在终端下输入123,就能显示你代码的执行结果了。
大致的过程就是这些了,当然具体的过程中可能出现很多问题。要用GDB调试啥的,我还不会,正在学习中。
希望对你有帮助,共同进步。
linux c 段错误如何定位
1.段错误是什么
一句话来说,段错误是指访问的内存超出了系统给这个程序所设bai定的内存空间,例如访问了不存在的内存地址、访问了系统保护的内存地址、访问了只读的内存地址等等情况。这里贴一个对于“段错误”的准确定义(参考Answers.com):
A segmentation fault(often shortened to segfault) is a particular error condition that can occur during the operation of computer software. In short, a segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed(e.g., attempts to write to a read-only location, or to overwrite part of the operating system). Systems based on processors like the Motorola 68000 tend to refer to these events as Address or Bus errors.
Segmentation is one approach to memory management and protection in the operating system. It has been superseded by paging for most purposes, but much of the terminology of segmentation is still used,"segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy.
On Unix-like operating systems, a process that accesses invalid memory receives the SIGSEGV signal. On Microsoft Windows, a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION exception.
2.段错误产生的原因
2.1访问不存在的内存地址
#include<stdio.h>
#include<stdlib.h>
void main()
{
int*ptr= NULL;
*ptr= 0;
}
2.2访问系统保护的内存地址
#include<stdio.h>
#include<stdlib.h>
void main()
{
int*ptr=(int*)0;
*ptr= 100;
}
2.3访问只读的内存地址
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char*ptr="test";
strcpy(ptr,"TEST");
}
2.4栈溢出
#include<stdio.h>
#include<stdlib.h>
void main()
{
main();
}
等等其他原因。
3.段错误信息的获取
程序发生段错误时,提示信息很少,下面有几种查看段错误的发生信息的途径。
3.1 dmesg
dmesg可以在应用程序crash掉时,显示内核中保存的相关信息。如下所示,通过dmesg命令可以查看发生段错误的程序名称、引起段错误发生的内存地址、指令指针地址、堆栈指针地址、错误代码、错误原因等。以程序2.3为例:
panfeng@ubuntu:~/segfault$ dmesg
[ 2329.479037] segfault3[2700]: segfault at 80484e0 ip 00d2906a sp bfbbec3c error 7 in libc-2.10.1.so[cb4000+13e000]
3.2-g
使用gcc编译程序的源码时,加上-g参数,这样可以使得生成的二进制文件中加入可以用于gdb调试的有用信息。以程序2.3为例:
panfeng@ubuntu:~/segfault$ gcc-g-o segfault3 segfault3.c
3.3 nm
使用nm命令列出二进制文件中的符号表,包括符号地址、符号类型、符号名等,这样可以帮助定位在哪里发生了段错误。以程序2.3为例:
panfeng@ubuntu:~/segfault$ nm segfault3
08049f20 d _DYNAMIC
08049ff4 d _GLOBAL_OFFSET_TABLE_
080484dc R _IO_stdin_used
w _Jv_RegisterClasses
08049f10 d __CTOR_END__
08049f0c d __CTOR_LIST__
08049f18 D __DTOR_END__
08049f14 d __DTOR_LIST__
080484ec r __FRAME_END__
08049f1c d __JCR_END__
08049f1c d __JCR_LIST__
0804a014 A __bss_start
0804a00c D __data_start
08048490 t __do_global_ctors_aux
08048360 t __do_global_dtors_aux
0804a010 D __dso_handle
w __gmon_start__
0804848a T __i686.get_pc_thunk.bx
08049f0c d __init_array_end
08049f0c d __init_array_start
08048420 T __libc_csu_fini
08048430 T __libc_csu_init
U __libc_start_main@@GLIBC_2.0
0804a014 A _edata
0804a01c A _end
080484bc T _fini
080484d8 R _fp_hw
080482bc T _init
08048330 T _start
0804a014 b completed.6990
0804a00c W data_start
0804a018 b dtor_idx.6992
080483c0 t frame_dummy
080483e4 T main
U memcpy@@GLIBC_2.0
3.4 ldd
使用ldd命令查看二进制程序的共享链接库依赖,包括库的名称、起始地址,这样可以确定段错误到底是发生在了自己的程序中还是依赖的共享库中。以程序2.3为例:
panfeng@ubuntu:~/segfault$ ldd./segfault3
linux-gate.so.1=>(0x00e08000)
libc.so.6=>/lib/tls/i686/cmov/libc.so.6(0x00675000)
/lib/ld-linux.so.2(0x00482000)
vscode C++ debug 配置(ubuntu 20.04)
在Ubuntu 20.04上配置Visual Studio Code(VSCode)进行C++调试,首先需要安装VSCode和必要的编译工具如gcc, g++, gdb。参考Get Started with C++ on Linux in Visual Studio Code,步骤如下:
1.安装VSCode和相关环境:确保VSCode、gcc、g++、gdb等工具已安装在系统中。
2.项目调试配置:在VSCode中,关键配置文件包括.vscode文件夹下的tasks.json、launch.json和c_cpp_properties.json。
2.1. IntelliSense配置:通过安装C/C++插件,为VSCode提供代码提示功能。
2.2. tasks.json文件:用于指定编译任务,例如命令行使用g++编译,`command`为`g++`,`args`包括编译文件名和输出文件名。如果项目用makefile编译,`command`后加上`make`,根据makefile的TAG(如TAG= dbg)动态添加-g选项。
2.2.2. launch.json文件:`program`字段指定要调试的程序,`PreLaunchTask`与tasks.json中的任务对应,`args`用于添加程序执行参数,如指定cacti程序和参数。
2.2.3. c_cpp_properties.json:通过Ctrl+Shift+P快捷键,选择"C/C++:Edit configurations(UI)"自动生成,用于设置编译器选项和构建配置。