1

I am building my own library of functions I use to help me learn C++. I am attempting a simple QT application and when I run the app I get an undefined error. Not sure what I am doing wrong, and its probably a noob thing.

Library Files

CC_Number.h

#ifndef CC_NUMBER_H
#define CC_NUMBER_H
#include <string>

using namespace std;

class CC_Number
{
public:
    CC_Number();
    virtual ~CC_Number();
    int randCC(int imin=0, int imax=1);
    int arraySize(int x[]);
    int arraySize(double x[]);
    int arraySize(float x[]);
    int arraySize(string x[]);
    string int_to_str(int i);
protected:
private:
};
#endif // CC_NUMBER_H

CC_Number.cpp

#include "CC_Number.h"
#include <sstream>

using namespace std;

CC_Number::CC_Number()
{
//ctor
}

CC_Number::~CC_Number()
{
//dtor
}

string CC_Number::int_to_str(int i){
stringstream ss;
ss << i;
string str = ss.str();
return str;
}

Code That Calls The Library

Animal_Birthdate.h

#ifndef ANIMAL_BIRTHDATE_H
#define ANIMAL_BIRTHDATE_H
#include "/home/mongo/Cpp/CC_Cpp/CC_Number.h"
#include <iostream>
#include <string>

using namespace std;

class Animal_Birthdate
{
public:
Animal_Birthdate();
Animal_Birthdate(int m, int d, int y);
string getBirthdate();
private:
int intMonth;
int intDay;
int intYear;
};

#endif // ANIMAL_BIRTHDATE_H

Animal_Birthdate.cpp

#include "animal_birthdate.h"
#include "/home/mongo/Cpp/CC_Cpp/CC_Number.h"
#include <iostream>
#include <string>

using namespace std;

Animal_Birthdate::Animal_Birthdate()
:intMonth(1), intDay(1), intYear(1)
{

}

Animal_Birthdate::Animal_Birthdate(int m, int d, int y)
:intMonth(m), intDay(d), intYear(y)
{

}

string Animal_Birthdate::getBirthdate()
{
CC_Number c;
string bd = c.int_to_str(intMonth);
return bd;
}

Errors I am Receiving /home/mongo/Cpp/CC_Herd_Manager/animal_birthdate.o:-1: In function `Animal_Birthdate::getBirthdate()':

/home/mongo/Cpp/CC_Herd_Manager/animal_birthdate.cpp:-1: error: undefined reference to `CC_Number::CC_Number()'

/home/mongo/Cpp/CC_Herd_Manager/animal_birthdate.cpp:-1: error: undefined reference to `CC_Number::int_to_str(int)'

Contents of CC_Cpp.pro

#-------------------------------------------------
#
# Project created by QtCreator 2013-05-31T10:42:21
#
#-------------------------------------------------

QT       -= core gui

TARGET = CC_Cpp
TEMPLATE = lib

DEFINES += CC_CPP_LIBRARY

SOURCES += \
CC_Number.cpp \
CC_File.cpp

HEADERS +=\
    cc_cpp_global.h \
CC_Number.h \
CC_File.h

unix:!symbian {
maemo5 {
    target.path = /opt/usr/lib
} else {
    target.path = /usr/lib
}
INSTALLS += target
}
Talon06
  • 1,756
  • 3
  • 27
  • 51

2 Answers2

0

It doesn't look that you need "a library" for what you're doing. I'd suggest simply using multiple translation units linked together into a simple application as a starting point. I.e. remove TEMPLATE = lib and you'll be set.

If you'd like to learn how to create libraries with qmake, read about the TEMPLATE = subdirs and read qmake's documentation which contains all information about how to produce libraries, what the difference between static and dynamic ones are, how to link to them, etc.

Jan Kundrát
  • 3,700
  • 1
  • 18
  • 29
  • That worked...sorta. It allowed me to find some errors in my code, but now I am getting /usr/lib/crt1.o:-1: In function `_start': (.text+0x18):-1: error: undefined reference to `main' – Talon06 May 31 '13 at 19:22
  • I replaced the TEMPLATE = lib in the .pro file and I can compile it completely now however when I try compiling the main program I still get the undefined error could it be the way I am linking the .h file? – Talon06 May 31 '13 at 21:15
  • That's because you do not have a ``main`` function. – Jan Kundrát Jun 01 '13 at 14:32
  • I added a main function and set the template to TEMPLATE = app however I still get an undefined reference to the classes I am trying to use. – Talon06 Jun 05 '13 at 14:02
0

I added the following to the template file of the project that was trying to use the library and it worked.

SOURCES += main.cpp\
    mainwindow.cpp \
animal.cpp \
email.cpp \
owner.cpp \
phone.cpp \
/home/mongo/Cpp/CC_Cpp/CC_Number.cpp \
/home/mongo/Cpp/CC_Cpp/CC_Date.cpp \
/home/mongo/Cpp/CC_Cpp/CC_TimeStamp.cpp

HEADERS  += mainwindow.h \
animal.h \
email.h \
owner.h \
phone.h \
/home/mongo/Cpp/CC_Cpp/CC_Number.h \
/home/mongo/Cpp/CC_Cpp/CC_Date.h \
/home/mongo/Cpp/CC_Cpp/CC_TimeStamp.h
Talon06
  • 1,756
  • 3
  • 27
  • 51