我正在尝试在 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 许可协议
我多次遇到这种情况,在大多数情况下,它与路径中的空格有关。
您可以进行的简单测试是将您的项目放在其路径中没有空格的文件夹中,例如您的用户主目录。
要修复它以便您可以将项目放在任何文件夹中,您需要首先使用构建信息制作一个 tasks.json 文件,并根据 vscode https://code.visualstudio.com/docs 的指南编译所有 *.cpp /cpp/config-clang-mac
现在关键部分是您应该将
"${file}",
更改为"\"${workspaceFolder}\"/*.cpp",
。注意两个\"
,这将确保处理路径中的任何空格,这在前面链接的 vscode 指南中没有提及要构建,请转到菜单“终端”并选择“运行构建任务”或按“Shift+Cmd+B”