I have a problem with linking my project. I'm trying to create shared library which could be used between three different projects. This library will be used for parsing XML files and general handling with objects from these XML files.
Here is minimal example which is affected. Library project consist of these files
============= Library.cpp =============
#include "library.h"
Library::Library(QString name){
this->name = name;
}
============= Library.h =============
#ifndef LIBRARY_H
#define LIBRARY_H
#include <QString>
class Library
{
public:
Library(QString name);
private:
static QString name;
};
#endif // LIBRARY_H
============= Library.pro =============
QT -= gui
TARGET = Library
TEMPLATE = lib
CONFIG += staticlib
SOURCES += library.cpp
HEADERS += library.h
unix {
target.path = /usr/lib
INSTALLS += target
}
Main application is made by these files.
============= main.c =============
#include <QCoreApplication>
#include "library.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Library lib("MyLib");
return a.exec();
}
============= Application.pro =============
QT += core
QT -= gui
TARGET = Application
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
SRCDIR = $$IN_PWD/../Library
INCLUDEPATH += $$SRCDIR
SRCDIR = $$IN_PWD/../Library
INCLUDEPATH += $$SRCDIR
LIBDIR = $$IN_PWD/../build-Library-Desktop_Qt_5_5_1_GCC_64bit-Ladění/libLibrary.a
LIBS += $$LIBDIR
Here is the output of linker
g++ -c -pipe -g -Wall -W -D_REENTRANT -fPIC -DQT_CORE_LIB -I../Application -I. -I../Library -I../Library -I../../../Qt/5.5/gcc_64/include -I../../../Qt/5.5/gcc_64/include/QtCore -I. -I../../../Qt/5.5/gcc_64/mkspecs/linux-g++ -o main.o ../Application/main.cpp
g++ -Wl,-rpath,/home/mint/Qt/5.5/gcc_64 -Wl,-rpath,/home/mint/Qt/5.5/gcc_64/lib -o Application main.o /home/mint/Development/test/Application/../build-Library-Desktop_Qt_5_5_1_GCC_64bit-Ladění/libLibrary.a -L/home/mint/Qt/5.5/gcc_64/lib -lQt5Core -lpthread
/home/mint/Development/test/Application/../build-Library-Desktop_Qt_5_5_1_GCC_64bit-Ladění/libLibrary.a(library.o): In function `Library::Library(QString)':
Makefile:214: recipe for target 'Application' failed
/home/mint/Development/test/build-Library-Desktop_Qt_5_5_1_GCC_64bit-Ladění/../Library/library.cpp:6: undefined reference to `Library::name'
collect2: error: ld returned 1 exit status
When I change static QString name in Library.h to non static variable, then everything is ok. It could be linked then. I think, that project files are setup correctly, but what am I missing ?