ld: symbol(s) not found for architecture x86_64clang: error: linker command failed with exit code 1

初学者在使用VSCode进行C++编程时遇到编译错误:ld: symbol(s) not found for architecture x86_64。该错误通常由于链接器找不到函数定义导致。解决方法是在主程序源文件中正确包含头文件和函数源文件。确保在主程序中不仅包含函数声明的头文件,还需直接包含实现函数的源文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:使用vscode初学c++,学习函数的分文件编写。遇到报错:

ld: symbol(s) not found for architecture x86_64clang: error: linker command failed with exit code 1

函数分文件编写一般有四个步骤:

  1. 创建后缀名为.h的头文件

  2. 创建后缀名为.cpp的源文件

  3. 在头文件中写函数声明

  4. 在源文件中写函数定义

函数声明的头文件 swap.h

#include <iostream>
using namespace std;
//函数的声明
void swap(int a, int b);

函数定义的源文件 swap.cpp

#include "swap.h"

void swap(int a, int b)
{
    int temp = a;
    a = b;
    b = temp;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
}

包含main函数的源文件

// #include <iostream>
// using namespace std;
#include "swap.h"

int main()
{
    int a = 10;
    int b = 20;
    swap(a, b);
    system("pause");
    return 0;
}

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值