0
我正在學習C++,我正在學習如何使用頭文件。但是,當我運行該程序時總會出現錯誤。爲什麼我不能在mac上使用g ++來運行這個簡單的C++代碼?
/* File add.h */
#ifndef ADD_H
#define ADD_H
int add(int, int);
#endif
/* ADD_H */
/* File add.cpp */
#include "add.h"
int add(int a, int b)
{
return a + b;
}
/* File triple.cpp */
#include<iostream>
#include "add.h"
using namespace std;
int triple(int);
int triple(int x)
{
return add(x, add(x, x));
}
int main()
{
int i=0;
int j;
while (i<=5)
{ j=triple(i);
cout<<j<<endl;
//cout<<triple(j)<<endl;
i++;
}
return 0;
}
這些是我使用的3個文件。當我運行:G ++ triple.cpp 在Mac上,錯誤給出如下:
Undefined symbols for architecture x86_64:
"add(int, int)", referenced from:
triple(int) in triple-e0558f.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
誰能給我這個錯誤的提示。非常感謝! 順便說一句,gcc的版本信息如下:
gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.51) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
g ++ add.cpp triple.cpp -o out –