Hi guys. I'm new to Qt an C++. I started developing an app on Windows platform Qt 5.1.1 Qt Creator v. 2.8.1 and would like to include some external source code in my project. So to learn how to do it, I wrote a simple c program consisting of 3 files, elab.cpp
#include <iostream>
#include "mat.h"
int sumup(int a, int b) {
return (a + b);
}
int main() {
using namespace std;
int a = precalc(3,4);
cout << "The sum of 3 and 4 is " << a;
int c = sumup(6,2);
cout << "The sum of 6 and 2 is " << c;
return 0;
}
#include <iostream>
#include "mat.h"
int sumup(int a, int b) {
return (a + b);
}
int main() {
using namespace std;
int a = precalc(3,4);
cout << "The sum of 3 and 4 is " << a;
int c = sumup(6,2);
cout << "The sum of 6 and 2 is " << c;
return 0;
}
To copy to clipboard, switch view to plain text mode
mat.h
#ifndef ADD_H
#define ADD_H
int precalc(int x, int y);
#endif
#ifndef ADD_H
#define ADD_H
int precalc(int x, int y);
#endif
To copy to clipboard, switch view to plain text mode
and mat.cpp
int precalc(int x, int y) {
return (x + y);
}
int precalc(int x, int y) {
return (x + y);
}
To copy to clipboard, switch view to plain text mode
If I compile both cpp-s to exe with mingw the program runs ok. Now I wonder how do I include it in my new Qt app. I want for instance invoke my function "precalc" to be precise. So far I tried to alter the pro file by adding lines like
SOURCES += "C:\Users\myuser\Documents\elab_c_proj"
INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
LIBS += "C:\Users\myuser\Documents\elab_c_proj"
HEADERS += "C:\Users\myuser\Documents\elab_c_proj"
SOURCES += "C:\Users\myuser\Documents\elab_c_proj"
INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
LIBS += "C:\Users\myuser\Documents\elab_c_proj"
HEADERS += "C:\Users\myuser\Documents\elab_c_proj"
To copy to clipboard, switch view to plain text mode
I realized that adding just the INCLUDEPATH directive is enough to get access to my function when coding. However the problem arises when I try to build the new qt app, compiler/linker don't know how, tells me somehing like "undefined reference to precalc". Since I'm new to C++ I'm not sure I understand the concept correctly, so my questions are:
1)what am I missing, doing wrong?
2)is it enough to just add the source code (cpp and h files) via INCLUDEPATH and the qmake knows how to build my external source code to final qt app binary staticaly?
3)or must my external app be precompiled first (say outside qt creator) like a lib file so that the qmake knows how to link to it?
4) can I make a kind of dynamic "dll" from my external app? How would I refer to it in this case from qt app?
Much thanks in advance for your help,
best regard, Arcull
Bookmarks