C Programming in Linux _ 1, linuxc programming _ 1

Source: Internet
Author: User

C Programming in Linux _ 1, linuxc programming _ 1
0x0: Why did I write this series of articles?
In the process of cracking, we cannot avoid writing programs, restoring algorithms, simulating algorithms, decrypting data in the game, and encrypting and decrypting fields stored in client gold coins in the game, dynamic encryption, decryption, and release of Chinese game source files are inseparable from our own analysis and code.
Of course, there are many language choices, commonly used for c, c ++, python, java, and lua. These are all used in the game cracking process, it is necessary for us to be proficient in writing a language. Otherwise, we simply look at the tutorials written by others and compare them to modifying the game. There is no progress in technology, so we can only let typing, clicking the mouse is faster.
During the last few days, I planned to take a good look at c. Because my computer had reinstalled the system on Ubuntu a few days ago, I found some knowledge about c Language Development in linux.
Here, it is just a simple record of learning over the past few days, so that you can read it later. Of course, you must have collected some materials for simple sorting and experimentation. You cannot say that everything is original, of course, you can write out the program without having to close the door and read the official documents. Here, I would like to thank all the friends who share their learning experience and learning materials for sharing them so that everyone can make progress together!
0x1: Preparations
In linux, there are many built-in compilers, such as gcc, so we don't need to find some software to configure the development environment. Use the default gedit, or use sublime 2. The effect is good, highlight some special fields.
0x2: Compile the test code in Linux. To compile a C-language source program, we need to use the GNU gcc compiler. Below
We use an example to illustrate how to use the gcc compiler.

<span style="font-size:18px;">int main(int argc,char **argv){printf("Hello Linux\n");}</span>






Enter./hello




The gcc compiler will generate a hello executable file for us. run. /hello, you can see the output result of the program. in the command line, gcc indicates that we use gcc to compile our source program. The-o option indicates that the executable file name output by the compiler is hello and hello. c is our source program file.
The gcc compiler has many options. Generally, we only need to know a few of them.
-O options are known, indicating the executable file name that we want to output.
-C option indicates that the compiler only needs to output the target code without outputting executable files.
The-g option indicates that the compiler is required to provide information for future debugging of the program during compilation.
With these three options available, we can compile our own simple source program. If you want to know more options, you can view the gcc help documentation, there are many details about other options.

0x3: Write Makefile
If we have multiple c files that need to be compiled, we generally do not enter such a line of code. We must write a shell script to compile it ourselves, however, if a project has many files that need to be compiled, we should first compile a Makefile file and execute the make command to perform this operation.
For example, we compile some files:




Then we will compile the corresponding file:
# This is the Makefile file of the above program.
Main: main. o mytool1.o mytool2.o
Gcc-o main. o mytool1.o mytool2.o
Main. o: main. c mytool1.h mytool2.h
Gcc-c main. c
Mytool1.o: mytool1.c mytool1.h
Gcc-c mytool1.c
Mytool2.o: mytool2.c mytool2.h
Gcc-c mytool2.c
With this Makefile file, we only need to execute the make command when we modify the file in the source program, our compilers only compile the files related to the files we modified. She doesn't even want to handle other files.
Next we will learn how to compile Makefile.
In Makefile, # the start line is a comment line. The most important thing in Makefile is to describe the dependency of the file. The general format is:

Target: components
TAB rule
The first line indicates dependency, and the second line indicates rules.
For example, the second line of the Makefile file above is main: main. o mytool1.o mytool2.o.
The dependent object (components) of target main is main. o mytool1.o mytool2.o when the dependent object is modified after the target is modified, it is necessary to execute the command specified by the rule line. as mentioned in the third line of Makefile above, we need to execute gcc-o main. o mytool1.o mytool2.o
Note that the TAB in the Rule line indicates that there is a TAB key.
Makefile has three very useful variables: $ @, $ ^, and $ <, which indicate the following meanings:
$ @ -- Target file, $ ^ -- all dependent files, $ <-- the first dependent file.
If we use the above three variables, we can simplify our Makefile file:
# This is the simplified Makefile.
Main: main. o mytool1.o mytool2.o
Gcc-o $ @ $ ^
Main. o: main. c mytool1.h mytool2.h
Gcc-c $ <
Mytool1.o: mytool1.c mytool1.h
Gcc-c $ <
Mytool2.o: mytool2.c mytool2.h
Gcc-c $ <
After simplification, our Makefile is simpler, but sometimes people want to be simpler. Here we will learn the default rule of a Makefile.
. C. o:
Gcc-c $ <
This rule indicates that all. o files are dependent on the corresponding. c files. For example, mytool. o depends on mytool. c.
The Makefile can also be changed:
# This is the simplified Makefile.
Main: main. o mytool1.o mytool2.o
Gcc-o $ @ $ ^
. C. o:
Gcc-c $ <
Well, our Makefile is similar. If you want to know more about Makefile rules, you can view the relevant documents.







How to program in C language in linux

This article introduces the basic knowledge required for C Programming in LINUX. In this article, we will learn the following:
Source program Compilation
Write Makefile
Library Link
Program debugging
Header files and system help

--------------------------------------------------------------------------------
1. source program Compilation
In Linux, If You Want To compile a C language source program, we need to use the GNU gcc compiler. The following example shows how to use the gcc compiler.
Suppose we have the following simple source code (hello. c ):
Int main (int argc, char ** argv)
{
Printf ("Hello Linux \ n ");
}

To compile this program, we only need to execute it in the command line:
Gcc-o hello. c
The gcc compiler will generate a hello executable file for us. Execute./hello to see the output result of the program.
Gcc indicates that we use gcc to compile our source program. The-o option indicates that the executable file name output by the compiler is hello.
And hello. c is our source program file. the gcc compiler has many options. Generally, we only need to know a few of them. -o options are known, indicating the executable file name that we want to output.
The-c option indicates that the compiler only needs to output the target code, but does not need to output executable files. the-g option indicates that the compiler is required to provide information for future debugging of the program during compilation. with these three options available, we can compile our own simple source program. If you want to know more options, you can view the gcc help documentation, there are many details about other options.
2. Write Makefile
Suppose we have the following program, the source code is as follows:

/* Main. c */
# Include "mytool1.h"
# Include "mytool2.h"

Int main (int argc, char ** argv)
{
Mytoolpattern print ("hello ");
Mytool2_print ("hello ");
}

/* Mytool1.h */
# Ifndef _ MYTOOL_1_H
# Define _ MYTOOL_1_H

Void mytooltypesprint (char * print_str );

# Endif

/* Mytool1.c */
# Include "mytool1.h"
Void mytooltypesprint (char * print_str)
{
Printf ("This is mytool1 print % s \ n", print_str );
}

/* Mytool2.h */
# Ifndef _ MYTOOL_2_H
# Define _ MYTOOL_2_H

Void mytool2_print (char * print_str );

# Endif

/* Mytool2.c */
# Include "mytool2.h"
Void mytool2_print (char * print_str)
{
Printf ("...... the remaining full text>

How to Learn c Programming in linux?

First, buy a LINUX getting started book. At least you need to know some common commands and use the VI editor to write scripts. We recommend laruence's private house dish, a good entry book for LINUX, and then start to learn the C language. As for how to learn the C language, haha, it's nothing more than reading books, watching videos, and writing programs by yourself, you can read books, watch videos, and write programs on your own. Of course, if you have any problems, you can GOOGLE or Baidu. If you cannot find the answer, you can go to some professional LINUX forums to ask questions.

Related Article

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.