0

I use Mac OS X. Then I wrote a simple program. However, I have an error with compiling in terminal.

My terminal code is : g++ main.cpp -o main

Then error is:

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)

I don't understand this error because when I build it in XCODE6 it doesn't give any error.

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;
}

UPDATED

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 

AlgorithmSortK.cpp:

#include "AlgorithmSortK.h"

AlgorithmSortK::AlgorithmSortK(int k) : SelectionAlgorithm(k){

}
int AlgorithmSortK::select(){
    return SelectionAlgorithm::k;
}

AlgorithmSortK.h:

#ifndef AlgorithmSortK__
#define AlgorithmSortK__
#include "SelectionAlgorithm.h"
class AlgorithmSortK : public SelectionAlgorithm{
    public:
        int select();
    public:
        AlgorithmSortK(int k);
};
#endif

I don't understand the problem. I can run in Xcode and I don't compile with terminal.. Kind Regards.

Ömer ASLAN
  • 139
  • 3
  • 15
  • you should also include `TestBed.cpp` on the commandline – wimh Oct 14 '14 at 19:26
  • try `g++ main.cpp TestBed.cpp -o main`, see also [Using G++ to compile multiple .cpp and .h files](http://stackoverflow.com/q/3202136/33499) – wimh Oct 14 '14 at 19:27
  • now the error is: `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) ` – Ömer ASLAN Oct 14 '14 at 19:28
  • you have a `AlgorithmSortK.h`, do you also have a `AlgorithmSortK.cpp`? – wimh Oct 14 '14 at 19:29
  • Yes of course I will add all files. – Ömer ASLAN Oct 14 '14 at 19:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63057/discussion-between-omer-aslan-and-wimmel). – Ömer ASLAN Oct 14 '14 at 19:31
  • Splitting simple definitions from the declarations to an additional cpp file is a bad idea! It complicates the compilation, makes it impossible to inline the code, brakes optimization in any way and makes it harder to understand if there are not a lot of comments. I prefer to inline as much as is possible if no circular dependency needs splitting declarations from definitions. Maybe the compile time increases and in complex scenarios the dependency grows which is maybe not acceptable. But in all other cases: Do not split in separate files! – Klaus Oct 14 '14 at 19:37

1 Answers1

4

Your program is built from multiple source files, so simplest way would be:

g++ main.cpp TestBed.cpp -o main

and put more .cpp files there the same way if you use them.

better way to compile each .cpp file into .o file and then link them together:

g++ -c main.cpp 
g++ -c TestBed.cpp
g++ main.o Testbed.o -o main

Then if you change one source you do not have to recompile everything. But that can be better done by utilities like make or your IDE

Slava
  • 43,454
  • 1
  • 47
  • 90
  • It doesn't work. It gives error which is `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)` – Ömer ASLAN Oct 14 '14 at 19:35
  • 1
    @ÖmerASLAN just add all your .cpp files to the g++ command line like Slava suggests you. – Anton Savin Oct 14 '14 at 19:38
  • @AntonSavin you're amazing ! However I don't understand the problem. why should I do ? Best Regards ! – Ömer ASLAN Oct 14 '14 at 19:42
  • @ÖmerASLAN The compiler needs to know what files you want compiled, it isn't psychic. – Red Alert Oct 14 '14 at 19:49