添加静态库时出现链接器错误
Linker error for adding static library
我正在按照这里的教程创建一个静态库并将其用于另一个项目。所以我想创建一个 .lib 文件并将其用于另一个项目。
静态库项目:
MyMathLib.h
#define PI 3.1415;
double PowerOf2(double UserNumber);
double PowerOf3(double UserNumber);
double CircleArea(double UserRadius);
double CircleCircum(double UserRadius);
我的数学库.cpp
#include "stdafx.h"
#include "MyMathLib.h"
double PowerOf2(double UserNumber) { return UserNumber * UserNumber; }
double PowerOf3(double UserNumber) { return UserNumber * UserNumber * UserNumber; }
double CircleArea(double UserRadius) { return UserRadius * UserRadius * PI; }
double CircleCircum(double UserRadius) { return 2 * UserRadius * PI; }
对于第二个项目,我做了以下工作:
添加 MyMathLib vc 项目
通用属性 ->引用 ->添加新引用
C/C++ -> 常规 -> 其他包含目录。
这是尝试调用库的 C 文件:
我的应用1.c
#include <stdio.h>
#include "MyMathLib.h"
int main()
{
double p2 = 10.0;
double radius = 4.0;
printf("The number %.2f to the power of 2 is %.2f. n", p2, PowerOf2(p2));
printf("A circle with a radius of %.2f, the area is %.2f. n", radius, CircleArea(radius));
return 0;
}
我得到的错误是:
1>------ Build started: Project: MyApps1, Configuration: Debug Win32 ------
1>MyApps1.obj : error LNK2019: unresolved external symbol _PowerOf2 referenced in function _main
1>MyApps1.obj : error LNK2019: unresolved external symbol _CircleArea referenced in function _main
1>c:usersbandikadocumentsvisual studio 2013ProjectsMyApps1DebugMyApps1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped ==========
所以某处存在链接错误。我尝试转到 MyApps1 属性 ->链接器 -> 输入 -> 其他依赖项,但我认为我无法为 MyMathLib 添加 .lib 文件。知道我错过了什么吗?
它与静态库与第二个项目的链接有关。
我认为在"配置属性 -> 链接器 -> 输入 ->其他依赖项"中添加生成的静态库名称没有任何问题。它应该解决链接问题。
使用此选项后,您是否遇到任何其他问题?
您没有将第二个文件添加到 VS 中的项目中。
相关文章:
- 静态数据成员的问题-修复链接错误会导致编译器错误
- Visual Studio mkl_link_tool.exe链接错误
- C++ 实现模板单例类时出现链接错误
- 使用类模板的方法链接错误
- MySQL C++连接器链接错误
- VS 2015 链接错误 无法构建依赖于 libcurl 的项目
- 运行 C++ 单元测试时LNK2005链接错误
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 链接错误,包括我创建的相同头文件 - C++
- 在Embarcadero C++ Builder中生成的DLL未解决的外部链接错误
- 使用标头保护的多个定义链接错误
- 链接错误:未定义对 stdscr 和 wgetch 的引用
- 使用 Vivek 的 Vcam / 捕获源过滤器构建/链接错误
- 升压program_options中的链接错误
- 使用 g++ 预处理器进行替换会导致链接错误
- 在调试配置中编译工作正常,但发布会给出链接错误
- 相邻矩阵设置链接错误
- 包含常量变量并包含在多个文件中的标头的链接错误
- C++链接错误,我理解但无法解决
- 是什么导致macOS Mojave上的GoogleTest链接错误