0

I am working on a practical assignment which requires us to use an external function to determine whether a integer entered is a prime number. I have created the header file, external functions file and main file but when i compile using

g++ -o main main.cpp extFunc.cpp

but I get the error:
/tmp/cca073oR.o: In function 'main':
main.cpp:(.text+0x42): undefined reference to 'isPrime(int)'
collect2: error: ld returned 1 exit status

The following are my cpp and header classes:
1)extFunc.h

    bool isPrime(int num);


2)extFunc.cpp

    # include "extFunc.h"
bool isPrime(int num) {
    int i;
    bool numPrime;

    //Determine if number is prime
    if (num <= 0) {
        numPrime = false;
    }
    if (num =  1) {
        numPrime = false;
    }
    if (num = 2) {
        numPrime = true;
    }
    if (num = 3) {
        numPrime = true;
    }
    else {
        numPrime = true;
        for(i = 2; i < num; i++) {
            if ((num%i) == 0){
                numPrime = false;
                break;
            }
        }
    }

    //Return values
    if (numPrime == true) {
        return true;
    } else {
        return false;
    }
}


3) main.cpp

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

        using namespace std;

        int main() {
    //Variables
    int uNum;
    bool prime;

    cout << "Enter a number: ";
    cin >> uNum;

    prime = isPrime(uNum);

    if  (prime = true) {
        cout << uNum << " is prime"  << endl;
    } 
    else {
        cout << uNum << " is not prime" << endl;
    }
    return 0;
}


I have tried some of the other suggestions I was able to find on the site including using #ifndef in the header file but it did not fixed anything for me. I am unable to identify what possible could be the problem since the function in the header file and the function file is the same and it is called correctly (from what I can see) in the main file.

  • 5
    Cannot reproduce. Your code builds just fine using the command you posted. – Miles Budnek Mar 23 '18 at 23:51
  • 1
    Possible duplicate of [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) – 1201ProgramAlarm Mar 24 '18 at 01:22

2 Answers2

0

Thanks for all the responses. I managed to fix the issue. The university posted a guide to help us with the assignment this morning and I changed my makefile which fixed the issue.

This is what I had for my makefile

main: main.o extFunc.o
    g++ -o main main.o extFunc.o
main.o: main.cpp extFunc.h
    g++ -c main.cpp
extFunc.o: extFunc.h extFunc.cpp
    g++ -c extFunc.cpp
run:
    ./main
clean:
    rm *. main
0

I think you confuse between "=" and "==" for instance

if (num =  1) {
[...]
}

should be :

if (num == 1) {
[...]
}

Perhapse that the use of "const" keyword is a good deal in order to avoid this kind of mistakes.

The error occurs : on line 10, 13 and 16 in extFunc.cpp and line 16 in main.cpp

Bertrand N
  • 11
  • 2