的main.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
int main(){
Burrito b;
return 0;
}
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito{
public:
Burrito();
};
#endif
Burrito.cpp
#include "Burrito.h"
#include <iostream>
Burrito::Burrito(){
}
编译&amp;链接:
lzsb$ g++ main.cpp -o main
Undefined symbols for architecture x86_64:
"Burrito::Burrito()", referenced from:
_main in ccVpCr0z.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
lzsb$
平台:
Mac OS 10.6.8
G++ : i686-apple-darwin10 --with-gxx-include-dir=/usr/include/c++/4.2.1
答案 0 :(得分:9)
您还需要编译Burrito.cpp
文件。编译器从每个.cpp文件创建目标文件,然后将它们链接起来。这是您的调用失败的地方,因为链接器无法在任何目标文件中找到引用的Burrito
类。要修复编译器调用,只需添加Burrito.cpp
g++ main.cpp Burrito.cpp -o main
答案 1 :(得分:4)
您的编译行应该是:
g ++ Burrito.cpp main.cpp -o main