首页 > 解决方案 > 库未链接并获得对“Lib::algorithmA(int const&)”错误的未定义引用

问题描述

我正在从我的讲师那里提取所有应该编译的代码,但似乎我的库文件没有正确链接,因此文件以未定义的方式运行。

我的主文件在 src 中运行,看起来像这样,在 #include <Lib.h> 语句之后有很多未使用的包含语句。

#include <iostream>
#include <Lib.h>
#include "Lib.h"
#include "../include/Lib.h"
//#include "../lib/libalgos-win.a"

using namespace std;

int main() {
    //you must change/expand this code
    int n = 20;
    //Lib guy = new Lib();
    Lib::algorithmA(n);
    cout << "Successful execution" <<endl;
    return 0;
}

我的 Lib.h 文件如下所示

#ifndef LIBRARY_LIB_H
#define LIBRARY_LIB_H



class Lib {
public:
    static void algorithmA(const int& n);
    static void algorithmB(const int& n);
    static void algorithmC(const int& n);
};


#endif //LIBRARY_LIB_H

在我的 Cmake 文件中,我有以下代码:

target_link_libraries(a2-program "${PROJECT_SOURCE_DIR}/lib/libalgos-win.a")

理论上,这应该将库文件链接到我在 lib 中的库,但相反,我收到一个关于未定义引用的错误。他没有就如何编辑代码给出更多说明,所以我完全迷路了。

标签: c++

解决方案


推荐阅读