Writing C programs under the Linux environment

Source: Internet
Author: User

One: gcc compile link
Usage: gcc [options] File ...
Options:
-pass-exit-codes returns the highest error code at a certain stage of exit
--help Show this Help description
--TARGET-HELP display target machine-specific command-line options
--help={common|optimizers|params|target|warnings|[ ^]{joined|separate|undocumented}}[,...]
Display command-line options of a specific type
(Use '-V--help ' to display command-line arguments for a child process)
--version Display Compiler version information
-dumpspecs display all built-in spec strings
-dumpversion Display the version number of the compiler
-dumpmachine display the compiler's target processor
-print-search-dirs Display the search path of the compiler
-print-libgcc-file-name display the name of the compiler companion library
-print-file-name=< Library > Display < library > full path
-print-prog-name=< Program > Show compiler Components < program > Full path
-print-multiarch Display The target ' s normalized GNU triplet, used as
A component in the library path
-print-multi-directory displays the root of different versions of LIBGCC
-print-multi-lib displaying command-line options and mapping between multiple repository search paths
-print-multi-os-directory showing the relative path of the operating system library
-print-sysroot Display Target Library directory
-print-sysroot-headers-suffix Displays the Sysroot suffix used to find the header file
-wa,< options > Comma-delimited < options > pass to Assembler
-wp,< options > Comma-delimited < options > pass to Preprocessor
-wl,< options > Comma-delimited < options > pass to Linker
-xassembler < parameters > passing < parameters > to Assembler
-xpreprocessor < parameters > passing < parameters > to the Preprocessor
-xlinker < parameters > passing < parameters > to the linker
-save-temps do not delete intermediate files
-save-temps=<arg> do not delete intermediate files
-no-canonical-prefixes generates a relative path to other GCC components without generating a normalized
Prefix
-pipe using pipelines instead of temporary files
-time timing for each sub-process
-specs=< file > Overwrite the built-in specs file with < file > content
-std=< Standard > Specify standard for input source file compliance
--sysroot=< directory > < directory > as the root directory for header and library files
-B < directory > add < directory > to the search path of the compiler
-V shows the program that the compiler calls
-### is similar to-V, but the option is enclosed in quotation marks and the command is not executed
-E is only preprocessed, not compiled, compiled, and linked
-S compiled into assembly language, not assembled and linked
-C compile, assemble to target code, do not link
Can compile only for individual files to avoid program errors
-o < file > output to < file >

-pie Create a position independent executable
-shared Create a shared library
-X < language > Specify the language of the subsequent input file
Allowed languages include: C C + + assembler None
' None ' means restoring the default behavior, which is guessing based on the file's extension
The language of the source file

gcc + 文件名  默认生成a.out可执行文件

Options that start with-G,-F,-M,-O,-W, or--param are automatically passed by GCC to the
Different sub-processes. To pass additional options to these processes, you must use the-w< letter > option.

Two../[executable] Execution program
./refers to the current directory,./file refers to the execution of files in the current directory. /a.out to execute a.out executable file

Three. GDB Debugging
1. Enter a file to indicate GdB debugging object;
2. Enter L to display the Code of the program;
3. Enter B N (number) to indicate the breakpoint location of the setting;
4. Enter r to start debugging;
5. Enter S or n (letter) to enter the next step;
6. Enter the P variable name to see the value of the variable;
7. Enter Q to exit GDB debugging.

Four. Write a simple program that does not include a header file
Take the example of writing a HelloWorld program.
1. Use Ctrl+alt+t to open the terminal
2. To create text in a folder, first go back to the folder's directory. If I'm building text on the desktop. (Input in terminal)
CD Desktop//Then press ENTER
3. Create a folder (this step can be skipped, but recommended)
mkdir file name
4. Create a text
gedit Hello WORLD.C//Then press ENTER to jump out of a Windows-like Notepad
5. Go inside and knock the code
C

Include <stdio.h>
int main(void) { printf("hello world!"); return 0; } 6.保存代码,关闭文本框。按esc,再输入:wq(一开始看不到尽管输进去,wq为保存并关闭,也可为w则为保存不关闭) 7.启动gcc对代码进行编译。 gcc helloworld.c -o execFile(此步骤会生成一个execFlie的文件,可用ls查看)8.如果有错误,系统会提示,按方向键找回gedit helloworld.c 的指令,按下回车,弹出文本框后根据系统提示对代码进行修改,完成后记得保存,关闭文本框。 9.运行./execFlie则会开始运行7.如果要进行调试,请参考我的第三部分:“有关编写C语言的指令”。

Five. Writing a program that includes a header file
such as://test.h

#ifndef Test_h
#define
Tesh_h

void print ();
#endif

TEST.c
#include <stdio.h>
#include "test.h"
void print ()
{printf ("This is JA test\n");}

Main.c
#include "test.h"
int main ()
{print (); return 0;
}
If the "function undefined" error occurs only when compiling main.c, this is the reason why the compilation is incomplete, so it is not possible to link to the required function.

The actual compilation method is as follows:
Gcc-c MAIN.C #-C parameter is for GCC to compile only, not link
Gcc-c test.c
Gcc-o main MAIN.O TEST.O # link the compiled target code

Writing C programs under the Linux environment

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.