C++ error LNK2019: "unresolved external symbol"解决方法

1>  LINK : D:\code\OpenGLTest\Debug\OpenGLTest.exe not found or not built by the last incremental link; performing full link
1>OpenGLTest.obj : error LNK2019: unresolved external symbol "void __cdecl build_rotmatrix(float (* const)[4],float * const)" (?build_rotmatrix@@YAXQAY03MQAM@Z) referenced in function "void __cdecl recalcModelView(void)" (?recalcModelView@@YAXXZ)
1>OpenGLTest.obj : error LNK2019: unresolved external symbol "void __cdecl add_quats(float *,float *,float *)" (?add_quats@@YAXPAM00@Z) referenced in function "void __cdecl animate(void)" (?animate@@YAXXZ)
1>OpenGLTest.obj : error LNK2019: unresolved external symbol "void __cdecl trackball(float * const,float,float,float,float)" (?trackball@@YAXQAMMMMM@Z) referenced in function "void __cdecl motion(int,int)" (?motion@@YAXHH@Z)
1>D:\code\OpenGLTest\Debug\OpenGLTest.exe : fatal error LNK1120: 3 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.47

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

在vs2010中调试程序时,出现以上错误。

查了很多资料,说是没有添加相应的lib到工程属性配置中。可是我已经添加过了,出现的这样错误。

经过查找资料,发现一篇很有用的文章,提示说是由于在C++中使用用C编写的文件导致的错误。

请参考:

http://www.cppblog.com/Dutyboy/archive/2010/11/19/134082.html

The problem with your project is that you are using a "C" style exported library in your C++ projects. When the compiler generates mangled names for C++ functions, they are different from unmangled names generated by the C compiler and hence the C++ compiler will not be able to link with the methods imported from coredll.lib.

The solution to this problem is - while including the header windbase_edb.h, you can explicitly tell the compiler that all functions included from this header are "C" style functions by changing your inclusion as below.

extern "C"
{
#include 
}

或者

#ifndef _C_H_

#define _C_H_

#ifdef __cplusplus    //__cplusplus是双下划线

extern "C" {

#endif

extern int add(int, int);

#ifdef __cplusplus

}

#endif

#endif /* _C_H_ */


开始搜到这篇文章,看到是英文,所以还以为是大众的答案呢,到后面仔细一看,才知道不一样。

say to me:以后看到英文的文章,不能抵触哦,要好好看。。


### 回答1: error lnk2001: unresolved external symbol _getpriority 是一个与函数 getpriority 相关的链接错误。该错误表示在链接器阶段无法找到 getpriority 函数的定义。 getpriority 函数是用于获取进程或进程组的优先级的一个标准函数,该函数声明在头文件 #include <sys/resource.h> 中。然而,由于某些原因,链接器无法找到该函数的定义,导致出现 unresolved external symbol 错误。 要解决此问题,可以按照以下步骤操作: 1. 确保在源代码中正确地包含了 <sys/resource.h> 头文件。在 C/C++ 源文件的开头添加 #include <sys/resource.h>,以确保查找到正确的函数声明。 2. 检查编译器选项是否正确设置。有时,编译器选项可能需要手动指定以链接相应的库。对于 getpriority 函数,可能需要链接 librt 或 libpthread 库。可以尝试添加 -lrt 或 -lpthread 到编译器的选项中。 3. 确保链接器能够找到 getpriority 函数的实现。实现该函数的库文件应该与编译器链接,以便在链接器阶段解决外部符号引用。这通常涉及到正确设置库文件的路径或将库文件添加到链接器的库路径中。 总结:error lnk2001: unresolved external symbol _getpriority 是一个链接错误,表示编译器无法找到 getpriority 函数的定义。要解决此问题,需要确保正确包含头文件、正确设置编译器选项和链接库文件。 ### 回答2: error lnk2001: unresolved external symbol _getpriority是一个链接错误,意味着链接器无法解析对_getpriority的外部符号引用。 _getpriority是一个用于获取进程优先级的函数,它在Windows下没有定义,因此链接器无法找到它的定义。 要解决这个问题,可以采取以下几种方法: 1. 检查代码中是否有对_getpriority函数的调用:首先检查代码中是否存在对_getpriority函数的调用,如果没有必要使用该函数,可以删除相关的代码,这样可以避免链接错误。 2. 更改代码以使用Windows下的类似功能:如果确实需要使用进程优先级相关的功能,可以改用Windows下的类似函数来代替_getpriority。可以使用Windows API中的GetPriorityClass函数来获取进程优先级,该函数返回一个整数值表示进程的优先级类别。 3. 添加相应的库文件:如果你确定代码中需要使用_getpriority函数,可以找到包含该函数定义的库文件,并将其添加到链接器的库文件路径中。可以通过在项目属性中添加相应的附加库路径和库文件名称来实现。 总之,要解决error lnk2001: unresolved external symbol _getpriority链接错误,可以通过删除对_getpriority函数的调用、使用Windows API中的相关函数或添加相应的库文件来解决问题。 ### 回答3: 这个错误是由于在C++代码中使用了一个未解析的外部符号"getpriority"所引起的。"getpriority"是一个用于获取进程优先级的函数,属于POSIX标准库中的一部分。 在C++中,要使用"getpriority"函数,需要包含对应的头文件<sys/resource.h>,并且需要链接实现该函数的库文件。根据错误提示"error lnk2001: unresolved external symbol _getpriority",可以推断出缺少对应的库文件的链接。 解决这个问题可以按照以下步骤进行: 1. 确保正确地包含了头文件<sys/resource.h>: #include <sys/resource.h> 2. 确认编译器的选项是否正确地链接了对应的库文件。 对于使用GCC编译器的情况,需要添加"-l"参数指定链接的库文件名,例如: g++ main.cpp -o main -l库文件名 3. 如果是在Windows平台上开发,可能需要使用Cygwin或MinGW等工具链来编译和链接,以确保POSIX相关的功能可用。 此外,请注意确保包含正确的库文件,并将其放置在正确的位置,以便编译器能够找到并链接到正确的实现。 总之,"error lnk2001: unresolved external symbol _getpriority"错误是由于未能解析所需库文件中的"getpriority"函数引起的。通过检查头文件和链接选项,以及在合适的位置放置正确的库文件,可以解决这个问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值