LNK2019 Visual Studio 2010下的错误 [英] LNK2019 Error under Visual Studio 2010
问题描述
我在Visual Studio 2010下创建了一个示例c ++项目,其中包含以下文件。
Ah
#ifndef A_H
#define A_H
#include< iostream>
void foo();
#endif
A.cpp
#includeAh
void foo()
{
int a = 1;
}
main.cpp
#includeAh
int main(int argc,char * argv [])
{
foo
return 0;
}
我在生成后得到以下输出:
1> ------开始构建:项目:opengl_test,配置:调试Win32 ------
1> main.cpp
1> Ah
1> A.cpp
1>生成代码...
1> Debug \A.obj:warning LNK4042:object specified more than once; extras ignored
1> main.obj:error LNK2019:在函数_main中引用的未解析的外部符号void __cdecl foo(void)(?foo @@ YAXXZ) p>
1> C:\Users\alp\Projects\Test Samples\opengl_test\Debug\opengl_test.exe:致命错误LNK1120:1 unresolved externals
========== Build:0成功,1失败,0最新,0跳过========= =
这个错误的原因是什么?
认为发生了什么是 Ah
是在项目的Source组,而不是Header组,因此它被编译为一个.cpp。因为 A.cpp
和 Ah
都会生成一个对象文件 A.obj
,最后一个编译是唯一一个链接。我相信最后一个编译是 Ah
,没有一个实现 foo()
,因此链接器找不到它。
I have created a sample c++ project under Visual Studio 2010 with following files.
A.h
#ifndef A_H
#define A_H
#include <iostream>
void foo();
#endif
A.cpp
#include "A.h"
void foo()
{
int a = 1;
}
main.cpp
#include "A.h"
int main(int argc, char* argv[])
{
foo();
return 0;
}
I am getting the following output after build:
1>------ Build started: Project: opengl_test, Configuration: Debug Win32 ------
1> main.cpp
1> A.h
1> A.cpp
1> Generating Code...
1>Debug\A.obj : warning LNK4042: object specified more than once; extras ignored
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl foo(void)" (?foo@@YAXXZ) referenced in function _main
1>C:\Users\alp\Projects\Test Samples\opengl_test\Debug\opengl_test.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
What is the reason for this error?
I think what happened is that A.h
was in the Source group of the project rather than the Header group, so it was compiled as if it were a .cpp. Since both A.cpp
and A.h
will generate an object file A.obj
, the last one to compile is the only one that got linked. I believe the last one compiled was A.h
, which didn't have an implementation of foo()
, thus the linker couldn't find it.
这篇关于LNK2019 Visual Studio 2010下的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!