I have very basic QT application (just create to explain my problem).
So here I go :) I have two folders, f1 and f2, and they are on same level (have same folder for parent). In f1 I have source code from my project, and in f2 another project.
For sake of this example, let's say that in f1 I have only common.h and common.cpp, and in f2 I have:
Test.pro
SOURCES = main.cpp
INCLUDEPATH += "..//f1//"
main.cpp
#include <common.h>
#include <QDebug>
int main(int argc, char *argv[])
{
qDebug()<<CalculateMD5("test");
}
When I try to build this project (Test.pro) I get following error:
f2/main.cpp:7: undefined reference to `CalculateMD5(QString)'
What am I doing wrong ? How should I include code from another project ?
I need CalculateMD5 function to be global.
Here you can download whole example (1kb):
http://www.xx77abs.com/test.rar
Thanks !!
For the simplest change, I think you need to change your .pro file to this:
Test.pro
SOURCES = main.cpp ../f1/common.cpp
INCLUDEPATH += "..//f1//"
If you have other code that will also be linking in common.cpp, then a better change would be to make a .pro file in f1 that generates a library, to be linked in to other applications.
But just manually adding common.cpp to your list of sources should do the trick.
Related
I was trying to centralize a variable from .pro file into my project by trying to define it and print in another .qml file
.pro file
VERSION=2.4
DEFINE APPVERSION=VERSION;
.qml file
text: qsTr(APPVERSION)
this seems to give me the above subjective error. Must there be any other way.
DEFINE works for C++, it is not for qml so a possible solution is to convert the DEFINE to string and then export it to qml using setContextProperty:
*.pro
VERSION = 2.4
DEFINES += APPVERSION=$$VERSION
main.cpp
// ...
#define QUOTE_(x) #x
#define QUOTE(x) QUOTE_(x)
// ...
QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("APPVERSION", QUOTE(APPVERSION));
// ...
*.qml
text: APPVERSION
I have just started using QtCreator (and C++ after 15 years away from it). I want to do some linear algebra stuff so I have included the library Eigen in my project file as follows (I have placed the Eigen library directly in my project for the moment):
INCLUDEPATH = "/home/Software/QtProjects/MyProject/eigen/"
My source file is:
#include <QCoreApplication>
#include <iostream>
#include <Eigen/Dense> % 'Eigen/Dense' file not found
using Eigen::MatrixXd; % use of undeclared identifier 'Eigen'
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MatrixXd m(2,2); % unknown type name 'MatrixXd'
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = 8;
m(1,1) = m(1,0) + m(0,1);
std::cout << m << std::endl;
return a.exec();
}
This code works and outputs the values of the matrix m in the console. However, in the QtCreator IDE, I have several error messages which I have included above as comments on the lines at which they appear.
So is there some way to fix this and make QtCreator stop showing these lines as errors when the code is actually working fine?
Try without quotation marks; and with += instead of =; INCLUDEPATH += /home/Software/QtProjects/MyProject/eigen/.
Go to Build > Run qmake to validate changes in the pro file.
INCLUDEPATH in qmake project file doesn't work
Qt creator Adding external library
How to add include path in Qt Creator?
On Linux, run
sudo apt-get install libeigen3-dev
Then add this to your .pro file
INCLUDEPATH += /usr/include/eigen3
And then run qmake
I am using the nlohmann json (https://github.com/nlohmann/json) library (1 single json.h file) with Qt. I realize Qt has it's own json functionality, so assume that any third party header may be used. Qt generates a whole lot of issues/warnings and I would like to try suppress issues/warnings for just the json.h file.
[EDIT] Interestingly the json third party library has been updated from v3.3.0 to v3.4.0 and all warnings gone (probably best outcome). Nonetheless, being able to suppress third party warnings would still be great.
I tried the suggestions here, by putting the .h in a sub directory as well as adding the sub directory to the INCLUDEPATH. $$PWD will apparently give the current directory of the .pro file. But there is no change after doing this.
INCLUDEPATH += $$PWD/thirdparty
QMAKE_CXXFLAGS += -isystem $$PWD/thirdparty/
The warnings are easy to reproduce by the simple hello world example below:
#include <QCoreApplication>
#include <QDebug>
#include "json.h"
using namespace nlohmann;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug() << "hello world";
return a.exec();
}
[EDIT]
Still hoping for some help. From the comments I have tried wrapping the inclusion, which didn't work, with what is seen below:
#pragma warning(push, 0)
#include "json.h"
#pragma warning(pop)
I am using qmake but thought that maybe gcc solutions might apply, seems they don't.
Any help would be greatly appreciated.
I'm struggling to get a subclass of QGLWidget compiled that uses glew. I'm running Qt 5.0.1 + MinGW 4.7 on Windows 7. Glew 1.7.0 has been compiled with said MinGW.
I have subclassed QGLWidget with UGGLWidget and included "GL/glew.h" before the QGLWidget includes. When compiling I get 200 Errors about:
'function' redeclared as different kind of symbol
conflicting declaration 'typedef'
So my Question:
How do I get a subclass of QGLWidget that uses glew compiled under Qt5?
Edit: I tried the same approach with another setup (Qt4.7 + MinGW4.4). It compiles and runs just fine. I have also tried using a more recent version of glew (Qt5.0.1 + MinGW4.7 + glew1.9.0) this doesn't compile either and gives the same compilation errors.
Here's a messy snippet of the compiler output:
> In file included from
> ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include/QtGui/qopengl.h:55:0,
> from ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtOpenGL/qgl.h:47,
> from ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include\QtOpenGL/QGLWidget:1,
> from src\UGGLWidget.h:4,
> from src\UGGLWidget.cpp:2: ..\..\..\..\..\..\Qt\Qt5.0.1\5.0.1\mingw47_32\include/GLES2/gl2.h:614:153:
> error: 'void __glewVertexAttribPointer(GLuint, GLint, GLenum,
> GLboolean, GLsizei, const GLvoid*)' redeclared as different kind of
> symbol In file included from src\UGGLWidget.cpp:1:0:
> ..\3rdPartyLibs\glew\include/GL/glew.h:13609:46: error: previous
> declaration of 'void (__attribute__((__stdcall__)) *
> __glewVertexAttribPointer)(GLuint, GLint, GLenum, GLboolean, GLsizei, const GLvoid*)'
Last but not least the header and source file of the subclass. It's really just empty for now.
Source:
//UGGLWidget.cpp
#include "GL/glew.h"
#include "UGGLWidget.h"
UGGLWidget::UGGLWidget(QWidget *parent) : QGLWidget(parent) { ... }
Header:
//UGGLWidget.h
#ifndef UGGLWIDGET_H
#define UGGLWIDGET_H
#include <QGLWidget>
class UGGLWidget : public QGLWidget
{
Q_OBJECT
public:
explicit UGGLWidget(QWidget *parent = 0);
};
#endif // UGGLWIDGET_H
Oh, and maybe relevant parts from the .pro file
QT += core gui opengl
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
LIBS += -L$$quote(../3rdPartyLibs/glew/lib/) -lglew32
LIBS += -lopengl32 -lglu32
INCLUDEPATH += $$quote(../3rdPartyLibs/glew/include)
The following fixed my problem, this goes everywhere, where I include glew:
#include "GL/glew.h"
#define QT_NO_OPENGL_ES_2
Another option: building Qt5.0.1 from source with -opengl desktop during the configure step.
For some reason QT_OPENGL_ES_2 is defined in my Qt5.0.1 installation. Looking through the inlcudes from QGLWidget.h I found the following in QtCore/qconfig.h
#if defined(QT_OPENGL_ES_2) && defined(QT_NO_OPENGL_ES_2)
# undef QT_OPENGL_ES_2
#elif !defined(QT_OPENGL_ES_2)
# define QT_OPENGL_ES_2
#endif
This leads to inclusion of GLES2/gl2.h which doesn't get along with glew. Knowing that the issue was related to GLES2 I came across a bug report and a QtProject post.
If I understand correctly, then the provided QtSDK installer for Qt5.0.1 has been built such that GLES is enabled.
Have a good day,
~Moritz
Note that QT 4.8RC release notes say:
Including QtOpenGL will not work in combination with GLEW, as QGLFunctions will undefine GLEW's defines.
I am using Qt 4.5 in Windows XP. My pro file has the variable VERSION = 1.0. Now i need to read this variable and get its value (1.0) from the source code. So that I don't have to maintain another variable for version inside my source code. I just read from the pro file and update it. So that the value remains consistent all over my project. Is it possible? Any pointers regarding this are welcome..
Use somethings like this:
DEFINES += VERSION=\\\"$$VERSION\\\"
This will define a macro that you can use in C source code. Get rid of the backslashes and quotes if you want a number, not a string.
I'll elaborate on this a bit.
In the YourApp.pro:
VERSION = 0.0.0.1
DEFINES += APP_VERSION=\\\"$$VERSION\\\"
In the main.cpp:
#include <QApplication>
QCoreApplication::setApplicationVersion(QString(APP_VERSION));
Wherever else in your sources, e.g. in the imaginary controller.cpp:
#include <QApplication>
QString yourAppVersion = QCoreApplication::applicationVersion();
somehow, when I tried qDebug() << QString(APP_VERSION); in a class.cpp not in main.cpp. has an error "C2065" APP_VERSION: undeclared identifier". but when I tried in main.cpp that worked.