Home > Back-end >  Undefined symbols for architecture arm64: clang: error: linker command failed with exit code 1
Undefined symbols for architecture arm64: clang: error: linker command failed with exit code 1

Time:01-30

I have some problems when I try to run my main.cpp file, only with mac gcc/clang/g compiler.

Here is the code:

random.h

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>

void initialize();
float randomFloat(int, int, int);

random.cpp

#include "random.h"

void initialize() { srand(time(NULL)); }

float randomFloat(int min, int max, int p) {
  int intPart = rand() % (max - (min - 1))   min;
  if (intPart == max) {
    intPart--;
  }
  float decimal = (float)(rand() % (int)pow(10, p)) / pow(10, p);
  return intPart   decimal;
}

util.h

#include <iostream>

int askInteger(const char *, bool);

util.cpp

#include "util.h"

using namespace std;

int askInteger(const char *message, bool onlyPositive) {
  int number;
  if (onlyPositive) {

    while (cout << "Type a correct " << message << ": ",
           !(cin >> number) || number < 0) {
      cerr << "Input error, try again. \n";
      if (cin.fail()) {
        cin.clear();
        cin.ignore();
      }
    }

  } else {
    while (cout << "Type a correct " << message << ": ", !(cin >> number)) {
      cerr << "Input error, try again. \n";
      if (cin.fail()) {
        cin.clear();
        cin.ignore();
      }
    }
  }
  return number;
}

main.cpp

#include "random/random.h"
#include "util/util.h"

using namespace std;

int main() {
  int n, min, max, p;

  n = askInteger("numbers quantity", true);
  p = askInteger("precession", true);
  min = askInteger("min value (included)", false);
  max = askInteger("max value (included)", false);

  while (max <= min) {
    cout << "max value should be greather than " << min << "\n";
    max = askInteger("max value (included)", false);
  }

  initialize();

  for (int i = 0; i < n; i  ) {
    cout << randomFloat(min, max, p) << "\n";
  }
}

And It gives me this result:

Undefined symbols for architecture arm64:
  "askInteger(char const*, bool)", referenced from:
      _main in main-2552a5.o
  "initialize()", referenced from:
      _main in main-2552a5.o
  "randomFloat(int, int, int)", referenced from:
      _main in main-2552a5.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

vscode applies this command to run the code:

Random-Numbers % cd "/Users/user/Downloads/Random-Numbers/" && g   main.cpp -
o main && "/Users/user/Downloads/Random-Numbers/"main

Here is my project structure:

evidence

commands g

  • Related