0
我在C++编程和一般编程方面很新颖。最近我们已经在课堂上将头文件和其他源文件(实现)添加到我们的程序中。我试过编写最简单的程序,以确保我理解将多个文件包含到一个程序中的基础知识,但它们不会编译,从而导致链接器错误。CodeRunner--无法编译多个源代码的C++程序
即:
Undefined symbols for architecture x86_64:
"FooBar::printSomething()", referenced from:
_main in main-d52d70.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
当我通过 “g ++ main.cpp中implementation.cpp” 终端编译,一切工作就好了。
我的代码:
的main.cpp
#include "header.h"
int main()
{
FooBar object;
object.printSomething();
return 0;
}
header.h
#ifndef _Header_h_
#define _Header_h_
#include <iostream>
using namespace std;
class FooBar{
public:
void printSomething();
private:
string helloWorld;
};
#endif
implementation.cpp
#include "header.h"
void FooBar::printSomething(){
helloWorld = "Hello World!\n";
cout << helloWorld;
}
我喜欢CodeRunner,但这真的让我很沮丧。
谢谢!
您确定您已将'implementation.cpp'添加到叮当中的待链接对象吗?另外,在Header.h中添加'#include' –