首页 > 解决方案 > c ++中的链接错误(多个cpp文件)

问题描述

我正在制作一个带有 3 个文件的素数生成器(其中两个是 .cpp,一个是 .h)
但是,当我尝试在 onlinegdb 中构建整个项目时,会出现此错误

/tmp/ccI5GCGK.o: In function `main':
main.cpp:(.text+0x11f): undefined reference to `int primegen(int&, long*, long*)'
collect2: error: ld returned 1 exit status

主文件

#include <iostream>
#include "primegen.h"

int main(void)
{
    //taking input for number of test cases
    int test_case{2};

    long int lower_lim[MAX] = {5, 15}, upper_lim[MAX] = {15, 25};

    //function present in primefunc.cpp
    primegen(test_case,lower_lim,upper_lim);  
}

主函数.cpp

// to make SUCCESS known to this file
extern int SUCCESS;

//main function to prime generator between limits
int primegen(int &test, auto *low, auto *up)
{
    static int cases=0;
    
    if(cases == test)
        return SUCCESS;
    
    int diff=up[cases]-low[cases];
    
    for(int i=0;i<diff;i++)
    {
        //some code to be added
    }
}

素数

// for making arrays of lower and upper limit
constexpr int MAX = 10;
constexpr int SUCCESS = 2;

// for printing out prime number
int primegen(int &, auto *, auto *);

编辑:-我尝试将功能从第二个 cpp 移动到 main.cpp 并且它有效,并且个人构建也取得了成功。

标签: c++c++17

解决方案


推荐阅读