How to include Winpcap to Qt creator? - c++

I tried to use different combination in .pro file but always get these errors :
error: C2065: 'PCAP_SRC_IF_STRING' : undeclared identifier
error: C3861: 'pcap_findalldevs_ex': identifier not found
Here is my .pro file :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = snif_del
TEMPLATE = app
LIBS += WS2_32.lib
INCLUDEPATH += D:/libs/WpdPack/Include
LIBS += -L D:/libs/WpdPack/Lib/ -lwpcap -lpacket
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
Also i added #include "pcap.h" to my code.

Your .pro file should look like this:
INCLUDEPATH += D:/libs/WpdPack/Include
LIBS += "-LD:/libs/WpdPack/Lib" -lwpcap -lws2_32
If your program uses Win32 specific functions of WinPcap, add
DEFINES += WPCAP
If your program uses the remote capture capabilities of WinPcap, add
DEFINES += HAVE_REMOTE
In your code you should add
#include <winsock2.h>
#include <pcap.h>

Related

Qt Creator : Adding an external library (header file not found)

This is my external library location in my Qt project (named contr):
This is my makefile :
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = contr
TEMPLATE = app
INCLUDEPATH += 3rdparty/libs/libusb
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
LIBS += -L"3rdparty/libs/libusb" -llibusb
In a file, I have this include :
#include <libusb.h>
When I compile, I get the following error :
mainwindow.cpp:3: erreur : C1083: Cannot open include file: 'libusb.h': No such file or directory
EDIT :
I work on Windows

LNK2001: Unresolved external symbol when using qwt plot in Qt creator

I am subclassing QwtPlot. I have the following error:
moc_myplot.obj:-1: error: LNK2001: unresolved external symbol "public: static struct QMetaObject const QwtPlot::staticMetaObject" (?staticMetaObject#QwtPlot##2UQMetaObject##B)
I have tried to the following things: run qmake, rebuild, clean, delete debug folder, recompile qwt library. That doesn't help. Here is minimal code:
myplot.h:
#ifndef MYPLOT_H
#define MYPLOT_H
#include <QObject>
#include <qwt_plot.h>
class MyPlot : public QwtPlot
{
Q_OBJECT
public:
MyPlot();
};
#endif // MYPLOT_H
myplot.cpp:
#include "myplot.h"
MyPlot::MyPlot()
{
}
Here is .pro file:
#-------------------------------------------------
#
# Project created by QtCreator 2015-06-22T19:33:24
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MyPlot
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
myplot.cpp
HEADERS += mainwindow.h \
myplot.h
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../../../../../qwt-6.1.2/lib/ -lqwt
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../../../../../qwt-6.1.2/lib/ -lqwtd
else:unix: LIBS += -L$$PWD/../../../../../qwt-6.1.2/lib/ -lqwt
INCLUDEPATH += $$PWD/../../../../../qwt-6.1.2/include
DEPENDPATH += $$PWD/../../../../../qwt-6.1.2/include
I am using Qt Creator 3.4.1 Based on Qt 5.4.2 (MSVC 2013, 32 bit). Kit: Desktop Qt 5.4.2 MSVC2013 64bit. Compiler: Microsof Visual C++ Compiler 12.0(amd64). If I comment Q_OBJECT macro in myplot.h everything is ok. I can use qwt_plot without subclassing, so that this->setCentralWidget(new QwtPlot()); line in mainwindow.cpp is ok.
It seems this is an old issue that was present in at least version 4.6.
The workaround is basically a preprocessor define of QWT_DLL from the "very lowest library that calls QWT".

undefined reference to own library function

I'm using QT(5)-Creator with 2 projects, that compile both with MinGW my C++ programs under Windows 7. One project generates a static library, and the other is just there to verify that the library is alright.
The error:
When building the library no errors are generated and it creates successfully the static lib MyClassName.a.
In turn, when trying to import the library, I get error messages durring building/compilation:
undefined reference to 'MyClassName::MyClassName()'
undefined reference to 'MyClassName::MyClassFunction()'
The files:
here's the main.cpp of my importer project
#include <QCoreApplication>
#include "MyClassName.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
MyClassName *mainThread = new MyClassName();
mainThread->MyClassFunction();
return a.exec();
}
and it's .pro file
QT += core
QT += widgets
QT -= gui
TARGET = TESTerase
CONFIG += console
CONFIG -= app_bundle
CONFIG +=extserialport
CONFIG +=staticlib
TEMPLATE = app
HEADERS += \
MyClassName.h
SOURCES += main.cpp
unix:!macx: LIBS += -L$$PWD/../MyClassName-Release/ -lMyClassName
INCLUDEPATH += $$PWD
INCLUDEPATH += $$PWD/../MyClassName-Release/release
DEPENDPATH += $$PWD/../MyClassName-Release/release
unix:!macx: PRE_TARGETDEPS += $$PWD/../MyClassName-Release/release/MyClassName.a
btw I've used QT-Creator's include external library function
My guesses:
Since I'm able to [Strg+Click] the functions in the main, I assume the error is thrown by the Linker.
It may also be a manner of importing/creating/using libraries too, since I just started with that topic.
Anyways searching the Internet about it usually just results in the hint, to use the same compiler for both projects - which shouldn't matter in my case (since I'm building both projects with the same Creator and the same settings).
Has anybody an idea?
Thanks for your support
Kind Regards
[Update for Bogdans request]
the new .pro file
#-------------------------------------------------
#
# Project created by QtCreator 2015-04-29T19:46:22
#
#-------------------------------------------------
QT += core
QT += widgets
QT -= gui
TARGET = TESTerase
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG +=extserialport
HEADERS += \
MyClassName.h
SOURCES += main.cpp
unix:!macx: LIBS += -L$$PWD/../MyClassName-Release/ -lMyClassName
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/../MyClassName-Release/release/ -lMyClassName
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/../MyClassName-Release/debug/ -lMyClassName
else:unix:!macx: LIBS += -L$$PWD/../MyClassName-Release/ -lMyClassName
INCLUDEPATH += $$PWD/../MyClassName-Release/release
DEPENDPATH += $$PWD/../MyClassName-Release/release
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/release/MyClassName.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/debug/MyClassName.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/release/MyClassName.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/../MyClassName-Release/debug/MyClassName.lib
else:unix:!macx: PRE_TARGETDEPS += $$PWD/../MyClassName-Release/MyClassName.a
Your .pro file defines library dependencies only for unix platform unix:!macx:. I suppose there should be also win32 for Windows.
Had the same problem just now. Build was working fine on macOS/OSX but not on Windows with the same codebase.
It was caused by the fact I was not exporting my symbols, which is necessary on Windows.
Use Q_DECL_EXPORT on functions when building the lib.
And use Q_DECL_IMPORT when using the functions from the lib.
Example how to do this here: https://wiki.qt.io/How_to_create_a_library_with_Qt_and_use_it_in_an_application#Creating_a_shared_library
Solved the problem in my case, hope it helps!

How to use FTD2xx library on QtCreator (with MSVC2012 compiler)

I have trouble in making an application which communicates FTDI devices via D2xx driver.There is some example project for Visual Studio in ftdi's website. But there isn't for QtCreator. I chose QtCreator as c++ IDE because my application needs Qt framework.
I put ftd2xx.h and ftd2xx.lib in the Qt project directory.
(ftd2xx.lib is static library, which were originally located in [download-folder]\CDM v2.08.30 for Windows 8.1\Static\i386)
When sample source code was compiled, I got following error message.
Error message when I use .pro file(1).
LNK1104: cannot open file 'ftd2xx.lib'
Error message when I use .pro file(2).
error: Assignment needs exactly one word on the left hand side.
How should I correct Qt project setup in order to utilizes ftd2xx library?
My source code and .pro file is here.
[main.cpp]
#include <QCoreApplication>
#include <windows.h>
#include "ftd2xx.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
FT_HANDLE ftHandle;
FT_STATUS ftStatus;
ftStatus = FT_Open(0,&ftHandle);
return a.exec();
}
[.pro file if the checkbox is enabled] ---(1)
#-------------------------------------------------
#
# Project created by QtCreator 2014-02-11T18:51:08
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = D2XXtest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += ftd2xx.h
win32:CONFIG(release, debug|release): LIBS += -L$$PWD/ -lftd2xx
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/ -lftd2xxd
INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/libftd2xx.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/libftd2xxd.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$PWD/ftd2xx.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$PWD/ftd2xxd.lib
[.pro file if the checkbox is NOT enabled] ---(2)
#-------------------------------------------------
#
# Project created by QtCreator 2014-02-11T18:51:08
#
#-------------------------------------------------
QT += core
QT -= gui
TARGET = D2XXtest
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
HEADERS += ftd2xx.h
win32: LIBS += -L$$PWD/ -lftd2xx
INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/
win32:!win32-g++ PRE_TARGETDEPS += $$PWD/ftd2xx.lib
else:win32-g++: PRE_TARGETDEPS += $$PWD/libftd2xx.a
Any help would be appreciated. Thanks in advance.
[Note]
OS: Windows 8.1
IDE: Qt Creator 3.0.0
Compiler: MSVC2012(32bit)
Target device: ft232hl
[Additional Note]
The configuration of "Add Library" is following.
If I enable the checkbox "Add "d" suffix for debug version, then I got error message about LNK1104. Otherwise, I got different error message.
My hunch is that you've made a typo in your error message, and the real error message reads as follows:
LNK1104: cannot open file 'ftd2xxd.lib'
See that d there? It doesn't belong, there's no ftd2xxd.lib. You need to change any references to ftd2xxd.lib back to ftd2xx.lib, re-run qmake, and you should be all set. Otherwise your .pro file seems correct.
After your edit, you gave a .pro file with the "checkbox enabled". That's certainly not true, since the contents you show imply that the checkbox is disabled, not enabled.
Anyway, we don't care what the dialog box settings are. Just show the actual .pro file that you really use, because so far I don't know which one you do use, and I don't know if you've re-run qmake after the .pro file was changed.
Are you sure that the ftd2xx.lib file is actually in the same folder where the .pro file is?

How do you include CGAL into a QT project in QT Creator?

I am trying to run some of the sample example code from CGAL as a Qt project, to run in Qt Creator. I expect that I will have to modify the .pro file. How should it be modified to use CGAL libraries?
I'm not familiar with CGAL specifically, but in general, you would need to add the following to your .pro file:
INCLUDEPATH += /path/to/cgal/headers
LIBS += -Lpath/to/cgal/libraries -lcgal_dll_name
You may also need to add some DEFINES if CGAL requires it, i.e.
DEFINES += SOME_MACRO_THAT_CGAL_REQUIRES
If you were looking for help on CGAL specifically, please clarify your question and I will delete this answer.
Although this is an old question, just for the sake of having a more complete answer, this is what I had to do in the .pro file:
INCLUDEPATH += /usr/include/
LIBS += -L/usr/include/
LIBS += -lCGAL
LIBS += -lgmp
LIBS += -lmpfr // not really needed for me, but added since gmp had to be added too
QMAKE_CXXFLAGS += -frounding-math -O3
Do NOT add stuff like the following, it will get you into trouble with weird error messages, as discussed in this link.
INCLUDEPATH += /usr/include/CGAL # do NOT add this!
LIBS += -L/usr/include/CGAL # do NOT add this!
I'm using Qt 4.8.6, gcc and Fedora 24, and here is my .pro for Qt-CGAL projects :
#-------------------------------------------------
#
# Project created by QtCreator 2017-01-08T14:50:29
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = My CGAL_test
TEMPLATE = app
LIBS += -lgmp -lmpfr -lCGAL
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui