简体   繁体   English

架构 arm64 的未定义符号:clang:错误:linker 命令失败,退出代码为 1

[英]Undefined symbols for architecture arm64: clang: error: linker command failed with exit code 1

I have some problems when I try to run my main.cpp file, only with mac gcc/clang/g++ compiler.当我尝试运行我的 main.cpp 文件时遇到一些问题,仅使用 mac gcc/clang/g++ 编译器。

Here is the code:这是代码:

random.h随机.h

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

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

random.cpp随机.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工具.h

#include <iostream>

int askInteger(const char *, bool);

util.cpp工具.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主.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: vscode 应用此命令来运行代码:

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

Here is my project structure:这是我的项目结构:

enter image description here在此处输入图像描述

I have tried already我已经试过了

gcc *.h
gcc -c *.h
g++ *.cpp
g++ -o *.cpp
g++ -c *.cpp

AT LAST!最后! I could run it我可以运行它

c++ vscode default commands doesn't match with g++ mac compiler c++ vscode 默认命令与 g++ mac 编译器不匹配

c++ vscode default command to run main: c++ vscode默认运行main的命令:

d "/Users/user/Downloads/Random-Numbers/" && g++ main.cpp -o main && "/Users/user/Downloads/Random-Numbers/"main
user@MacBook-Pro-de-user Random-Numbers % cd "/Users/user/Downloads/Random-Numbers/" && g++ main.cpp -o main && "/Users/
user/Downloads/Random-Numbers/"main

you have to compile by terminal with the command:您必须使用以下命令通过终端进行编译:

user@MacBook-Pro-de-user Random-Numbers % g++ -o ./main.exe random/random.cpp util/util.cpp main.cpp
user@MacBook-Pro-de-user Random-Numbers % ./main.exe     

证据

commands g++命令 g++

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:ld:未找到体系结构 arm64 的符号,clang:错误:linker 命令失败,退出代码为 1(使用 -v 查看调用) - Error : ld: symbol(s) not found for architecture arm64, clang: error: linker command failed with exit code 1 (use -v to see invocation) 更新到Mac OS 10.8之后:“体系结构x86_64的未定义符号”; “ c声:错误:链接器命令失败,退出代码为1” - After update to Mac OS 10.8: “Undefined symbols for architecture x86_64”; “clang: error: linker command failed with exit code 1” MacOS 中架构 arm64 的未定义符号 - Undefined symbols for architecture arm64 in MacOS GLFW“架构 arm64 的未定义符号” - GLFW "Undefined symbols for architecture arm64" 在体系结构x86_64 clang中找不到符号:错误:链接器命令失败,退出代码为1(使用-v查看调用) - symbol(s) not found in architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 使用 clang-14 链接到 fftw-3 的架构 arm64 的未定义符号 - Undefined symbols for architecture arm64 linking to fftw-3 with clang-14 架构 arm64、Xcode 12.3 的未定义符号 - Undefined symbols for architecture arm64, Xcode 12.3 架构 arm64 的未定义符号:_BN_new - Undefined symbols for architecture arm64: _BN_new 在 MacOS 上编译 c++ 和汇编代码时架构 arm64 的未定义符号 - Undefined symbols for architecture arm64 when compiling c++ and assembly code on MacOS ld:找不到架构 x86_64 的符号,clang:链接器命令失败 - ld: symbols not found for architecture x86_64, clang: linker command failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM