Compile the. c file with extern with GCC...

Source: Internet
Author: User

Not compilation at first ~
Later I saw Grandpa Tan's book.

When you encounter extern during compilation, first find the definition of the external variable in this file. If it is found, it will extend the scope in this file. If it cannot be found, find the definition of external variables from other files during the connection. If the definition is found, the scope will be extended to this file. If the definition cannot be found, handle the error.

Different files are not directly compiled when they are connected.

[Ktktkt @ Jintao common] $ cat file1.c
# Include <stdio. h>

Extern int multiply (int A, int B)
{
Int C;
C = A * B;
Return C;
}
[Ktktkt @ Jintao common] $ cat file2.c
# Include <stdio. h>

Extern int sum (int A, int B)
{
Int C;
C = A + B;
Return C;
}
[Ktktkt @ Jintao common] $ cat extern. c
# Include <stdio. h>

Int main ()
{
Extern int multiply ();
Extern int sum ();

Int A, B;
Int result;
Printf ("Please input a and B/N ");
Scanf ("% d", & A, & B );
Result = multiply (A, B );
Printf ("the result of multiply is % d/N", result );
Result = sum (A, B );
Printf ("/nthe result of sum is % d/N", result );

Return 0;
}

[Ktktkt @ Jintao common] $ GCC file1.c file2.c extern. C-o extern
[Ktktkt @ Jintao common] $./extern
Please input A and B

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.