添加静态库时出现链接器错误

Linker error for adding static library

本文关键字:链接 错误 静态 添加      更新时间:2023-10-16

我正在按照这里的教程创建一个静态库并将其用于另一个项目。所以我想创建一个 .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 中的项目中。