0

I'm making a prime generator with 3 file (two of them are .cpp and one is .h).
However when i try to build the whole project it in onlinegdb gives this error

/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

main.cpp

#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);  
}

primefunc.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
    }
}

primegen.h

// 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 *);

EDIT :- I tried moving the function from the 2nd cpp to the main.cpp and it worked and also individual builds also gives success.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Are you sure you are compiling and linking the primefunc.cpp? Please explain your compile options so people can test exactly what you are doing. – kdcode Feb 06 '21 at 04:24
  • I just wrote these codes in separate filenames in online gdb and tried to build it and thats when the error comes. I even tried building those files separately it builds and i also tried sending the primegen() from 2nd cpp file to main.cpp it works Its just when i try to build it https://onlinegdb.com/ZQPW1E6qEG – Astronomical_Kabutar Feb 06 '21 at 04:31
  • 1
    Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) Also, [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Ken White Feb 06 '21 at 04:44
  • well it answered one question atleast that that this error is linker error but unfortunately i can't find where the error lies since i made it as per the rules. and yes i understand now about the image – Astronomical_Kabutar Feb 06 '21 at 05:01
  • Though it may not be immediately obvious, `primegen` is a function template, not a regular function (it's using the [abbreviated function template](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template) syntax, new in C++20). It's equivalent to `template int primegen(int &, T*, U*);` Like any template, its implementation needs to be visible to the compiler at the point where it's used, which usually means it should be placed in a header file. – Igor Tandetnik Feb 06 '21 at 19:22

0 Answers0