Under Linux C language learning What are the benefits?

Author: home learning tribal
link: https: //www.zhihu.com/question/23893390/answer/832610610
Source: know almost
copyrighted by the author. Commercial reprint please contact the author authorized, non-commercial reprint please indicate the source.

In the Windows environment, you use the IDE to write a simple hello.c. Then press F11, directly compile and run, and then automatically generate a variety of assorted documents cook in your source directory, you do not know these files is doing, right? Even if you do not know in the end it is how to generate the hello program you're running.

But under Linux is not the same, you can know the program is compiled every process, details, you can even control every process of compiling the program!

Take your hello.c example: Generally speaking, from a C language source file to generate the final executable file, the process is as follows;

  1. C source file: Use vim to write a simple program helloworld
  2. Pretreatment: Pretreatment After generating C source files hello.i
  3. Compile: translating the C source files into assembler files hello.s
  4. Assembler: Assembler files will be compiled into object files hello.o
  5. Link: The link object files into an executable file

Under Linux, you can you can precisely control each compiler, gcc command via the linking process

$  gcc  -E  hello.c > hello.i //会生成预处理后的C源文件hello.i $ gcc -S hello.i //将hello.i编译成汇编文件hello.s $ gcc -c hello.s //将汇编文件hello.s汇编成hello.o $ gcc hello.o -o hello //将目标文件链接成可执行文件hello $ ./hello // 运行可执行文件hello 

Do it again according to the above process, you know the program is compiled, each process link, and you can control it - if you are interested, we can even disassemble our executable:

$ objdump  -D   hello   >  hello.S

That did not, we can be a binary executable file, the disassembly, assembly file is generated, and then to study it.

See here, you may resent: but the command in Linux is very troublesome ah knock, knock knock each time to go, how can click in Windows easy F11!

Do not worry, in Linux we can use shortcut keys: make command

Create a file in your Makefile hello.c file directory, add the following:

hello:hello.c
    gcc hello.c -o hello

After you modify save and exit vim, then tap command in the current directory: $ make

You will find, you make will automatically compile your source code, executable file in the current directory: hello. If you want to add a new item to the C source file main.c, you can directly modify the Makefile, and make the compiler directly on the line.

hello:hello.c main.c     
    gcc hello.c -o hello main.c

If you suspect trouble every time you modify Makefile, we can re-write a new Makefile, automatically searches for all C source files in our current directory, compile, link, an executable file:

.PHONY:all clean
SRCS = $(wildcard *.c) EXE = hello all: $(EXE) $(EXE):$(SRCS)  gcc -o $@ $^ clean:  rm -f $(EXE)

Using the above Makefile, you add the Makefile in the current directory where the new C source file, and then make, you will find all the C compiler will automatically make the files in your current directory.

In fact, in a variety of Windows Integrated Development Environment (IDE), as you said DEV C ++, including VC ++ 6.0, the underlying fact is that they rely on make / makefile to compile the program. Only the name, just not the same rules a little, as in VC ++ 6.0 installation package below, you will find similar EXE file called nmake, in fact, it is equivalent to make Linux environment.

summary:

Learning C language under Linux, we may be familiar with the program compiled by the underlying command, the specific process links, and even control, optimizing compiler process. While under Windows, using the editing interface with integrated development environment (IDE), set the program, compile, link, debug in one-stop service, friendly and interactive features. We can be more easily developed.

Environmental recommendations:

If you later want to engage in C ++ / Java desktop development, learning C language is only one of your transition, laying the foundation stage. It is recommended to use a variety of learning C language IDE development tools under Windows.

If you later want to engage in C language development environment under Linux, embedded development, it is recommended from the outset, to learn C language under Linux environment.

Guess you like

Origin www.cnblogs.com/wkfvawl/p/11618648.html