CPP - 编译错误 (G++)
Posted
技术标签:
【中文标题】CPP - 编译错误 (G++)【英文标题】:CPP - Compiling error (G++) 【发布时间】:2014-10-14 19:20:14 【问题描述】:我使用 Mac OS X。然后我编写了一个简单的程序。但是,我在终端中编译时出错。
我的终端代码是:g++ main.cpp -o main
那么错误是:
Undefined symbols for architecture x86_64:
"TestBed::TestBed()", referenced from:
_main in main-3003ff.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不明白这个错误,因为当我在 XCODE6 中构建它时,它没有给出任何错误。
TestBed.cpp:
#include "TestBed.h"
using namespace std;
TestBed::TestBed()
void TestBed::execute()
int x ;
x = algorithm->select();
cout << "x: " << x;
void TestBed::setAlgorithm(int type, int k)
if(type==1)
algorithm = new AlgorithmSortAll(k);
else if(type==2)
algorithm = new AlgorithmSortK(k);
TestBed::~TestBed()
TestBed.h:
#ifndef TestBed__
#define TestBed__
#include <iostream>
#include "SelectionAlgorithm.h"
#include "AlgorithmSortAll.h"
#include "AlgorithmSortK.h"
class TestBed
private:
SelectionAlgorithm *algorithm;
public:
//If I delete virtual keyword in execute,setAlgorithm,~TestBed
//It gives 3 more errors.
virtual void execute();
virtual void setAlgorithm(int type, int k);
TestBed();
virtual ~TestBed();
;
#endif
main.cpp:
#include <iostream>
#include "TestBed.h"
using namespace std;
int main()
TestBed *tb = new TestBed();
int algorithm_type;
cin >> algorithm_type;
int k;
cin >> k;
tb->setAlgorithm(algorithm_type, k);
tb->execute();
delete tb;
return 0;
更新
AlgorithmSortAll.cpp: #include "AlgorithmSortAll.h"
AlgorithmSortAll::AlgorithmSortAll(int k) : SelectionAlgorithm(k)
int N;
std::cin >> N;
int *pNums = 0;
pNums = new int[N];// Allocate n ints and save the pointer in pNums
for (int i=0; i<N; i++)
int number;
std::cin >> number;
pNums[i] = number;
//Sorting
int i, j, moved;
for (i = 0; i < N; i++)
moved = pNums[i];
j = i;
while (j > 0 && pNums[j - 1] > moved)
pNums[j] = pNums[j - 1];
j--;
pNums[j] = moved;
//Assignin k
SelectionAlgorithm::k = pNums[k];
delete [] pNums; // When done, free the memory pointed to by pNums
pNums = 0;
int AlgorithmSortAll::select()
return SelectionAlgorithm::k;
AlgorithmSortAll::~AlgorithmSortAll()
AlgorithmSortAll.h:
#ifndef AlgorithmSortAll__
#define AlgorithmSortAll__
#include "SelectionAlgorithm.h"
class AlgorithmSortAll : public SelectionAlgorithm
public:
virtual int select();
AlgorithmSortAll(int k);
virtual ~AlgorithmSortAll();
;
#endif
算法排序K.cpp:
#include "AlgorithmSortK.h"
AlgorithmSortK::AlgorithmSortK(int k) : SelectionAlgorithm(k)
int AlgorithmSortK::select()
return SelectionAlgorithm::k;
算法排序K.h:
#ifndef AlgorithmSortK__
#define AlgorithmSortK__
#include "SelectionAlgorithm.h"
class AlgorithmSortK : public SelectionAlgorithm
public:
int select();
public:
AlgorithmSortK(int k);
;
#endif
我不明白这个问题。我可以在 Xcode 中运行,但我不使用终端编译.. 亲切的问候。
【问题讨论】:
您还应该在命令行中包含TestBed.cpp
试试g++ main.cpp TestBed.cpp -o main
,另见Using G++ to compile multiple .cpp and .h files
现在错误是:Undefined symbols for architecture x86_64: "AlgorithmSortK::AlgorithmSortK(int)", referenced from: TestBed::setAlgorithm(int, int) in TestBed-c2c82a.o "AlgorithmSortAll::AlgorithmSortAll(int)", referenced from: TestBed::setAlgorithm(int, int) in TestBed-c2c82a.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
你有AlgorithmSortK.h
,你也有AlgorithmSortK.cpp
吗?
是的,我当然会添加所有文件。
【参考方案1】:
您的程序是由多个源文件构建的,因此最简单的方法是:
g++ main.cpp TestBed.cpp -o main
如果您使用更多的 .cpp 文件,请以相同的方式放在那里。
将每个 .cpp 文件编译成 .o 文件然后将它们链接在一起的更好方法:
g++ -c main.cpp
g++ -c TestBed.cpp
g++ main.o Testbed.o -o main
然后,如果您更改一个源,则不必重新编译所有内容。但这可以通过 make
或您的 IDE 等实用程序来更好地完成
【讨论】:
它不起作用。它给出的错误是Undefined symbols for architecture x86_64: "AlgorithmSortK::AlgorithmSortK(int)", referenced from: TestBed::setAlgorithm(int, int) in TestBed-c2c82a.o "AlgorithmSortAll::AlgorithmSortAll(int)", referenced from: TestBed::setAlgorithm(int, int) in TestBed-c2c82a.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
@ÖmerASLAN 只需按照 Slava 的建议将所有 .cpp 文件添加到 g++ 命令行即可。
@AntonSavin 你太棒了!但是我不明白这个问题。我为什么要这样做?最好的问候!
@ÖmerASLAN 编译器需要知道你要编译什么文件,这不是通灵的。以上是关于CPP - 编译错误 (G++)的主要内容,如果未能解决你的问题,请参考以下文章