Using Qt Creator to develop an Apache2 module - qt

I'm developing an Apache2 module as a subproject of a larger project. The source file is (for example) module_example.c. Apache2 modules are compiled with apxs2, thus:
% apxs2 -c module_example.c
I've successfully added rules (patterned on how ODB works) to my Qt Creator .pro file to find and use apxs2, like this:
APXS_FLAGS =
APXS_FILES += module_example.c
for(dir, APXS_FILES) {
APXS_PWD_FILES += $$PWD/$${dir}
}
apxs.name = apxs2 -c ${QMAKE_FILE_IN}
apxs.input = APXS_PWD_FILES
apxs.output = ${QMAKE_FILE_BASE}.so
apxs.commands = apxs2 $$APXS_FLAGS -c ${QMAKE_FILE_IN}
apxs.depends = $$APXS_PWD_FILES
apxs.clean = module_${QMAKE_FILE_BASE}.so
QMAKE_EXTRA_COMPILERS += apxs
QtCreator correctly compiles the module using apxs2 (although it leaves the binaries in the source directory instead of the build directory, which isn't ideal), but then also tries to compile it with GCC (which fails). How do I tell Qt Creator to use my "extra compiler" instead of the normal one? Changing the extension to something else (module_example.apxs, for example) doesn't appear to be an option, because apxs2 has no option to specify the extension of C source files. Any ideas?

It's not Qt Creator that does it, but qmake. You need to remove module_example.c from SOURCES. The problem is in the part of the .pro file you're not showing.
If you wish to easily access the file from Qt Creator, add it to OTHER_FILES. It will be shown in the project structure in the IDE, but won't be compiled by default.
Also, it's up to you to tell apxs to output to the build path. You need ${OUT_PWD}.

Related

Qt creator does not detect opencv

I am trying to run a program in Qt5.6 with openCV3.1, but no matter what I do, it does not detect openCV include.
Projects -> Build Environment -> Include :
I added C:\opencv\opencv3.1.0\opencv\build\include
Projects -> Build Environment -> LIB:
I added C:\opencv\opencv3.1.0\opencv\build\x64\vc14\lib
However, still in the code there is a yellow line indicating that it doesn't detect opencv:
and there are lots of errors like this:
The environment variable for openCV is also set as:
C:\opencv\opencv3.1.0\opencv\build
How should I install Qt with openCV to avoid these problems?
In the .pro file, add the following lines:
INCLUDEPATH += C:\opencv\opencv3.1.0\opencv\build\include
LIBS += -LC:\opencv\opencv3.1.0\opencv\build\x64\vc14\lib \
opencv310.lib \
And also you need to add the .dll file's path C:\opencv\opencv3.1.0\opencv\build\x64\vc14\bin to the system path (this time you need to restart the Qt IDE)

How to use qmllint in Qt Creator?

qmllint is a syntax checker for QML files written by KDAB which is shipped as a plugin with Qt 5.4. It's usage is based on command line like:
$ qmllint myFile.qml
Is it possible to use it directly in Qt Creator?
QtCreator
You can actually set custom commands in QtCreator to be run without cluttering your qmake file manually because that will effect all the other people in your project, too.
So, if you want to make sure that you only do it for yourself and not clutter it for others, using QtCreator's shiny GUI, I would suggest to follow this:
Projects (left pane)
Build & Run
Build Steps
Add Build Step
Here is the screen how exactly you can set up the command with the corresponding arguments:
With QtCreator's GUI, you can easily change the order with the same concept without touching your project file should you prefer that. There are use cases for that like:
You would not want to run any steps, not even qmake, before the qml file is properly validated
You only have C++ files, so there is no such a thing as "linkage".
etc.
qmake
There are other "generic" approaches useful outside QtCreator, although you asked about this IDE, like putting the command into variables like:
QMAKE_PRE_LINK
QMAKE_PRE_LINK = qmllint $$PWD/path/to/myFile.qml
QMAKE_POST_LINK
QMAKE_POST_LINK = qmllint $$PWD/path/to/myFile.qml
System command execution from your qmake project file
system("qmllint $$PWD/path/to/myFile.ml")
Adding custom targets with QMAKE_EXTRA_TARGETS
qmllinttarget.commands = qmllint $$PWD/path/to/myFile.qml
QMAKE_EXTRA_TARGETS += qmllinttarget
I believe the point in this question is not to have a check for a single file with known name, but to run qmllint on all qml files of a project. Ideally this should be done before building anything, because a build with erroneous qml files is likely to have no real value.
Extending lpapp's answer and playing around with qmake a bit, I came to this solution:
ALL_PWD_QML_FILES = $$files($${_PRO_FILE_PWD_}/*.qml , true)
# a command that creates an empty file with a given name.
win32 {
MY_TOUCH_CMD = copy NUL
} else {
MY_TOUCH_CMD = touch
}
qmllint.output = .qmllint/${QMAKE_FILE_BASE}.qmllint
qmllint.input = ALL_PWD_QML_FILES
qmllint.commands = qmllint ${QMAKE_FILE_NAME} && $${MY_TOUCH_CMD} ${QMAKE_FILE_OUT}
qmllint.CONFIG += no_link recursive target_predeps
QMAKE_EXTRA_COMPILERS += qmllint
This assumes that all qml files are either in the same directory as the .pro file or in subdirectories.
It will run qmllint on all qml files before the actual build, but only if any qml file has changed since a previous build.
Tested on Windows with Qt 5.11 and MSVC.
You can use QMAKE_POST_LINK variable in your .pro file like :
QMAKE_POST_LINK = qmllint $$PWD/QMLFiles/myFile.qml
This runs qmllint on your QML file when you build your project.

Qt: mingw compiled library does only work with both library.so and library.lib file present

I compiled a library using the MinGW toolchain provided with Qt 5.0.2 on Windows. As a result I received a library.so file. First I failed using the library in a Qt application, but now I found out that everything works fine when I make a copy of the liblibrary.so file and call it liblibrary.dll or liblibrary.lib (which is the only file ending supported by the add library wizard in QtCreator).
Now I wonder if this is normal or if I should change something in order not to have both files (which are exact copies). Leaving one away makes the application crash during start up. I added the library as follows to my Qt pro file:
LIBS += -L"../path/to/library" -llibrary
INCLUDEPATH += $$quote(../path/to/library)
EDIT: I compiled the library using the MinGW of Qt, not as Qt project but using mingw32-make and the provided Makefile. As a result I get the liblibrary.so.
EDIT: It seems to work also when renaming the copy to liblibrary.dll instead of .lib. But still, I need two files to make the application work -- the .so and the .dll.
Chris
That's weird, I think you should get a *.a and *.dll files when building a shared lib with MinGW on Windows, as said in the documentation:
In windows, MinGW will output .a and .dll, MSVC2010 will ouput .lib and .dll. In linux, MinGW will output .so, .so.1, .so.1.0 and .so.1.0.0 – .lib, .a and .so are import libraries.
You definitely shouldn't rename your file!
Be careful to:
not to include the "lib" prefix after "-l" in your project file.
put everything after after "-l" in lower case as you're on Windows
not adding any extension to your library name after "-l"
add and reference the .h file used in your library
A real example using QtWebsocket lib:
INCLUDEPATH += "$${PWD}/include/"
LIBS += -L"$${PWD}/libs/" -lqtwebsocket
...
HEADERS += ... \
$${PWD}/include/QWsSocket.h \
...
In my include/ folder, I have the following file:
QWsSocket.h (taken from original project - required)
In my libs/ folder, I have the following file:
libQtWebsocket.a
QtWebsocket.dll
Edit: I struggled with this too initially. Have you tried to build your lib as a static lib instead (CONFIG += staticlib in your library project)? This might help you getting you *.pro file right before switching to using the shared library.
Edit 2: Ok, the fact that you get a *.so file is still a bit odd. In this question
the user has the same issue as you and keep both files, which is just a workaround. According to a later answer it seems that you need to modify your makefile to generate a file with the proper extension. Maybe this will help: http://www.mingw.org/wiki/sampleDLL

QTCreator copy files to output directory with INSTALLS

I have two sub directories docroot and config in my Qt project. Files in these directories shall be copied to the build directory whenever I build / debug the project.
As of https://stackoverflow.com/a/3991210/356726 this is possible by using INSTALLS (QtDoc), which seems to be much easier than running copy command (e.g here). A similar approach is described here.
config.path = $${DESTDIR}/config
config.files = config/*
docroot.path = $${DESTDIR}/docroot
docroot.files = docroot/*
INSTALLS += config docroot
However, when I run a build in Qt Creator nothing happens. This here says I need to run make install . Can I somehow trigger / do this from within Qt Creator automatically whenever I build. I would need always the latest version of the files.
EDIT: Eventually I have used $$OUT_PWD instead of $$DESTDIR
Original comment from Logan here:
"Just a note: I used $$OUT_PWD instead of $$DESTDIR to make it work. For reference $$OUT_PWD is the folder that the program is built to, and $$PWD is the folder that the program is Being built from -in other words it's where the .pro file is."
What you need is a custom build step.
Switch to Projects Mode: press Ctrl+5.
On Build Settings tab under Build Steps click on Add Build Step.
Choose Make from the menu.
Write install into Make arguments: text input box.
(The version where I checked these is Qt Creator 2.4.1.)
I was using Shadow Build on Window 7 and I ran into the same problem than you.
Moreover, after setting my INSTALLS and running make install I was having the following message :
Nothing to be done for `install'.
The reason is that you have to set $$DESTDIR yourself.
In my case I wanted to copy *.qml files, that's how I achieved it:
# if you are using Shadow build, you need to get the output folder
CONFIG(release, debug|release): DESTDIR = $$OUT_PWD/release
CONFIG(debug, debug|release): DESTDIR = $$OUT_PWD/debug
# if you are using normal build (non-shadow) that would have worked as well.
CONFIG(release, debug|release): DESTDIR = release
CONFIG(debug, debug|release): DESTDIR = debug
QmlFiles.path = $$DESTDIR/Qml
QmlFiles.files += $$files(Qml/*.qml)
INSTALLS += QmlFiles
EDIT :
I figure out that $$OUT_PWD can be use to find the Shadow Build Output path. So, I fixed the code which finally come close to what you were using.

Can I get qmake -project to add LIBS += .... to my .pro file?

I have a project that uses Qt. So I have "qmake" make my Makefile from the .pro file. But Qmake can also make that .pro file: qmake -project . This worked until I needed to add an external extra library to my project.
I get lots of hits on google that tell me to add LIBS += ... to my project file, but I want to tell qmake -project something that causes it to add it for me. In effect of course I'll be doing it myself, but I don't think that it's proper that I am editing the generated project file.
If for example I add files to the project directory, I'll have to recreate it and add in the library again, or I'll have to manually add the files to the (almost completely computer-generated) project file. I'm now using a script to auto-generate the project file, and then add in the LIBS += directive, but is there a proper way to do this?
When you are developing without the Qt Creator IDE, unless the IDE includes by itself some automatic utilities, you must edit manually the .pro configuration file.
The generated .pro file is a skeleton file which YOU must fill in with the libraries that you need, then the qmake system figures out the other dependencies and compiles your project. It is a essentially a much better version of pkg-config of gtk + Makefiles.
When you add more source and resource files to your project then manually you must add them to the .pro file.
Example:
QT += core gui
TARGET = qtcp1
TEMPLATE = app
SOURCES += main.cpp\
mainwindow.cpp \
miwidget1.cpp \
lcdrange.cpp
HEADERS += mainwindow.h \
miwidget1.h \
lcdrange.h
FORMS += mainwindow.u
Alternately, you can issue qmake -project over and over again, but this can cause some unforseen accidents, since it includes everything that is in the current directory at the time, including the pre-processed files for conversion to standard C++ from QT dialect. This intermediate files must be erased (cleaned), before the remaking the project and the next make or can lead to tricky problems.
Using the official and free QT Creator IDE takes away most of this burden by adding automatically the new data to the .pro file and cleaning loose ends. Some other IDEs like Code::BLocks and Codelite provide some facilities for QT, but not to the level of QT creator. Some prefer to edit the .pro themselves for custom reasons, other like more other styles of IDEs, like Eclipse.
You should test the waters and decide by yourself what fits best to your needs.
ReEdited, to clarify a few things.

Resources