I started to work for my homework using QT creator to make the GUI, but I gt this error and I can't manage to find the reason for it, nor can I understand what it means. I suppose it sees my main function twice but I do not know why... please assist me in fixing this error:
error:
Makefile.Debug:155: warning: overriding commands for target `debug/main.o'
Makefile.Debug:142: warning: ignoring old commands for target `debug/main.o'
debug/main.o: In function `Z5qMainiPPc':
D:\c++\Labs\GUI_r/../../../info/qt/Desktop/Qt/4.8.1/mingw/include/QtGui/qwidget.h:494: multiple definition of `qMain(int, char**)'
debug/main.o:D:\c++\Labs\GUI_r/main.cpp:7: first defined here
collect2: ld returned 1 exit status
Code:
#include <QtGui/QApplication>
#include "mainwindow.h"
#include "controller.h"
#include "StudentRepository.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
StudentRepository *stre = new StudentRepository();
Controller *c = new Controller(stre);
MainWindow w(c);
w.show();
return a.exec();
}
edit: long code removed - not the reason for the error. Check the answere it is useful.
The reason for that linking error is because of awkawrd behaivior behalf QT creator. I had in the projectName.pro -
QT += core gui
TARGET = GUI_r
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
main.cpp \ /////// Double call of main.cpp
StudentRepository.cpp \
controller.cpp
HEADERS += mainwindow.h \
controller.h \
StudentRepository.h \
Student.h \
ui_mainwindow.h \ /////Double call of ui_mainwindow.h
ui_mainwindow.h
FORMS += mainwindow.ui
Thank you, i hope this post will be usefull to other new users of QTcreator.
Maybe your project contains another source file with a main. Somewhere files duplicated. Check "SOURCES =" and main.cpp in your .pro file.
You can only have one QApplication per program!
Review your classes (Controller, StudentRepository, MainWindow) and make sure that they are not declaring QApplication as well.
It does see two definitions of qMain , not your main.
You have probably taken a sample program and modified it by adding your code. Recreate those steps and see when it stopped working.
When writing a code, do a compilation as often as possible, to find such errors right after you've introduced them.
Related
I'm following Qt's Qt for Beginners tutorial and for some reason I am getting a compilation error when trying to build the simplest example of a gui. It looks like it's failing on button creation because it thinks that qcolor is unimplemented(?). I just downloaded the latest Qt 5.14.0 and installed it today, so it's possible that something happened during the install?
This is what my project layout looks like:
build-new_qt_project-Desktop_Qt_5_14_0_GCC_64bit-Debug:
Makefile
new_qt_project:
main.cpp
new_qt_project.pro
main.cpp:
#include <QApplication>
#include <QPushButton>
int main(int argc, char **argv)
{
QApplication app (argc, argv);
QPushButton button ("Hello world !");
button.show();
return app.exec();
}
new_qt_project.pro:
TEMPLATE = app
TARGET = new_qt_project
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += main.cpp
And this is the output from trying to make it:
In file included from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qpixmap.h:45:0,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qicon.h:46,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/qabstractbutton.h:44,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/qpushbutton.h:44,
from ../../Qt5.14.0_2/5.14.0/gcc_64/include/QtWidgets/QPushButton:1,
from ../new_qt_project/main.cpp:2:
../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qcolor.h: In constructor ‘constexpr QColor::QColor(int, int, int, int)’:
../../Qt5.14.0_2/5.14.0/gcc_64/include/QtGui/qcolor.h:79:18: sorry, unimplemented: use of the value of the object being constructed in a constant expression
0) {}
^
make: *** [main.o] Error 1
I'm using the default (detected) kit to build it and it has the C++ compiler pointing to the one in /usr/bin, so I think that is correct. Is there something additional I've missed?
I think this is the problem:
Qt bug
update your gcc. up to 4.9 !!!
i had this problem and i used gcc 4.8 and after update it to gcc-7 it is now correct
For pure educational purposes, I'd like to learn how to create my own QT Makefile from scratch.
I've used QMake successfully, but I still would like to learn how to make an extremely simple Makefile to run a basic QT application.
Here is my code that I want to compile with the Makefile:
#include <QApplication>
#include <QMainWindow>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow m;
m.show();
return a.exec();
}
I don't have any special requirements and do not need any special native packaging.
Yes! That was a heck of a learning experience. :)
Here is the minimal Makefile I created.
# In the first two lines, I define the source (.cpp) file and the desired output file to be created (./hello)
SOURCE = hello.cpp
OUTPUT = hello
# This variable is used as a quick reference to the QT install path. This is my custom install directory on Mac OS X.
QT_INSTALL_PATH = /usr/local/qt/5.9.1/clang_64
# This is where you would provide the proper includes depending on your project's needs
INCLUDES = -I$(QT_INSTALL_PATH)/lib/QtWidgets.framework/Headers
#This is the RPATH. To be honest, I am not completely sure of what it does, but it is neccesary.
RPATH = -Wl,-rpath,$(QT_INSTALL_PATH)/lib
#Here is where you link your frameworks
FRAMEWORKS = -F$(QT_INSTALL_PATH)/lib -framework QtCore -framework QtWidgets
# The main action that happens when you type "make"
all:
# We run the g++ command and make sure we use c++11 with the second argument. Then after that, we simply plugin in our previous variables.
g++ -std=c++11 $(SOURCE) $(INCLUDES) $(FRAMEWORKS) $(RPATH) -o $(OUTPUT)
And in case you are wondering, here is my hello.cpp file. It's a very basic program that only imports QtWidgets.
#include <QtWidgets>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;
window.resize(320, 240);
window.show();
window.setWindowTitle(QApplication::translate("toplevel", "Welcome to QT!"));
return app.exec();
}
This was a lot of fun and I definitely learned a lot about how QT programs are compiled. However, in the future I definitely plan on sticking to qmake, as it's a really great tool.
I'm currently learning how to use Qt. I want to try out some simple image processing applications using Qt, and since I'm already familiar with CImg I want to use that. I guess it should be possible to do so, if not mark my question for deletion or something.
My question is: how to get CImg working for Qt? CImg is a header file. Lets say its located on my desktop. I import it using Qt creator 4.1.0, by using the "add existing file..." in the rightclick menu on the header folder. Then my menu looks like this:
.
It compiles when I add #include "CImg.h", but I can't use it, even when I'm trying to type using namespace cimg_library it will tell me that cimg_library doesn't exist. I also tried just creating a header file and copying the content of the CImg.h into it but then it simply fails to compile and the Qt Creator freezes.
Edit: I managed to make the situation a bit better by adding the header location to the include code (like this: #include "C:/Users/Marci/Desktop/CImg.h" )I can now "see" CImg related stuff in the dev environment, and it won't bother me with not finding the constructor for CImg or anything like that. However when I try to compile while using anything CImg related it will give me around 20 linker errors. (Error code: LNK2019) My .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
C:/Users/Marci/Desktop/CImg.h
LIBS += -C:/Users/Marci/Desktop/ -CImg.h
FORMS += mainwindow.ui
Edit2: after implementing the changes that PeterT suggested in his comment my .pro file looks like this:
#-------------------------------------------------
#
# Project created by QtCreator 2016-11-08T17:08:58
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = grayscale
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h \
INCLUDEPATH += C:/Users/Marci/Desktop
FORMS += mainwindow.ui
And my mainwindow.cpp (in which i'm trying to create a CImg object) looks like this:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <CImg.h>
using namespace cimg_library;
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
CImg<unsigned char> myimage(100,200);
}
MainWindow::~MainWindow()
{
delete ui;
}
The compiler errors i get are: error: C2871: 'cimg_library': a namespace with this name does not exist
error: C2065: 'CImg': undeclared identifier
error: C2062: type 'unsigned char' unexpected
I hope this is specific enough.
After I few months I figured it out. The problem lies in the fact, that CImg uses a windows .dll file for the visualizing functions of the class cimg_display. Since Qt is platform independent it doesnt like this. However you can make it work with an interesting trick. First you need to include the header file normally, in the project file. After that, whenever you actually #include it, you need to write the following macro:
#define cimg_display 0
In my understanding this makes it work, because the C and C++ compilers simply copy the content of the included file into the source. And thanks to the macro the compiler will ignore the class thats causing trouble for us.
This is the sample code I am trying execute in Qt Creator. I have included all the JX libraries in Qt. The JX c++ code works outside Qt well but I want to make it work inside Qt. Please review the code and suggest me a way to achieve this.
Currently I am getting errors such as "undefined reference to `v8::Locker::Locker(v8::Isolate*)'" and 1000 similar errors. I gues its because qt is not recognizing the JX libraries.
Thanks in advance.
#include "mainwindow.h"
#include <QApplication>
#include "jx.h"
#if defined(_MSC_VER)
// Sleep time for Windows is 1 ms while it's 1 ns for POSIX
// Beware using this for your app. This is just to give a
// basic idea on usage
#include <windows.h>
#else
#include <unistd.h>
#define Sleep(x) usleep(x)
#endif
void callback(JXValue *results, int argc) {
// do nothing
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
const char *contents =
"process.requireGlobal = require;";
JX_Initialize(argv[0], callback);
JX_InitializeNewEngine();
JX_DefineMainFile(contents);
JX_StartEngine();
JXValue ret_val;
JX_Evaluate("process.requireGlobal('./dummy.js').data",
"eval",&ret_val);
while (JX_LoopOnce() != 0) usleep(1);
JX_Free(&ret_val);
JX_StopEngine();
return a.exec();
}
Yes, the 'undefined reference to' is a linker error.
This is due to the fact that the linker can't find the library (libraries) in the paths it already has, so you need to specified it (them) and you've to do it in the .pro file of your Qt project.
.pro file works similarly to makefile: here you have to specify "extra" paths where source and header files are located, as well as libraries.
For your specific question, you need to add all the library requesting to make jxcore working, adding them to the keyword "LIBS": -L introduce path(s) at which libraries are stored, while -l introduce libraries themselves you want to use. I.e.
LIBS += -L/path/to/your/libraries \
-lname_of_library_without_lib
You can find more detailed description at this link http://doc.qt.io/qt-4.8/qmake-project-files.html
Go directly to last section if you're just interested in how add libraries
Hope it helps
I'm trying to get the libao library working in Qt. Here's what I have so far.
#include <ao/ao.h>
...
static int audio_driver;
static ao_device *audio_device;
static ao_sample_format audio_format;
...
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
ao_initialize();
audio_driver = ao_default_driver_id();
MainWindow w;
w.show();
return a.exec();
}
It says that every reference to anything in the ao library is an undefined reference.
error: undefined reference to `ao_initialize'
error: undefined reference to `ao_default_driver_id'
And so on all the way through the code.
For what it's worth, every function in ao/ao.h is in an extern "C".
Any idea what's causing this?
Many thanks.
You doesn't link against ao dynamic library.
If you use qmake add following lines in .pro file
LIBS += -lao
If library in non-standard location, add these lines too
INCLUDEPATH += path/to/headers
LIBPATH += path/to/library
If you're on Linux, or anywhere else where pkg-config is available, then the way this should be done is by adding "link_pkgconfig" to the CONFIG variable, and then add the package name to the PKGCONFIG variable. For example, if you're using libao and libvorbisfile:
CONFIG += link_pkgconfig
PKGCONFIG += ao vorbisfile
This will make sure that not only the correct link flags will be used, but also the correct CFLAGS/CXXFLAGS, which is also important.