Qt - check availability of linked .lib on windows with preprocessor directives - c++

I'm trying to use preprocessor directives in C++ for avoiding to compile code that requires a .lib, in case the library cannot be linked.
My .pro file contains:
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
and my directives are of the form:
#ifdef _WINSCARD_H_
// do something
#endif
or
#ifndef _WINSCARD_H_
// do something
#endif
This winscard comes with this windows sdk and I can definitely use its functionalities. The problems arise when I try to restrict the compilation based on these conditional directives.
Code compiles fine when using
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
in the .pro file.
Code is skipped during the compilation phase, as if the library is missing when using the above .pro configurations and the conditional directive, even though the library is available and linked:
#ifdef _WINSCARD_H_
// code that needs to be compiled only when library is present and linked.
#endif
The only change is the introduction of #ifdef _WINSCARD_H_.

It is possible to generate conditional build in qmake basing on file existence.
You can add in your .pro file something like that
exists( C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86/winscard* ) {
message( "Configuring for winscard..." )
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
DEFINES += _WINSCARD_H_
}
The block after the built-in function exists() is parsed only when the path is found (it is possible to use asterisk to match part of filename). Here _WINSCARD_H_ is defined only if required file is found.
So, that macro can be used in source code for conditional compilation.
See qmake Test Functions Reference for details.

Related

How do I translate this CMake file to QMake?

Minimal example .zip
I'm trying to use SoLoud in my Qt project.
I managed to successfully compile & use it with this cmake file:
file(GLOB_RECURSE AUDIOSOURCE ${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/audiosource/*.c*)
file(GLOB_RECURSE CORE ${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/core/*.c*)
file(GLOB_RECURSE FILTER ${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/filter/*.c*)
add_compile_definitions(WITH_MINIAUDIO)
set(SOLOUD_SRCS
SoLoud/src/backend/miniaudio/soloud_miniaudio.cpp
${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/c_api/soloud_c.cpp
${AUDIOSOURCE}
${CORE}
${FILTER}
)
add_executable(output main.cpp ${SOLOUD_SRCS})
target_include_directories(output PUBLIC ${CMAKE_CURRENT_LIST_DIR}/SoLoud/include)
My main project is built with Qmake. So I'm trying to translate it...
INCLUDEPATH += SoLoud/include
DEFINES += "WITH_MINIAUDIO=\"\""
SOURCES += SoLoud/src/backend/miniaudio/soloud_miniaudio.cpp \
SoLoud/src/c_api/soloud_c.cpp \
$$files("SoLoud/src/audiosource/*.c*", true) \
$$files("SoLoud/src/core/*.c*", true) \
$$files("SoLoud/src/filter/*.c*", true)
HEADERS += $$files("SoLoud/include/*.h", true) \
This produces a wall of errors:
https://pastebin.com/pUQuysEr
Why?
BTW: i'm aware that recursive search of *.c* files is bad practice. However, that's what library's authors recommend and I gave up trying to find every single .cpp i need (there were MANY).

How to add an hdf5 to a Qt-Project?

Situation:
I need to add a library (HDF5 in my case) to my qt project.
I know how to code c++ enough for my purposes, but i have no clue about the .pro file. When i try to google my problem or general guides for adding libraries i find lots of answers but understand none of them, because they require more knowledge then i have. They say stuff like "compile it here and there", "add this and that to your system", "use qmake in directory xyz". Can someone please answer the question so that one who only knows a bit of c++ and the green compile & run button of qt understands it? Would be great :-)
What I tired:
I know that there is a wizarb in qt that can add librarys to projects. I used it to add the libraries needed. I added them as "external librarys".
I tried as dynamic or static, i tried adding only the hdf5.lib or the hdf5_cpp.lib too.
Problem:
When I do it as I described above and try to use a function from the added library i always get errors like: undefined reference to H5::Function_Name_xyz.
My .pro looks like (generated by the qt add-library-wizard):
TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += \
main.cpp
#Add external static library "hdf5.lib":
unix|win32: LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5
INCLUDEPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
DEPENDPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
win32:!win32-g++: PRE_TARGETDEPS += 'C:/Program Files/HDF_Grou/HDF5/1.10.2/lib/hdf5.lib'
#Add external static library "hdf5_cpp.lib"
unix|win32: LIBS += -L'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/' -lhdf5_cpp
INCLUDEPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
DEPENDPATH += 'C:/Program Files/HDF_Group/HDF5/1.10.2/include'
win32:!win32-g++: PRE_TARGETDEPS += 'C:/Program Files/HDF_Group/HDF5/1.10.2/lib/hdf5_cpp.lib'
Using:
C++
Qt 5.10.1
MinGW 32bit
HDF5 1.10.2
Windows 7
I had this same issue awhile back. I did the following to correct the linking error.
Make sure you run qmake (Build-> Run qmake) after adding a new library to the .pro file.
Since you are using windows with the pre-built HDF library, you should use the MSVC2015 32 bit Build&Run kit. The pre-built libraries used Microsoft Visual C++ compiler, so to use the HDF libraries you will need to use that compiler.
Per the HDF documentation, you need to list the external libraries first. The following snippit shows my setup using dynamic libraries.
If your using dynamic libraries be sure to add DEFINES += H5_BUILT_AS_DYNAMIC_LIB to your .pro file.
win32: LIBS += -L$$PWD/'../../../../../Program Files/HDF_Group/HDF5/1.10.4/lib/' -lszip -lzlib -lhdf5 -lhdf5_cpp
INCLUDEPATH += $$PWD/'../../../../../Program Files/HDF_Group/HDF5/1.10.4/include'
DEPENDPATH += $$PWD/'../../../../../Program Files/HDF_Group/HDF5/1.10.4/include'

Linking libtensorflow_cc.so file to QT project

I want to use tensorflow_cc library in QT creater
I tried linking the libtensorflow_cc.so in .pro file but I'm still unable to access the header files.
test.pro file:
LIBS += -L/usr/local/lib/ -ltensorflow_cc
INCLUDEPATH += /usr/local/lib/
test.cpp file:
#include <tensorflow/core/platform/env.h>
error :
tensorflow/core/platform/env.h: No such file or Directory
I have added the path of the .so file to LD_LIBRARY_PATH. Is there anyway to include the tensorflow library and use the header files in QT.
This is a similar question but it didn't quite help me.
Thanks!
Add this to the .pro file:
LIBS += /usr/lib/libtensorflow_cc.so

link boost libs to qt with msvc

I have installed qt-opensource-windows-x86-msvc2013_64_opengl-5.4.0.exe and compiled boost_1_58_0.zip with this command: b2 toolset=msvc --build-type=complete stage. It works fine with Visual Studio, but when I try use it with Qt I get this error:
:-1: error: LNK1104: cannot open file 'libboost_filesystem-vc120-mt-gd-1_58.lib'
Here is my .pro file:
TEMPLATE = app
QT += qml quick widgets
SOURCES += main.cpp \
testclass.cpp
RESOURCES += qml.qrc
INCLUDEPATH += C:\boost
LIBS += "-LC:\boost\stage\lib\libboost_filesystem-vc120-mt-gd-1_58.lib"
#Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
testclass.h
In the LIBS variable use just "-L" for correct library path (-L). You made a mix, specifying a file (lowercase l) while libs directory is missing.
You do not need to specify the library, boost has pragmas for that.

Add static .a library in QtCreator

I want to use functionality of a math library ALGLIB and it's offered in .h and .cpp files. So I build it and added all the .o files to alglib.a. I copied it to my source directory and added these lines to my .pro file:
INCLUDEPATH += /path/to/ALGLIB/cpp/src
LIBS += -Lalglib
Well - I still get those "undefined reference to ..." errors when trying to build.
-L sets a directory in which the linker should search for libraries.
-l sets a library file to link in the following way: -lalglib will look for a file named libalglib.a in all directories that are set with -L
Adding a file to LIBS without anything will link that exact file.
So either:
LIBS += alglib.a
or, provided that the alglib file name is libalglib.a:
LIBS += -Lalglib-directory -lalglib