In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you how to build the C language development environment under Linux, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!
Gedit editors under Linux, many programmers admire editors that use command-line modes such as Vim and Emacs. They are powerful and high enough to be used regardless of whether they have a desktop environment or not. they are an essential artifact for Linux Candle + programmers. But Vim and Emacs are not easy to use, users need to remember a lot of commands and shortcuts, proficiency makes perfect, so it takes a period of time to learn and adapt, which will increase the cost of learning for beginners, so it is not recommended here.
There is a built-in graphical interface editor under CentOS called Gedit. Gedit is a simple and practical text editor with an elegant interface, syntax highlighting and easier to use than Vim. It is no different from the editor under Windows. This tutorial uses Gedit as the editor of the C language code.
If you are using another Linux distribution, you may not have Gedit installed by default, so you have to install it yourself. The specific steps are as follows:
You can't use 'macro parameter character #' in math mode sudo apt-get update # Update Software list $sudo apt-get install gedit-gmate # installation
First, create a new folder called demo under our own user directory. This folder is specially used to store files related to the C language, such as source files, target files, executable files, etc., and it is designed for us to learn and use.
The user directory is sometimes called the home folder or home folder, and its path is / home/username, where username is the user name we used when we logged in to Linux. Linux creates a folder under the home directory for each logged-in user, which is dedicated to storing profiles, text documents, pictures, executables, etc., used by that user to distinguish them from other users.
Next you need to create a blank main.c source file. Main.c is actually a plain text file without any special format, but unlike Windows, you can create a new text document in the right-click menu. Linux must use the command to create it, as shown below:
You can't use 'macro parameter character #' in math mode touch main.c # create an empty file named main.c with the touch command $gedit main.c # use the gedit command to edit the main.c
This completes the creation of the source file and is ready to edit the source file using Gedit.
Enter the following C code in Gedit:
# include int main () {puts ("C language Chinese Network"); return 0;} the effect after the input is completed is as follows:
Ctrl+S saves the file and completes the editing of the source code. At this point, you need to close the Gedit window before the $gedit main.c command ends, and you can continue to enter other commands on the console.
The following figure shows the actual effect on the console:
Note that under CentOS, after using the touch command to create the main.c, enter the demo directory, and you can also use Gedit to open the main.c in the right-click menu, as shown below:
The most widely used GCC compiler under Linux is GCC, and most Linux distributions are installed by default. Whether developers or beginners, GCC is generally used as the preferred compilation tool under Linux. This tutorial also does not hesitate to use GCC to compile C language programs.
GCC is just a compiler, has no interface, and must be used in command line mode. The source file can be compiled into an executable file through the gcc command.
1) the easiest way to generate an executable is to generate an executable:
You can't use 'macro parameter character #' in math mode gcc main.c # follows the source file name immediately after the gcc command
Open the demo directory and you will see an extra file named a.out, which is the resulting executable, as shown in the following figure:
This completes the whole process of compilation and linking at one time, which is very convenient.
Note: unlike Windows,Linux, which does not distinguish executables by file suffixes, the executable suffix under Linux can theoretically be arbitrary, where .out is just used to indicate that it is the output file of GCC. Regardless of the name of the source file, the default name of the executable generated by GCC is always a.out.
If you do not want to use the default file name, you can customize the file name with the-o option, for example:
$gcc main.c-o main.out
The name of the generated executable program is main.out.
Because the suffix of the executable file under Linux is only formal, the executable file can also be without a suffix, for example:
$gcc main.c-o main
The name of the generated executable program is main.
You can also export executable files to other directories through the-o option, not necessarily in the current directory, for example:
$gcc main.c-o. / out/main.out
Or
$gcc main.c-o out/main.out
Means to output the executable file to the out directory in the current directory and name it main.out. . / represents the current directory, and if you don't write it, the default is the current directory.
Note: the out directory must exist, if not, the gcc command will not be created automatically, but will throw an error.
2) running the executable program above we have generated the executable program, so how do we run it? Simply enter the name of the program in the console, as shown below:
$. / a.out
. / represents the current directory, and the whole command means to run the a.out program in the current directory. If you do not write. /, Linux will look for a.out under the system path, and the program obviously does not exist under the system path, so it will fail.
The so-called system path is the path specified by the environment variable. We can add our own path or delete a path by modifying the environment variable. In many cases, a Linux command corresponds to an executable program, and if the path is not specified when executing the command, it will look for the corresponding program under the system path.
After entering the above command, press enter, and the program begins to execute, and it displays the output directly on the console, as follows:
Gcc main.c language Chinese net
The following figure shows the actual effect on the console:
If the program is in another directory, run the program with the name of the directory, for example:
$. / out/main.out
Or
$out/main.out
Whether or not to add. / at this time, Linux can recognize that out is a directory, not a command. By default, it will look for the directory under the current path instead of going to the system path, so it won't make an error if you don't add. /.
Note that if the program does not have execute permissions, you can use the sudo command to increase the permissions, for example:
$sudo chmod 777 a.out
In order to give readers a more comprehensive understanding, we might as well connect the above two parts to fully demonstrate the whole process from editing the source file to running the executable program:
You can't use 'macro parameter character #' in math mode touch main.c # create a new blank source file You can't use 'macro parameter character #' in math mode gcc main.c # generate the executable program You can't use 'macro parameter character #' in math mode # and continue waiting for other commands to be entered
The following figure shows the actual effect on the console:
Step-by-step compilation described above is to complete the whole process of compilation and linking at one time through the gcc command, which is the most convenient, as we usually do in the process of learning the C language. In fact, the gcc command can also separate compilation from linking, completing only one task at a time.
1) Compile) compiling a source file to a target file requires the-c option, for example:
Gcc-c main.c
Compile main.c to main.o. Open the demo directory and you will see main.o:
For Microsoft compilers (embedded in Visual C++ or Visual Studio), the suffix for the object file is .obj; for the GCC compiler, the suffix is .o.
One source file generates one target file, multiple source files generate multiple target files, and the number of source files is the same as the number of target files. Typically, the default destination file name is the same as the source file name.
If you want to customize the name of the target file, you can use the-o option, for example:
Gcc-c main.c-o A.O
The name of the target file generated is a.o.
2) Link following the name of the target file immediately after the gcc command, you can link the target file into an executable file, for example:
Gcc main.o
Link main.o as a.out. Open the demo directory and you will see a.out.
It is OK to follow the name of the source file or the name of the target file immediately after the gcc command, and the gcc command can automatically identify whether it is a source file or a target file: if it is a source file, it takes two steps to compile and link to generate the executable file; if it is an target file, you only need a link.
You can still customize the name of the executable using the-o option, for example:
Gcc main.o-o main.out
The name of the executable file generated is main.out.
Here is a complete demonstration:
Gcc-c main.c. / a.out C Chinese website $
The real effect on the console is:
These are all the contents of the article "how to build a C language Development Environment under Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.