编译cpp代码时出错:collect2:错误:ld返回1退出状态 我正在学习C++,并且在遇到编译器错误时,正在学习如何处理多个文件: “在函数main'中: bravo.cpp:(.text+0x71):未定义对sum(int,int)”的引用 collect2:错误:ld返回1退出状态“

编译cpp代码时出错:collect2:错误:ld返回1退出状态 我正在学习C++,并且在遇到编译器错误时,正在学习如何处理多个文件: “在函数main'中: bravo.cpp:(.text+0x71):未定义对sum(int,int)”的引用 collect2:错误:ld返回1退出状态“,c++,g++,C++,G++,我制定的准则是: 主要功能 #include<iostream> #include "add.h" int main(){ int a,b; std::cout<<"Give me two numbers to add: \n"; //std::cin>>a>>b; std::cout<<"The sum of two numbers is "<< sum(10,5)<< std::endl; } #inc

我制定的准则是: 主要功能

#include<iostream>
#include "add.h"
int main(){

int a,b;
std::cout<<"Give me two numbers to add: \n";
//std::cin>>a>>b;
std::cout<<"The sum of two numbers is "<< sum(10,5)<< std::endl; 
}
#include<iostream>
#include "add.h"

int sum(int a, int b){
return a+b;
}
求和函数

#include<iostream>
#include "add.h"
int main(){

int a,b;
std::cout<<"Give me two numbers to add: \n";
//std::cin>>a>>b;
std::cout<<"The sum of two numbers is "<< sum(10,5)<< std::endl; 
}
#include<iostream>
#include "add.h"

int sum(int a, int b){
return a+b;
}
#包括
#包括“add.h”
整数和(整数a,整数b){
返回a+b;
}
那么我哪里出了问题? 我尝试检查堆栈溢出错误,但无法实现问题的解决方案? 有什么想法吗?
谢谢

您收到链接器错误,因为您可能刚刚尝试编译main.cpp函数。链接器随后无法找到总和(int,int),因为您也没有编译add.cpp函数

假设您使用的是g++,您可以像这样编译这两个文件:

g++ main.cpp add.cpp
它会起作用的