Linux compiled c++/c and several common commands (Ubuntu)

Source: Internet
Author: User

today, my brother asked me a C question, originally thought to solve with Java, the results found that some statements are not through, and then downloaded the installation of Codeblocks, trying the process is always prompted not to have the highest executive permission, I day ... Later thought with g++ to compile, but I have been doing Java, had to now learn to sell, for everyone to tidy up a bit of information to seebefore compiling, we need to install g++ GCC in the system, which is the compiler for C++/C under Linux. The code is as followsinstallation command:
sudo apt-get install build-essentialsudo apt-get install gccsudo apt-get Install g++

c compile and runnow we're going to write a simple C program in a text editor (like all books that learn C or C + + appear)
# include <stdio.h>int main () {    printf ("hello,world!\n");    return 0;}

now save as HELLO.C, open your terminal and enter the file in the current directory:
gcc hello.c-o Hello

then enter sudo./hello in the terminal and you will see the results of the program running in the terminal.


The following is how C + + is compiledWrite the program (I don't have to say more about it)
Code:

# include <iostream>using namespace std;
int main () {    cout<< "HelloWorld" <<endl;    return 0;}

Save to Hello.cpp
then use the g++ command to compile
g++ Hello.cpp-o Hello


Multi-file compilation: Here are three source files Hello.h, Hello.cpp, MyFirst.cpp

File_NO1:Hello.hclass Hello {public     :      hello ();     int Display ();};/ /file_no2:hello.cpp#include <iostream> #include "Hello.h" using namespace std; Hello::hello () {}int Hello::D isplay () {    cout<< "hello,world!\n" <<endl;     return 0;} File_no3:myfirst.cpp#include <iostream> #include "Hello.cpp" int main () {    Hello Thehello;    Thehello.display ();    return 0;}

in g++, there is a parameter-C can only compile without connection, then we can compile the file in the following order,
Code:

g++-C hello.cpp-o hello.o
g++-C Myfirst.cpp-o MYFIRST.O
g++ myfirst.o hello.o-o Myfirst


you will ask, if it is a project, there may be hundreds of documents, such a compilation method, the person is not to be exhausted in front of the computer, or until you compile successfully, not the hair is white, hehe, so we have to write the above compilation process into the following text file:
#这里可以写一些文件的说明MyFirst: MYFIRST.O hello.og++ myfirst.o hello.o-o myfirsthello.o:hello.cppg++-C hello.cpp-o hello.omyfirst.o:myfirst.cppg++-C Myfirst.cpp-o MYFIRST.O


Debugging of the program:Save As Myfirst, in the terminal input: Make myfist, the program has been wrong but all the programmers common enemy, in writing the program we should try to avoid the error, but when writing, how inevitably there is such a mistake, It's a good idea to do the necessary debugging of the program, so how do we debug the program, see below:
GDB./filename The following is a command that can be used in the debug state (you can enter only the word input, such as break to B), the description in angle brackets List < display source code >break line number < set breakpoint >run < run the program > Continue < Continue to execute >print variable from breakpoint < view variable value >del line number < Delete breakpoint >step < stepping, trace to function internal >next < stepping, Cannot trace to function internal >quit < exit >

It ' s never too late to learn!!! Keep the right attitude to learn, one day to reach the shore of success!!!

Linux compiled c++/c and several common commands (Ubuntu)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.