include compiled through command prompt static library in qt project - qt

I’m trying to make a simple static library, like:
my.h
void My();
my.cpp
#include "my.h"
#include "stdio.h"
void My()
{
printf("Hello, world");
}
If I made it in qtcreator, I can call My() function in other project like:
.pro
INCLUDEPATH += c:/Users/PC/my
LIBS += -Lc:/Users/PC/my/debug -lmy
.cpp
#include <my.h>
....
My();
When compiling the library, the makefile is created by qmake. If I try to compile as stated in makefile through command prompt, I get some other object and library files. Then if I include them into my project, I obtain error:
undefined reference to `My()'.
How to link static library through command prompt and then include it into project?

Related

Error while compiling with qmake, qt?

I tried to write a simple qt application and I get this error when I try to compile it
#include <QApplication>
#include <QTextEdit>
int main(int argv, char **args)
{
QApplication app(argv, args);
QTextEdit textEdit;
textEdit.show();
return app.exec();
}
_
qmake file.cpp
/home/j/qtf.cpp:4: Extra characters after test expression.
/home/j/qtf.cpp:6: Extra characters after test expression.
/home/j/qtf.cpp:8: Extra characters after test expression.
/home/j/qtf.cpp:11: Extra characters after test expression.
Where is the error
In order to compile your application you have to:
Create a project file,
Generate makefiles,
Build the project.
To perform these steps simply invoke the corresponding commands:
qmake -project (generates project file)
qmake (generates makefile)
make (build the project)

Qt linker errors Qmake + Makefile

I want to build a Qt project outside Qt Creator, so I'm using qmake to generate a Makefile on the following project file:
TEMPLATE = app
TARGET = test
INCLUDEPATH += . \
/usr/include/qt5/QtWidgets/
QMAKE_CXXFLAGS += --std=c++11
# Input
SOURCES += test.cc
Which was generated also by qmake, bar the c++11 flag and the second include path. The Makefile contains link paths to the Qt library
LIBS = $(SUBLIBS) -L/usr/X11R6/lib64 -lQt5Gui -L/usr/lib/x86_64-linux-gnu -lQt5Core -lG L -lpthread
What's weird in the above is that I don't have a /usr/X11R6 folder. Instead, libQt5Gui.so is located in /usr/lib/x86_64-linux-gnu, so I'm a little puzzled where the X11R6 comes from.
Anyway, this is my linker output:
test.cc:(.text.startup+0x20): undefined reference to `QApplication::QApplication(int&, char**, int)'
test.cc:(.text.startup+0x25): undefined reference to `QApplication::exec()'
test.cc:(.text.startup+0x2f): undefined reference to `QApplication::~QApplication()'
test.cc:(.text.startup+0x43): undefined reference to `QApplication::~QApplication()'
The above is the result of building the following source:
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
return app.exec();
}
When I try to build the project file in Qt Creator, the same error appears. Am I missing libraries? Or is something configured erroneously?
(I'm on Ubuntu 14.04, and I just installed the qtcreator package from the repo's, assuming that all development libraries would be installed along with it.)
As stated in the docs, you need to include the widget library to use QApplication.
Add this to your project file:
QT += widgets
If you're not going to build a GUI app, use QCoreApplication instead. It doesn't have that dependency.

How to create Qt GUI application from dll called by win32 console application?

(I am not native English so Sorry if I make any grammar mistake)
I am new to Qt, started 2 days ago, and had only few experiences of WINAPI and MFC.
I am thinking of...
First, creating Qt GUI application from its dll
Second, my static library file(*.lib) will call this dll file using QLibrary.
Lastly, my console application will have its lib and its header file, and dll to create Qt GUI Application.
and my console application does not have .pro file, just created on visual studio and has only .vcproj and .sln file.
Here is the source code:
http://cfile239.uf.daum.net/attach/037B654151C4FF8D2D0EB7
I copied dll, lib and its header file into its win32 console application.
and when I compile console, I get this error message.
fatal error C1083: Cannot open include file: 'QApplication': No such file or directory
I know it is absolutely right. (cuz my lib uses QLibrary and its lib and header file is included in win32 console application..)
well, actually I do not want to have .pro file including QApplication class into my console application to fix this problem.
Is there any possible way to fix it while avoiding having .pro file?
or should I create .pro and set it to have QT library?
Thank you for reading it. :D
You should indicate your compiler Qt's path.
If you use vs,choose tools->option->project and solutions,VC++ Directories,set your Qt header files path,lib path,bin path, and set Qt's bin path to environment variable PATH.
In your static library project, try this:
create.h
#pragma once
void createQt(int argc, char* argv[]);
create.cpp
#include<QtGui/QWidget>
#include<QtGui/QApplication>
#include "create.h"
#pragma comment(lib,"qtguid4.lib")
void createQt(int argc, char* argv[])
{
QApplication app(argc,argv);
QWidget w;
w.show();
app.exec();
};
in you console program:
.cpp file:
#include "stdafx.h"
#include "create.h"
#pragma comment(lib,"CreaeQt.lib")
int main(int argc, char* argv[])
{
createQt(argc,argv);
return 0;
}

QT ffmpeg Setting

I can't use the ffmpeg in QT.
My Steps :
-1. compile the ffmpeg as normal
-2. add lib/include path in QT .pro
LIBS += -L/home/kim/ffmpeg/lib -lavcodec -lavformat
INCLUDEPATH += /home/kim/ffmpeg/include
-3. in code
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}
and just call a function named av_register_all();
then I got the following bulid issues< many functions undefined reference >
in function `rl2_read_packet`: undefined reference to `av_free_packet`
in function `av_register_all`: ....
I search a solution of the problem but not work for me.
Any other solution? Thanks.
This is an old post and it is almost solved. By the way, I faced to the problem and I had lots of time struggling with
undefined reference to av_register_all
I am using Qt Creator 5.3.2 and this is my MYPROJECT.pro file
MYPROJECT.pro
QT += core
QT -= gui
TARGET = MYPROJECT
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
QMAKE_CXXFLAGS += -D__STDC_CONSTANT_MACROS
LIBS += -LC:\ffmpeg-20151219-git-2dba040-win32-dev\lib
LIBS += -lavcodec -lavformat -lavutil -lswscale
INCLUDEPATH +=C:\ffmpeg-20151219-git-2dba040-win32-dev\include
Setting up project.pro
It is recommended to build ffmpeg from scratch, but if you are on windows and if you are not willing to compile this masterpiece, then congrats! you are like me! Go for pre-built in zereanoe and download both the latest shared and dev win-32 versions. Extract the latter two packages in a place where there is no spaces in the path, i.e. mine is in: C:\ffmpeg-20151219-git-2dba040-win32-dev
I recommend that don't rename the main folder because it is highly informative. Set the include folder of the [PATH TO ffmpg-YYYYMMDD-git-win32-dev]/include to your project's search space by the following line of code:
INCLUDEPATH +=C:\ffmpeg-20151219-git-2dba040-win32-dev\include
You should also use libraries in [PATH TO ffmpeg-YYYYMMDD-git-win32-dev]/lib to your project as below:
LIBS += -LC:\ffmpeg-20151219-git-2dba040-win32-dev\lib
LIBS += -lavcodec -lavformat -lavutil
You can call libraries other than lavcodec, lavformat and lavutil. In your cpp file you should enclose headers corresponding to ffmpeg in extern "C" block.
DLLs should be added
The final step is to add all the dll files in the shared version of ffmpeg that you have recently downloaded.
Copy *.dll files within
[PATH TO ffmpeg-YYYYMMDD-git-2dba040-win32-shared]/bin
and paste them wherever your executable exists. Fo instance, my project is located on C:/QtProjects/MYPROJECT, then my binaries are located on:
C:/QtProjects/build-MYPROJECT-Desktop_Qt_5_3_MinGW_32bit-Debug
Now, copy all the dlls to your project's debug or release folder.
My main.cpp
This is my main.cpp :
main.cpp
#include <iostream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswscale/swscale.h>
}
using namespace std;
int main(int argc, char *argv[])
{
cout<<"I have included FFMPEG in my project"<<endl;
av_register_all();
cout<<"If everything goes well you will see ME"<<endl;
return 0;
}
I had the same problems, it worked using the following trick.
In code cpp source:
#define __STDC_CONSTANT_MACROS
extern "C" {
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
}
And in your .pro file:
LIBS += -LC:/Users/tom/bla/ffmpeg/lib/ -lavcodec -lavformat -lavutil
I think in your case, you need the -lavutil lib parameter.

undefined reference linker error with signals and slots

Using Qt Creator, I am creating a class with custom slots in Qt:
class CustomEdit : public QTextEdit
{
Q_OBJECT
public:
CustomEdit(QWidget* parent);
public slots:
void onTextChanged ();
};
However, I'm getting this linker error:
undefined reference to 'vtable for CustomEdit'
The documentation says:
if you get compiler errors along the lines of "undefined reference to vtable for LcdNumber", you have probably forgotten to run the moc or to include the moc output in the link command.
... but it is not obvious what that means.
Is there something I need to add to my class, or to the .pro file?
EDIT -- Here is my .pro file:
QT += network \
script \
webkit \
xml
TARGET = MyApp
TEMPLATE = app
SOURCES += main.cpp \
MainWindow.cpp \
CustomEdit.cpp
HEADERS += MainWindow.h \
CustomEdit.h
FORMS += mainwindow.ui
EDIT:
This question deals with the same problems, but the answers don't make it clear how I can fix this issue in Qt Creator. Do I need to explicitly add the moc-generated .cpp file to my list of files to be linked? That seems like a pain.
Qt creator is an IDE which is used to create your Qt projects. That's all. But qmake allows you to buid the project. From qmake manual,
qmake automates the generation of Makefiles so that only a few lines of information are needed to create each Makefile. qmake generates a Makefile based on the information in a project file. qmake contains additional features to support development with Qt, automatically including build rules for moc and uic.
You don have to include your moc files separately, but you have to run qmake which contains all the information about the required moc files. Take a look at the generated Makefile.debug or Makefile.release after qmake and you can find all the moc files will be included for you.
Now to run qmake in Qt creator, just go to Build->RebuildAll which will run the qmake and all the (moc) files will be generated and linked (in Makefile.debug) as well.
Hopefully this will elimate your undefined reference.

Resources