clang: error: linker command failed with exit code 1

vscode 分文件编写函数错误:clang: error: linker command failed with exit code 1

Undefined symbols for architecture x86_64: “swap(int, int)”, referenced from:_main in 函数的分文件编写-591e2c.old: symbol(s) not found for architecture x86_64

调用.h文件时,出现error

#include 
using namespace std;
#include "swap.h" //调用自己写的头文件
int main(){
    int a=10;
    int b=20;
    swap(a,b);
    return 0;
} 

会出现以下报错

Undefined symbols for architecture x86_64:
"swap(int, int)", referenced from:_main in 函数的分文件编写-9a621a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

解决方式
将头文件中的调用 “swap.h"改为"swap .cpp” 可以正常执行

#include 
using namespace std;
#include "swap.cpp" //调用自己写的头文件
int main(){
    int a=10;
    int b=20;
    swap(a,b);
    return 0;
}

或者增加头文件swap.cpp也可执行

#include 
using namespace std;
#include "swap.h" //调用自己写的头文件
#include "swap.cpp"
int main(){
    int a=10;
    int b=20;
    swap(a,b);
    return 0;
}

报错原因是vscode中自定义函数头文件与源文件没有完全耦合

你可能感兴趣的:(c++,c++,函数式编程)