Ubuntu下编译C/C++文件

来源:互联网 发布:淘宝网天天特价女棉衣 编辑:程序博客网 时间:2024/04/18 15:14

安装GCC 的C/C++编译器

sudo apt-get update
sudo apt-get install build-essential manpages-dev

检测一下是否安装成功

$ whereis gcc

$ which gcc

$ gcc –version

如下

写C

接下来就可以写个helloworld试试了,我的是helloworld.c

#include<stdio.h>/* helloworld.c:  My first C program on a Linux */int main(void){ printf("Hello! This is a test prgoram.\n"); return 0;}

然后开始编译

gcc helloworld.c -o helloworld

可以看到生成了一个heloworld编译文件

现在可以运行编译文件了

./helloworld

写C++

#include "iostream"// helloworld2.C - Sample C++ prgoram int main(void){    std::cout << "Hello! This is a C++ program.\n";    return 0;}

那编译时这么写

g++ hellowrld2.C -o helloworld2

运行这么写

./helloworld2

使用调试器GDB

一般如果代码写错了,编译时得到的错误信息不是很多,我们可以使用gdb

C文件

gcc -g -Wall helloworld.c -o hellowrld

C++文件

g++ -g -Wall helloworld2.c -o helloworld2

访问www.manpager.com/linux/man1/gcc.1.html获取更多消息

0 0
原创粉丝点击