0

I am getting "undefined reference to" error for every function I am trying to use from the header file. The code works fine when i copy the function bodies directly into "main.cpp", but I want to implement it using header file. I am really confused. Even tried enclosing the function declaration and function bodies in "RSA.h" and "RSA.cpp" in namespace but it still didn't work out.

The error i am getting: getting "undefined reference to" error for every function I am tryying to use from the header file

Here's my code.

RSA.h

#ifndef RSA_H_INCLUDED
#define RSA_H_INCLUDED
#include<stdlib.h>

bool isPrime(unsigned long long n);
unsigned long long getPrime(unsigned long long min, unsigned long long max);
unsigned long long gcd(unsigned long long x, unsigned long long y);
unsigned long long lcm(unsigned long long x, unsigned long long y);
unsigned long long modInverse(unsigned long long e, unsigned long long lam);
unsigned long long modExp(unsigned long long base, unsigned long long exp, unsigned long long n);

#endif  //RSA_H_INCLUDED

RSA.cpp

#include<stdlib.h>  //for srand()

bool isPrime(unsigned long long n){
    for(unsigned long long i = 2;  i*i <= n; i++){
        if(n%i == 0){
            return false;
        }
    }
    return true;
}
unsigned long long getPrime(unsigned long long min, unsigned long long max){
    unsigned long long num;
    do{
        num = rand()%(min+max)+min;
    }while(!isPrime(num));
    return num;
}
unsigned long long gcd(unsigned long long x, unsigned long long y){
    if(x == 0){
        return y;
    }
    return gcd(y%x, x);
}
unsigned long long lcm(unsigned long long x, unsigned long long y){
    return (x*y)/gcd(x,y);
}
unsigned long long modInverse(unsigned long long e, unsigned long long lam){
    for(unsigned long long i = 1; i <= lam; i++){
        if((i*e)%lam == 1){
            return i;
        }
    }
    return -1;
}
unsigned long long modExp(unsigned long long base, unsigned long long exp, unsigned long long n){
    unsigned long long ans = 1;
    for(unsigned long long i = 0; i < exp; i++){
        ans = (ans * base) % n;
    }
    return ans;
}

main.cpp

#include<iostream>
#include<climits>   //for UCHAR_MAX and USHRT_MAX
#include "RSA.h"

int main(){
    unsigned long long p, q, n, lambda, d, e, m, c;
    unsigned int seed;
    
    std::cout << "Enter a seed: ";
    std::cin >> seed;
    
    srand(seed);

    p = getPrime(UCHAR_MAX, USHRT_MAX);
    q = getPrime(UCHAR_MAX, USHRT_MAX);
    
    n = p*q;
    std::cout << "n: " << n << std::endl;

    lambda = lcm(p-1, q-1);
    std::cout << "lambda: " << lambda << std::endl;

    do{
        e = getPrime(2, lambda-1);
    }while(lambda%e == 0);
    std::cout << "e: " << e << std::endl;

    d = modInverse(e, lambda);
    std::cout << "d: " << d << std::endl;

    std::cout << "Public key: n = " << n << " e = " << e << std::endl;
    std::cout << "Private key: n = " << n << " d = " << d << std::endl;

    std::cout << "Enter a positive number less than " << n <<":\n";
    std::cin >> m;

    c = modExp(m, e, n);
    std::cout << "Cipher: " << c << std::endl;

    std::cout << modExp(c, d, n);

    return 0;
}
Nitish
  • 11
  • 4
  • Most likely your bug is `${file}` in your `tasks.json` file. By default VSCode builds only the active file and ignores all other files. See that `main.cpp` is the only file compiled. The VSCode documentation explains this and tells you what to do to use more than 1 file here: [https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson](https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson) – drescherjm Jan 31 '22 at 02:30
  • The most important part that ties everything you posted together is the *one* thing that is likely related to your problem: the *build* command(s) that compile and link your code. Inconveniently, it isn't among the malaise. It is, however, present in the off-site picture , `g++ main.cpp -o main` . That means your RSA.cpp code isn't being compiled, much less linked, to the final target `main`. – WhozCraig Jan 31 '22 at 02:31
  • how can i make sure all files are compiled and linked? – Nitish Jan 31 '22 at 02:33
  • I gave you the link to the VSCode documentation that tells you to replace `${file},` with `"${workspaceFolder}\\*.cpp",` to support more than 1 source file. – drescherjm Jan 31 '22 at 02:34

1 Answers1

0

The problem was with the way VSCode compiles files.

By default VSCode builds only the active file and ignores all other files. See that main.cpp is the only file compiled. The VSCode documentation explains this and tells you what to do to use more than 1 file here:

– drescherjm

Nitish
  • 11
  • 4