我有一個簡單的程序,我完全從http://www.learncpp.com/cpp-tutorial/19-header-files/的示例中複製,因爲我正在學習如何使用多個文件創建C++程序。在一個非常簡單的C++程序中出現「undefined reference」錯誤
程序編譯,但建立時,出現以下錯誤:
/tmp/ccm92rdR.o:在功能主要: main.cpp中:(文本+ 0X1A):未定義參考`添加(INT ,INT)」 collect2:LD返回1退出狀態
下面的代碼:
的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.h
#ifndef ADD_H
#define ADD_H
int add(int x, int y); // function prototype for add.h
#endif
add.cpp
int add(int x, int y)
{
return x + y;
}
有誰知道爲什麼出現這種情況?
非常感謝。
的代碼幾乎是完美的。在'add.cpp'中包含'add.h'。將其編譯爲'g ++ main.cpp add.cpp',它會生成'a.out' – Arun 2013-04-04 02:54:44