您的位置:首页 > 编程语言 > C语言/C++

Ubuntu下编译C/C++文件

2015-09-10 15:46 627 查看

安装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获取更多消息
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ubuntu c语言 gcc