ld:未找到架构 x86_64 的符号 - 错误

新手上路,请多包涵

我正在尝试在 Mac OS Catalina 10.15.2 上的 VSCode 上运行代码。

我在此功能上收到此错误。

  $ 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)

这是我正在使用的主文件 -

 #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 文件。

  #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 头文件

     int nalea(int max);

请帮助我,我是 C++ 新手….

原文由 DJ_92 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 637
1 个回答

我多次遇到这种情况,在大多数情况下,它与路径中的空格有关。

您可以进行的简单测试是将您的项目放在其路径中没有空格的文件夹中,例如您的用户主目录。

要修复它以便您可以将项目放在任何文件夹中,您需要首先使用构建信息制作一个 tasks.json 文件,并根据 vscode https://code.visualstudio.com/docs 的指南编译所有 *.cpp /cpp/config-clang-mac

  1. 打开你的 main.cpp
  2. 转到菜单“终端”,选择“配置默认构建任务”
  3. 选择“C/C++:clang++ 构建活动文件”。您应该得到一个包含以下信息的 tasks.json 文件
   {
       "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
               }
           }
       ]
   }

  1. 现在关键部分是您应该将 "${file}", 更改为 "\"${workspaceFolder}\"/*.cpp", 。注意两个 \" ,这将确保处理路径中的任何空格,这在前面链接的 vscode 指南中没有提及

  2. 要构建,请转到菜单“终端”并选择“运行构建任务”或按“Shift+Cmd+B”

原文由 Simon Bøgh 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏