Linux link compilation detailed _linux shell

Source: Internet
Author: User


Why should I include a header file instead of a. c file

Test code:

Copy Code code as follows:

M.C file:
#include "t.c"
int main ()
{
Test ();
return 0;
}

Compile:

Copy Code code as follows:

GCC M.c-o m-wall

In the file included from m.c:1:0:
T.C: In the function ' test ':
T.c:3:2: Warning: Implicit declaration of function ' Putchar ' [-wimplicit-function-declaration]
Compile through, with only one warning, generate executable m, run it normally, output a space.

Modify the following t.c file:

Copy Code code as follows:

#include <stdio.h>
void Test ()
{
printf ("test\n");
}

Post-Compile execution

Output: Test

From this, it can be seen that the inclusion of the. c file does not affect the program, but more directly than the inclusion of the. h file, which mainly takes into account large projects, the direct links between the documents, such as A.C file M.c file, B.C The file contains the M.c file, and the A.C file contains the B.C file, the error occurs at compile time, and the function name is redefined.
The difference between #include <> and #include "":

For header files that are included with angle brackets, GCC first finds the directory specified by the-I option, and then looks for the system header directory (usually/usr/include, which also includes/usr/lib/gcc/i486-linux-gnu/4.3.2/include on my system) Instead, for the header file enclosed in quotation marks, GCC first finds the directory containing the. c file that contains the header file, finds the directory specified by the-I option, and then finds the system's header file directory.


Static Library

Copy Code code as follows:

* STACK.C * *
Char stack[512];
int top =-1;

Copy Code code as follows:

* PUSH.C * *
extern Char stack[512];
extern int top;
void push (char c)
{
Stack[++top] = c;
}

Copy Code code as follows:

* POP.C * *
extern Char stack[512];
extern int top;
Char pop (void)
{
return stack[top--];
}

Copy Code code as follows:

* IS_EMPTY.C * *
extern int top;
int is_empty (void)
{
return top = = 1;
}

Copy Code code as follows:

* Stack.h * *
#ifndef Stack_h
#define Stack_h
extern void push (char);
extern char pop (void);
extern int is_empty (void);
#endif

Copy Code code as follows:

* MAIN.C * *
#include <stdio.h>
#include "Stack.h"
int main (void)
{
Push (' a ');
char c = pop ();
printf ("%c\n", c);
return 0;
}

In the same directory as the 5. c files and one. h file, create a new makefile file in the current directory, and use makefile to compile.

Copy Code code as follows:

MAIN:LIBSTACK.A MAIN.O
Gcc-o main main.o-l.-lstack
LIBSTACK.A:STACK.O PUSH.O POP.O is_empty.o
Ar rs libstack.a stack.o push.o pop.o is_empty.o
STACK.O:
Gcc-o stack.o-c STACK.C
push.o
Gcc-o push.o-c push.c
POP.O:
Gcc-o pop.o-c pop.c
Is_empty:
Gcc-o is_empty.o-c is_empty.c
MAIN.O:
Gcc-o main.o-c MAIN.C

Executed after compilation./main
Show: A

Anti-compile directive: View anti-compile program

Copy Code code as follows:

objdump-d Main

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.