ld:找不到架构x86_64的符号-错误 [英] ld: symbol(s) not found for architecture x86_64 - Error
问题描述
我正在尝试在Mac OS Catalina 10.15.2上的VSCode上运行代码.
I am trying to run a code on VSCode on Mac OS Catalina 10.15.2.
我在此功能上遇到此错误.
I am getting this Error on this function.
$ g++ main.cpp
Undefined symbols for architecture x86_64:
"nalea(int)", referenced from:_main in main-508a59.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation)
这是我正在使用的主文件-
This is the main file I am using -
#include<iostream>
#include "proba.h"
#include "constantes.h"
#include "affichage.h"
int main(){
int a = nalea(60);
std::cout<<a;
//int a = InitAffichage();
return 0;
}
这是从中调用该函数的proba.cpp文件.
This is the proba.cpp file from where the function is called.
#include <cstdlib> // pour rand()
#include <cstdio> // pour fprintf()
#include <cmath> // pour floor()
#include "proba.h" // types et déclaration des fonctions
int nalea( int max)
{
return (int)floor(rand()*((float)max)/RAND_MAX );
}
这是proba.h头文件
This is proba.h header file
int nalea(int max);
请帮助我,我是C ++的新手....
Please help me, I am new to C++....
推荐答案
我已经多次遇到这种情况,并且在大多数情况下,这与您的路径中的空格有关.
I have encountered this multiple times, and in most cases it is related to spaces in you path.
您可以做的简单测试是将您的项目放在一个文件夹中,该文件夹的路径中没有空格,例如您的用户主目录.
The simple test you can make is to put your project in a folder without spaces in its path e.g. your user home directory.
要对其进行修复,以便可以将项目放置在任何文件夹中,需要首先制作一个带有构建信息的task.json文件,然后根据vscode
To fix it so you can have the project in any folder, you need to first make a tasks.json file with the build information and compile all *.cpp according to the guide from vscode https://code.visualstudio.com/docs/cpp/config-clang-mac
- 打开您的main.cpp
- 转到菜单终端",选择配置默认构建任务"
-
选择"C/C ++:clang ++构建活动文件".您应该获得一个具有以下信息的task.json文件
- Open your main.cpp
- Go to the menu "Terminal", select "Configure Default Build Task"
Select "C/C++: clang++ build active file". You should get a tasks.json file with the following information
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: clang++ build active file",
"command": "/usr/bin/clang++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
现在关键的部分是您应该将"$ {file}",
更改为"\" $ {workspaceFolder} \"/*.cpp",代码>.请注意两个
\"
,这将确保处理路径中的任何空格,这在前面链接的vscode指南中未提及
Now the critical part is that you should change "${file}",
to "\"${workspaceFolder}\"/*.cpp",
. Take note of the two \"
, which will make sure to handle any spaces in your path, this is not mention in the vscode guide linked earlier
这篇关于ld:找不到架构x86_64的符号-错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!