0

I have two projects in qtcreator: BiosPatcher - the code & BiosPatcherTest - googletest unit tests for BiosPatcher code. Advice from here:

multiple main declaration in qtcreator project which uses googletest

How to import sources from one project in another and connect builded project as library in qtcreator to another qtcreator project?

BiosPatcher project has:

BiosPatcher\src\bios\Bios.{cpp, hpp} class

and in BiosPatcherTest i have test:

#include "src/bios/Bios.hpp" //not works
...
TEST(BiosTest, testReadMethodReadsFromBiosIO) {
MockBiosIO mockBiosIO;
EXPECT_CALL(mockBiosIO, readAsBytes())
    .Times(AtLeast(1));

MockReentrantLock mockReentrantLock;
MockBiosVector mockBiosVector;
MockPatch mockPatch;
MockLog mockLog;

Bios bios;
bios.setBiosIO(&mockBiosIO);
bios.setLock(&mockReentrantLock);
bios.setBiosBytesVector(&mockBiosVector);
bios.setLog(&mockLog);
bios.setPatch(&mockPatch);

bios.read();

}

Community
  • 1
  • 1
Vyacheslav
  • 3,134
  • 8
  • 28
  • 42

1 Answers1

0

First, you need create BiosPatcher C++ shared library project. After, you need just add BIOSPATCHER_LIBRARY to class declaration

this

class BiosPatcher
{

need to be replaced by this

class BIOSPATCHERSHARED_EXPORT BiosPatcher
{

and after it just compile your library and include same headers in your second project, and add to linking generated .lib file.

DEPENDPATH''= . ../BiosPatcher
INCLUDEPATH ''= ../BiosPatcher
LIBS''= -L../BiosPatcher/debug -lBiosPatcher

When I created library project in my Qt Creator with name BiosPatcher, IDE automaticly created biospatcher_global.h file, that containts BIOSPATCHERSHARED_EXPORT defination. That file need to be included in all library headers. You see, if defined BIOSPATCHER_LIBRARY then BIOSPATCHERSHARED_EXPORT will be defined as Q_DECL_EXPORT, else Q_DECL_IMPORT. BIOSPATCHER_LIBRARY is defined in library .pro file, that means if you include those headers from another project, class will be imported from compiled library.

#ifndef BIOSPATCHER_GLOBAL_H
#define BIOSPATCHER_GLOBAL_H

#include <QtCore/qglobal.h>

#if defined(BIOSPATCHER_LIBRARY)
#  define BIOSPATCHERSHARED_EXPORT Q_DECL_EXPORT
#else
#  define BIOSPATCHERSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // BIOSPATCHER_GLOBAL_H

Also read this: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application

Inline
  • 2,566
  • 1
  • 16
  • 32