error LNK2005, already defined?
来源:互联网 发布:win10软件锁怎么设置 编辑:程序博客网 时间:2024/05/20 09:49
I have 2 files A.cpp and B.cpp files in a project "Win32 Console Application".
Both 2 files have only 2 lines following code:
#include "stdafx.h"
int k;
When compiling it threw the error
Both 2 files have only 2 lines following code:
#include "stdafx.h"
int k;
When compiling it threw the error
Error 1 error LNK2005: "int k" (?a@@3HA) already defined in A.obj
Why this error?
You broke the one definition rule and hence the linking error.
Solutions:
one: If you need the same named variable in the two cpp files then You need to use Nameless namespace(Anonymous Namespace) to avoid the error.
namespace { int k;}
two: If you need to share the same variable across multiple files then you need to useextern
A.h
extern int k;
A.cpp
#include "A.h"int k = 0;
B.cpp
#include "A.h"//Use `k` anywhere in the file
notice:
In case of extern
, k
should be defined once in any of the source files.
0 0
- already defined...error LNK2005
- LNK2005...already defined...ERROR
- error LNK2005, already defined?
- error LNK2005: already defined错误
- error LNK2005:*** already defined in ***
- error LNK2005:class CWinApp already defined
- error LNK2005: _DllMain@12 already defined
- error LNK2005: * already defined in *.obj
- error LNK2005: _DllMain@12 already defined
- error LNK2005: _DllMain@12 already defined
- error LNK2005: already defined in libcmt.lib
- VS error LNK2005:**already defined in **.obj
- error LNK2005 : already defined in libcmt.lib
- error LNK2005: already defined in libcmt.lib
- error LNK2005: ...already defined in MSVCRTD.lib
- error LNK2005:XXXX already defined in XXX.lib
- error LNK2005: _DllMain@12 already defined in MSVC
- error LNK2005: _DllMain@12 already defined in xxx 的应对
- js学习笔记之事件处理模型
- QQ通信原理概述
- 开发者必知的五大编程谬论
- expect实现scp ssh自动输入密码登录
- final、static、static final修饰的字段赋值的区别
- error LNK2005, already defined?
- 南邮 OJ 1180 H - 管道小球
- win7 64位系统 配置php最新版开发环境(php+Apache+mysql)
- 公司注册的种类及英文缩写
- Servlet接收JSP参数乱码问题解决办法
- leetcode --237 delete node in a linked list
- HTML embed标签使用方法和属性详解
- 如何获取别人微信文章中的图片
- 有些品牌Android真机无法打印log解决办法