GCC compilation command-minor C language (14)

Source: Internet
Author: User

[Mac 10.7.1 lion intel-based x64 GCC 4.2.1]

Q: How can I enable GDB to debug compiled files?

A: You can add the-G parameter. Save the following code as hello. C:

#include <stdio.h>int main(){    printf("hello world!\n");    return 0;}

Compile GCC hello. C-O hello to get hello.

Use GDB hello to start debugging and enter the list:

The prompt is displayed, indicating that no symbols are loaded. This indicates that the preceding compilation command does not include debugging information and cannot be properly debugged by GDB.

Add the-G parameter to recompile gcc-G hello. C-O hello to get the file hello.

Start debugging with GDB hello and enter list:

Now we can see the source code and debug it.

Q: How can I compile a program to enable the GPROF tool to be used?

A: Use the-p parameter. However, according to the bug list, the MAC system of Intel architecture cannot run GPROF normally. This example will be written in Ubuntu later.

Q: If the source code extension is not supported by GCC by default, what will happen during compilation?

A: copy the preceding hello. c file as hello. Xichen and compile it with GCC-O hello. Xichen:

You can see that a problem has occurred. You can use the-x parameter to compile a file as a specified type:

Gcc-x C hello. Xichen-O hello is to compile hello. Xichen as C code:

It can be seen that the compilation has been completed correctly. The-x parameter can be followed by C, C ++, and other types of names, or none to close the compilation of Files specified as specific types, for more information, see the GCC help documentation.

Q: What if I need to test whether the Code meets the ANSI standard?

A: You can use the-ANSI parameter. For C code, it will cause compilation problems for code that does not comply with the C90 standard.

For // as a comment, it is supported from c99 and used as a test.

#include <stdio.h>int main(){    // c99 support // style comment    printf("hello world!\n");    return 0;}

Save as hello. C:

Gcc-ANSI-O hello. c Compilation:

Compile with GCC-O hello. C:

As you can see, there is no problem.

Q: If the C language has different standards, which of the following standards is used?

A: You can use the-STD = parameter format and follow c99, gnu99, and other parameters.

Q: How do I disable some keywords as keywords?

A: You can use the-fno-ASM parameter to disable ASM, inline, and typeof as keywords. Save the following code as hello. C:

#include <stdio.h>inline int add(int a, int b){    return a + b;}int main(){    printf("hello world!\n");    return 0;}

Compile with GCC-fno-ASM hello. C-O hello:

The compilation error is displayed. Use GCC hello. C-O hello to compile:

Q: Sometimes, Some compilers use char as a signed character by default, and some as unsigned ones. Can we use compiler parameters for free control?

A: Yes. You can use-funsigned-Char or-fno-signed-Char to automatically treat char as the unsigned type. Similarly, use-fsigned-Char or-fno-Unsigned-Char to automatically treat char as the signed type. For example, save the code as unsigned_signed_char.c:

#include <stdio.h>int main(){    char ch = 0xFF;    if(ch < 0)        printf("char is signed...\n");    else        printf("char is unsigned...\n");    return 0;}

Use GCC unsigned_signed_char.c-O unsigned_signed_char to compile and run:

Use GCC unsigned_signed_char.c-O unsigned_signed_char-funsigned-Char to compile and run:

Use GCC unsigned_signed_char.c-O unsigned_signed_char-fsigned-Char to compile and run the command:

Q: If a file sometimes forgets to contain a header file, how can I insert the header file with the compilation parameter?

A: You can use the-include parameter. The Code is as follows and saved as no_header.c:

int main(){    fprintf(stdout, "no header file\n");    return 0;}

Use GCC no_header.c-O no_header to compile:

As you can see, an error occurred while compiling. Use GCC no_header.c-O no_header-include/usr/include/stdio. h to compile:

Run OK:

Similarly, you can use-dxxx or-dxxx = YYY or-uxxx or-UNDEF to define the macro to get the cancellation macro (xxx indicates the macro name, YYY indicates the macro-defined string ).

Q: What if I want to apply a macro to another file?

A: You can use the-imacros parameter. The-imacros file is followed by the macro name to be included. Run the following code to save it as imacros. h:

#ifndef IMACROS_H#define IMACROS_H#define N   100double  add(double, double);#endif

The following code is saved as imacros. C (it is in the same directory as the above imacros. h ):

#include <stdio.h>int add(int, int);int main(){    int i = N;    printf("i is %d\n", i);    return 0;}

We can see that N in the imacros. c file is not defined, and imacros. H has its macro definition. Use GCC imacros. C-o imacros-imacros. h to compile:

You can see that the compilation is OK and imacros. description of the add function in H and imacros. there is no warning about the inconsistency of the add function declaration in C, because-imacros only sets imacros. the Macros in the H file are processed, while others are not processed.

Running result:

The imacros. C code is modified as follows:

#include <stdio.h>#include "imacros.h"int add(int, int);int main(){    int i = N;    printf("i is %d\n", i);    return 0;}

Compile with GCC imacros. C-o imacros:

As you can see, a compilation error occurs because the add statement is inconsistent. Here we can see the role of the-imacros parameter.

Q: If some header files are moved to another directory for ease of management, and the header file path in the Code does not require an absolute path, how can I set the command line parameters?

A: You can use the-I parameter to add additional header file paths. Run the following code to save it as include_folder.c:

#include <stdio.h>int main(){    fprintf(stdout, "hello\n");    return 0;}

Create the following file stdio. h in the same directory of Hello. C:

#ifndef STDIO_H #define STDIO_H #endif

It is basically useless.

If you use GCC include_folder.c-O include_folder for compilation, there is no problem.

If you use GCC include_folder.c-O include_folder-I. To compile:

It can be seen that the compilation is faulty because-I. the compilation parameter adds the local directory to the header file search path and takes priority over the path specified by-I. include_folder.c contains stdio. h header file, which is first searched in the local directory and contains a header file without any purpose, resulting in subsequent compilation errors. Similar to the-I parameter for header file path modification, there are also the-idirafter,-iprefix,-iwithprefix,-iwithprefixbefore parameters.

Q: If a makefile must use-I to include the specified header file path, but it cannot be used for some reason, can a command solve this problem?

A: Yes. You can use-I-to cancel the extra header file path specified by the-I parameter. However, this parameter format may be discarded in the future. We recommend using-iquote instead. The code above can be correctly compiled using gcc include_folder.c-O include_folder-I.-I-or GCC include_folder.c-O include_folder-I.-iquote.

Q: If you do not want to use the system header file and want to directly use the header file that you have compiled, which is stored in another directory, what should you do?

A: The following code is nostdinc. C:

#include <stdio.h>#include <stdlib.h>int main(){    int i = NOSTDINC;    return 0;}

Nostdinc. C has a head file stdio. h in the same directory:

#ifndef STDIO_H #define STDIO_H #define NOSTDINC    99#endif

Compile with GCC nostdinc. C-o nostdinc-I.-nostdinc:

We can see that the-nostdinc parameter is used, and the compiler will not search under the path of the system header file. will make it search in the local directory, and stdlib is not found. h header file. An error is returned. You can remove the-nostdinc parameter option and use GCC nostdinc. c-o nostdinc-I. compile:

Alternatively, you can remove the inclusion code of the stdlib. h header file from the nostdinc. C source code.

Q: Sometimes the source code needs to be preprocessed, but the comments in the source code are deleted, which may affect the code after analysis and preprocessing. How can we prevent the comments from being deleted during preprocessing?

A: You can use the-C parameter. It is generally used with the-e parameter. The following code is simple_hello.c:

int main(){    // just write something    return 0;}

Use gcc-e simple_hello.c for preprocessing. The following figure shows the content:

# 1 "simple_hello.c"# 1 "<built-in>"# 1 "<command-line>"# 1 "simple_hello.c"int main(){ return 0;}

As you can see, the comment is deleted.

If gcc-c-e simple_hello.c is used for preprocessing, the following content is obtained:

# 1 "simple_hello.c"# 1 "<built-in>"# 1 "<command-line>"# 1 "simple_hello.c"int main(){ // just write something return 0;}

The annotation still exists.

Q: Sometimes I need to view the dependency of a source code. How can I view it?

A: You can use the-M parameter. For example, save the following code as hello. C:

#include <stdio.h>int main(){    printf("hello, xichen\n");    return 0;}

Use gcc-M hello. C to get the following content:

hello.o: hello.c /usr/include/stdio.h /usr/include/sys/cdefs.h \  /usr/include/sys/_symbol_aliasing.h \  /usr/include/sys/_posix_availability.h /usr/include/Availability.h \  /usr/include/AvailabilityInternal.h /usr/include/_types.h \  /usr/include/sys/_types.h /usr/include/machine/_types.h \  /usr/include/i386/_types.h /usr/include/secure/_stdio.h \  /usr/include/secure/_common.h

We can see that the hello. c dependency.

Similar parameters include-MD,-Mm, and-MMD.

Q: If you want to pass a compilation option to an assembly program or link program internally called by GCC, how can you transfer it?

A: You can use-Wa and option to pass the option to the assembler. You can use-WL and option to pass the option to the linked program.

Q: Sometimes, how do I generate debugging information in stabs format?

A: You can use the-gstabs command, but it does not generate GDB debugging information. Therefore, you cannot use GDB to debug the generated executable files. Similar compilation parameters include-gstabs + and-ggdb.

Q: What parameters are used if you want to use static or dynamic links or to generate a shared library?

A: You can use the-static or-shared parameters. However, in MAC systems,-static is basically useless, and the-shared parameter is not supported.

Q: What parameters can be used to experience the features of the ancient C language?

A: You can use the-traditional parameter. I can't write the specific examples. I wrote several very old-looking syntaxes and the GCC compilation was successful.

Q: What should I do if I need to generate optimization code for a specific platform?

A: You can use-o-related parameters for optimization, or use-mtune = cpu_type for specific platform optimization.

Q: What should I do if I need to show no warning information or all warning information that can be displayed?

A: You can use the-W parameter or the-wall parameter. The following code is saved as warning. C:

#include <stdio.h>void main(){    int i;    printf("hello, xichen\n");}

Compile with GCC warning. C-o Warning:

Compile with GCC warning. C-o Warning-wall:


It can be seen that warnings including I not being used are also displayed;

Use GCC warning. C-o Warning-W to compile:

No warning information is displayed.

Xichen

22:26:32

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.