1

I try to make simple example from simplest book on C++. But I receive a error. Please, be lenient to my poor knowledge. This is not duplicate of C++ LNK1120 Fatal Error: 1 unresolved externals

File prime.h

bool is_prime(int);

File prime.cpp

// prime.cpp
#include <cmath> // Needed for sqrt
#include "prime.h" // is_prime prototype
/** is_prime(n)
* Determines the primality of a given value
* n an integer to test for primality
* Returns true if n is prime; otherwise, returns false
*/
bool is_prime(int n) {
    bool result = true; // Provisionally, n is prime
    double r = n, root = sqrt(r);
    // Try all possible factors from 2 to the square
    // root of n
    for (int trial_factor = 2; result && trial_factor <= root; trial_factor++)
        result = (n % trial_factor != 0);
    return result;
}

File for tests.cpp

#include <iostream>
#include "prime.h"
/*
* main
* Tests for primality each integer from 2
* up to a value provided by the user.
* If an integer is prime, it prints it;
* otherwise, the number is not printed.
*/
int main() {
    int max_value;
    std::cout << "Display primes up to what value? ";
    std::cin >> max_value;
    for (int value = 2; value <= max_value; value++)
        if (is_prime(value)) // See if value is prime
            std::cout << value << " "; // Display the prime number
    std::cout << '\n'; // Move cursor down to next line
}

They are in 1 folder:

enter image description here

prime.h is included as header

enter image description here

Subsystem Console is chosen:

enter image description here

I get such error. Please help me.

------ Build started: Project: for tests, Configuration: Release x64 ------

for tests.obj : error LNK2001: unresolved external symbol "bool __cdecl is_prime(int)" (?is_prime@@YA_NH@Z)

D:_c++\for tests\x64\Release\for tests.exe : fatal error LNK1120: 1 unresolved externals

Done building project "for tests.vcxproj" -- FAILED.

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

NutCracker
  • 11,485
  • 4
  • 44
  • 68
Igor K.
  • 813
  • 6
  • 17

0 Answers0