Compile c/c++ program on ubuntu

1.Open up a terminal on Ubuntu Linux and install the build-essential package by typing the following command in the terminal

     sudo apt-get install build-essential

2.Create a directory and a sub directory to hold your C/C++ programs and your main HelloWorld program.

    mkdir -p CCPP/HelloWorld

3.Then we will change into our created directory by issuing the following command

    cd CCPP/HelloWorld

4.Next we will use a text editor such as gedit or nano to create our C or C++ source code using the following command.

5.For example for a C source code file we would issue the following command

    gedit main.c

6.Enter the following C source code below:

  • #include<stdio.h>
  • #include<stdlib.h>
  • int main()
  • {
  • printf("\nHello World,\nWelcome to my first C program in Ubuntu Linux\n\n");
  • return(0);
  • }
  • 7.Save the file as main.c and exit
  • 8.For example for a C++ source code file we issue the following command
  •     gedit main.cpp
  • 9.Add the following lines below to create your C++ source code:
    • #include<iostream>
    • using namespace std;
    • int main()
    • {
    • cout<<"\nHello World,\nWelcome to my first C ++ program on Ubuntu Linux\n\n"<<endl;
    • return(0);
    • }
    • 10.Save the file as main.cpp and exit
    • 11.Compiling your C/C++ program
      • Important: Make sure you are in the CCPP/HelloWorld directory before you compile your C/C++ programs.
        1. 12.If you are compiling the C program version of Hello World type in the terminal
          • Type/Copy/Paste: gcc -Wall -W -Werror main.c -o HelloWorldC
          • The first line will invoke the GNU C compiler to compile the file main.c and output (-o) it to an executable called HelloWorldC.
          • The options -Wall -W and -Werror instruct the compiler to check for warnings.
        2. 13
          If you are compiling the C++ program version of Hello World type in the terminal
          • Type/Copy/Paste: g++ -Wall -W -Werror main.cpp -o HelloWorldCPP
        3. 14
          If you should happen to get permission errors, you need to make the file executable. You can do this by issuing the following commands below
          • Type/Copy/Paste: chmod +x HelloWorldC
          • or
          • Type/Copy/Paste: chmod +x HelloWorldCPP
        4. 15
          In order to execute your program you will have to type in the following commands.
          • To execute the C program version of the program:
          • Type/Copy/Paste: ./HelloWorldC
          • To execute the the C++ program version of the program:
          • Type/Copy/Paste: ./HelloWorldCPP




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值