Resolve problem with already defined in. obj (difference in definition/declaration)

Source: Internet
Author: User

First you need to figure out what the definition is and what the Declaration is (declaration).

Declaration of a function function:
int myfunc (int a,int b);
Defined:
int myfunc (int a,int b) {return a+b;}

In. h Although you can declare and define functions, this. h can only be used once, otherwise a redefinition error will occur:

Error LNK2005 _myfunc already defined in Xxx.obj

so try not to define functions in. h .

Second, the variable variable declaration:
extern int A;

If the declaration contains an initialization expression, it is treated as defined , even if it is preceded by extern. The extern declaration can be initialized only if it is outside the function. Therefore, do not initialize the extern declaration .

Defined:
int a;//a is an integer external variable

The meaning of the external variable definition and the external variable declaration is different. an external variable can be defined only once , its position is outside of all functions, and the declaration of an external variable in the same file can be multiple times, its position can be within the function, or it can be outside the function.

The following are good program design styles and conventions:

The function declaration in the header file (. h), and the definition of the function in the source file (. c);

The declaration of the variable is uniformly put to the head file;

Reference: http://www.cnblogs.com/magicsoar/p/3702365.html

Http://c.biancheng.net/cpp/biancheng/view/143.html

http://my.oschina.net/xinxingegeya/blog/232758

Resolve problem with already defined in. obj (difference in definition/declaration)

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.