Using CMake for a new library and namespace

Let me first apologize for all of the code, but I felt it was needed, anyone that helps will most likely need it anyway.

So I'm having issues with CMake, I want to make a libary and namespace for the classes found in my timer.h and sortablebase.h files, I also have a class in sortable.h that derives from sortablebase.h. The object is to be able to include the library in main.cpp via #include<sort/timer.h> and using the namespace sort either by "using" or by sort::

So let me start by showing the error I'm currently getting, and we'll go from there. This is after cmake CMakeLists.txt and make (these are just the top few since the lower errors are likely a result):

Linking CXX executable sorttest
CMakeFiles/sorttest.dir/main.cpp.o: In function `main':
main.cpp:(.text+0xcd): undefined reference to `sort::Timer::Timer()'
main.cpp:(.text+0xd9): undefined reference to `sort::Timer::Timer()'
main.cpp:(.text+0xe5): undefined reference to `sort::Timer::start()'

Here is the directory structure:

practicum_6
|-- CMakeLists.txt
|-- examples
| |-- CMakeLists.txt
| |-- main.cpp
| |-- sortable.cpp
| |-- sortable.h
|-- libsort
| |-- CMakeLists.txt
| |-- sortablebase.cpp
| |-- timer.cpp
|-- sort
|-- sortablebase.h
|-- timer.h
3 directories, 10 files


Root CMakeLists.txt:
1
2
3
4
5
6
7
8
cmake_minimum_required(VERSION 2.8)
project(PRACT5)

include_directories(${PROJECT_SOURCE_DIR})

set(CMAKE_CXX_FLAGS "-Wall")
add_subdirectory(${PROJECT_SOURCE_DIR}/examples)
add_subdirectory(${PROJECT_SOURCE_DIR}/libsort)


Examples CMakeLists.txt:
1
2
3
set (SORTEXE main.cpp sortable.cpp sortable.h)
add_executable(sorttest ${SORTEXE})
target_link_libraries(sorttest ${sort/timer.h})


Libsort CMakeLists.txt:
1
2
3
4
5
set(LIBSORT_HEADERS ${PROJECT_SOURCE_DIR}/sort/timer ${PROJECT_SOURCE_DIR}/sort/sortablebase)

set(LIBSORT_SOURCES timer sortablebase)

add_library(sort/timer ${LIBSORT_HEADERS} ${LIBSORT_SOURCES})


sortablebase.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef SORTABLEBASE_H
#define SORTABLEBASE_H

#include <vector>
#include <cstdlib>
#include <algorithm>

namespace sort{
class SortableBase {
public:
  void printFirst(const unsigned long& number) const;
  void printLast(const unsigned long& number) const;
protected:
  SortableBase(const std::vector<int>& data);
  std::vector<int> m_data;
};
}
#endif 


 


timer.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef TIMER_H
#define TIMER_H

namespace sort{
class Timer{
public:
  Timer();
  void start();
  void stop();
  double elapsed() const;
  void reset();
private:
  bool started;
  double m_start;
  double m_stop;
};
}

#endif 


sortablebase.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//#include "sortablebase.h"
#include "../sort/sortablebase.h"

#include <cassert>
#include <iostream>


sort::SortableBase::SortableBase(const std::vector<int>& data) :
m_data(data) {assert(!data.empty());}

void sort::SortableBase::printFirst(const unsigned long& number) const{
  assert(number<=m_data.size());
  for(size_t i=0; i<number; ++i)
    std::cout << m_data[i] << ' ';
  std::cout << std::endl;
}

void sort::SortableBase::printLast(const unsigned long& number) const {
  assert(number<=m_data.size());
  for(size_t i=m_data.size()-1; i>=m_data.size()-number; --i)
    std::cout << m_data[i] << ' ';
  std::cout << std::endl;
}


timer.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "../sort/timer.h"
#include <cassert>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif

sort::Timer::Timer() : started(false), m_start(0.0), m_stop(0.0){}

void sort::Timer::start() {
  assert(started==false);
  started = true;
  
#ifdef _WIN32
  DWORD start = GetTickCount();
  m_start = static_cast<double>(start);
#else
  timeval start;
  gettimeofday(&start, 0);
  m_start = 1.e6*static_cast<double>(start.tv_sec)
          + static_cast<double>(start.tv_usec);
#endif
}

void sort::Timer::stop() {
  assert(started==true);
  started = false;
  
#ifdef _WIN32
  DWORD stop = GetTickCount();
  m_stop = static_cast<double>(stop);
#else
  timeval stop;
  gettimeofday(&stop, 0);
  m_stop = 1.e6*static_cast<double>(stop.tv_sec)
         + static_cast<double>(stop.tv_usec);
#endif
}

double sort::Timer::elapsed() const {
  assert(started==false);
  return (m_stop-m_start);
}

void sort::Timer::reset() {
  assert(started==false);
  m_start=0.0;
  m_stop=0.0;
}


sortable.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "sortable.h"

//Practicum 4 code:
Sortable::Sortable(const std::vector<int>& data) : sort::SortableBase(data)
{
    
}

bool Sortable::DescendingComparer::operator()(const int& x, const int& y) const {
    
    if(x >= y) return 1;
    else return 0;
}

void Sortable::stlsort () {
    
    DescendingComparer compare;
    std::sort(m_data.begin(), m_data.end(), compare);
    
}

int compareDescending(const void* x, const void* y) {
    
    if(*(const int*)x > *(const int*)y) return -1;
    if(*(const int*)x == *(const int*)y) return 0;
    if(*(const int*)x < *(const int*)y) return 1;
    
}

void Sortable::qsort() {
    
   std::qsort(&m_data[0], m_data.size(), sizeof(int), compareDescending);
}


sortable.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* 
 * File:   sortable.h
 * Author: Joshua Evans
 *
 * Created on September 28, 2011, 11:27 AM
 */


//Practicum 4 code:
#include "../sort/sortablebase.h"

extern "C"
int compareDescending(const void* x, const void* y);

class Sortable : public sort::SortableBase {
    
public:
    
    class DescendingComparer {
    
    public:
        
        bool operator()(const int& x, const int& y) const;
    };
    
    Sortable(const std::vector<int>& data);
    
    void stlsort ();
    void qsort ();
    
};


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* 
 * File:   main.cpp
 * Author: Joshua Evans
 *
 * Created on September 21, 2011, 11:25 AM
 */
#include <iostream>
#include <vector>
#include <cstdlib>
#include "sortable.h"
#include <sort/timer.h>


using namespace std;

int main() {
    
    int vect_size;
    
    cout << "Vector size: ";
    cin >> vect_size;    
    
    vector<int> sort_vect;
    
    srand(0);
    
    for(int i=0; i < vect_size; i++){
        
        sort_vect.push_back(rand());
    
    }; 
    
    Sortable quick_srt(sort_vect);
    Sortable stl_srt(sort_vect);
    
    sort::Timer qtime;
    sort::Timer stime;
    
    qtime.start();
    quick_srt.qsort();
    qtime.stop();
    
    stime.start();
    stl_srt.stlsort();
    stime.stop();
    
    
    cout << "qsort first: ";
    quick_srt.printFirst(5);
    cout << "stl first: ";
    stl_srt.printFirst(5);
    
    cout << "qsort last: "; 
    quick_srt.printLast(5);
    cout << "stl last: ";
    stl_srt.printLast(5);
    
    cout << "qsort:   " << qtime.elapsed() << endl;
    cout << "stlsort: " << stime.elapsed() << endl;
    cout << "speedup: " << (qtime.elapsed()/stime.elapsed()) << endl;

    
    return 0;
}



The code did work before I wrote the CMake files, I have changed the includes a bit in attempts to get it to work, but the code is the same. I just want to understand what I need to do or how to create the functionality with CMake.

Here is a link to the actual assignment PDF, instructor is quite backlogged:

https://gateway-audio.vis.uky.edu/~jh/cs216/Practicum_Week_6.pdf
Topic archived. No new replies allowed.