main.cpp
#include <iostream>
#include "add.h" // this brings in the declaration for add()
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is " << add(3,4) << endl;
return 0;
}
add.cpp
int add(int x,int y)
{
return x+y;
}
add.h
#ifndef ADD_H
#define ADD_H
int add(int x,int y); //function prototype for add.h
#endif
在Microsoft Visual C++ 2010下 IDE下编译自动识别,编译不会出错 在Linux下 直接编译main.cpp是会报
g++ -o main main.cpp
/tmp/ccVwA0IL.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `add(int, int)'
collect2: ld 返回 1
解决方法
g++ -o main add.cpp main.cpp
编译通过