0

I want to include a class from one project in another project.

I have a subdirs project that contains two sub projects, a windows qt console application and an autotest project to test the console application. My console application contains one class which I want to pull into my unit tests for testing:

Here's the header:

// calculator.h:
#ifndef CALCULATOR_H
#define CALCULATOR_H

class Calculator{

private:

public:
    Calculator(int year);

    int getYear(){ return 666; }
    int getMonth();
    int getDay();
};

#endif

Here's the source:

// calculate.cpp
"#include "calculator.h"

Calculator::Calculator(int year){}

int Calculator::getMonth(){
    return 42;
}

int Calculator::getDay(){
    return 3333;
}

Here's how my unit test looks:

//tst_foobar.cpp

#include <QtTest>
//#include "../Calculator/calculator.h"
#include "../Calculator/calculator.cpp"

// add necessary includes here

class Foobar : public QObject
{
    Q_OBJECT

private slots:
    void test_case1();
};

void Foobar::test_case1()
{
    Calculator myCalc(42);
}

QTEST_APPLESS_MAIN(Foobar)

#include "tst_foobar.moc"

My problem is when I include the other subdir's header file like this: #include "../Calculator/calculator.h" it doesn't work properly. I cannot test any of the class functions defined in calculator.cpp. I can explicitly include calculate.cpp like this, #include "../Calculator/calculator.cpp" and my tests work as expected, but is this the proper way to do it?

I've never seen .cpp files included into files like this, only the header? But if only include the header, the header doesn't include the function definitions in calculator.cpp? Should my header file include the .cpp file? That way I could include just the header in other files like you often see in C++. By why then does the class generated by QT Creator do things the other way around? Creates a header file and a .cpp file, and the .cpp file is the one that includes the header??

Very new to C++ programming and a bit confused. Detailed help greatly appreciated.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47

3 Answers3

0

Your questions are going to be answered as soon as you use CMake, so this might help.

All that boring part to find headers and cpp files are done within a CMakeLists.txt file. Then an executable file is created when you use add_executable()

Be aware that you will probably create a build directory to be "clean".

I think your CMakeLists.txt file is going to be something like

cmake_minimum_required(VERSION 2.8.11)
project(your_project_name)

enable_testing()

# Tell CMake to run moc when necessary:
set(CMAKE_AUTOMOC ON)

# As moc files are generated in the binary dir, tell CMake
# to always look for includes there:
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Test REQUIRED)

add_executable(foo your_test.cpp your_src.cpp)
add_test(foo foo)

target_link_libraries(foo Qt5::Test)
ignacio
  • 1,181
  • 2
  • 15
  • 28
0

You actually never include the cpp file. If you include the h file you should be able to access the objects that are in the class.

0

When you include a .h file its basically saying copy whats in the .h file and paste it in the cpp. calculator.h is included in calculator.cpp so when the program compiles it will create binary file with both cpp and .h together. cpp files get compiled and linked together .h files dont since they are already going to be compiled with the .cpp. So in terms of main you just need to include the .h if you include .cpp it would not be efficient . **The reason why you also include the .h in main is so the program will know about the class. I hope that answers your question. To sum things up: Include calculator.h in calculator.cpp and main.

billy
  • 19
  • 1
  • 7