[英]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在此处输入图像描述
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.