How to prevent Qt from linking - qt

I have a bigger project including code in different languages. One part of it is written in C++/QML. The final linkage is performed via DMD. I need qmake to generate only object files without linking. Currently qmake tries to link a library, what fails with undefined reference.
Is there a way to make qmake generate only object files without linking?

Simply change the project type to the lib template, and add CONFIG += staticlib. The static library is simply a single file archive of all your objects. DMD can then link it to the final executable.

Related

Create and Use Dynamic Library with Resources in Qt

I have created a Dynamic Library using Qt Creator which I have included in another Qt Project but when I call functions with works with resource files for example reading the contents of a .txt in resources, it does not work.
Once I have the Build of the Dynamic Library which created the *.so, *.a and the qrc_resources.cpp files in another folder outside the library project folder, example:
Project Folder: /home/username/Qt Project/library/
Build Folder: /home/username/Qt Project/build-library/
I then copy the build files into the library project folder and from the test project (a Qt Application Project), I right click on the project -> Add Library -> External Library -> Select the .so file (from the library project folder which I just copied).
Then, after including the header files and calling a function which reads the contents of a file from the qrc resources, it does not work.
UPDATE
I used a QFile to check if the Resource File exists in which it does but I get this error: QIODevice::read (QFile, ":/file.txt"): device not open
Adding Libraries to Projects
later
I think need to create an external resource file.
So I was able to resolve the problem by using a QFile object instead of ifstream object to open and read the file.

How to automatically include Qt Linguist files into Qt resources for the CMake project?

I use CMake to build Qt project with internatiolization support. Using qt5_create_translation I can create *.ts files into the source directory and *.qm files into the build directory.
I want to reduce the number of files I should to distribute. Storing translation files :/translations/*.qm into the Qt resources is straitforward solution.
But during build process names of the resources are generated automatically and there is no native way to emplace them into the *.qrc file automatically.
How to achieve desired using, say, add_custom_command or something else?
Qt's qrc file is just xml files. Thus you can an external tool to add it.
A pure cmake solution could be:
Create a dummy resource file named #TRANS_FILE#
Add this file to your qrc file
Fetch the name of the translation file into a variable. Don't know how.
Use cmake's configure_file to replace the dummy resource name
Example:
set(TRANS_FILE ${NameOfTranslationFile})
configure_file(infile.qrc outfile.qrc #ONLY)

Qt way to get libraries path

I have several QPlugins (shared objects) that I install {CMAKE_PREFIX}/lib/<appname>, how can I find out this path at runtime to load the plugins? Note that the CMAKE_PREFIX user user settable.
If these plugins are part of your application project and you are looking for a build time answer, you can generate a config.h file containing that path.
You can use CMake's configure_file command for that.

Add appropriate header files to Qt programs so that I can use Q_DECLARE_PRIVATE

I want to create a shared library to communicate with database. I want to use Pimple idea in my implementation, but I don't know which header files must be added to program so that Qt can find Q_DECLARE_PRIVATE and some other macros.
Is it necessary to add some command to .pro file?

Get path of file in external library

I have 2 Qt projects that I've made. One builds a library the other builds a GUI that uses that library. My project with the library includes a bunch of shaders that I've made. Within my GUI I want to reference the path to these shaders. My approach is to have a static function in my library called getLibraryPath() that returns a QString referencing the path on my file system to the library.
I've tried using QDir::currentPath() and QApplication::applicationFilePath() and other similar functions that return paths but they all return paths referencing my GUI's path, not the library's path even though the function to get the path is located inside the library.
Within my GUI I try to reference a shader by doing: Shader->addShaderFromSourceFile(QGLShader::Vertex, MyExternalLib::getLibraryPath() + "/shaders/MyVertexShader.vs");
A library is just a bunch of code. It has no notion of having its own path. The normal way of doing what you intend to is:
If the library is a separately installed product -- set a registry entry in the installer, and check it at runtime. This will be platform specific, unless the library is written in Qt -- then you can use QSettings, but make sure that you set your company name, application, etc. appropriately as it's not done by QApplication for you anymore.
If the library is installed as a part of your application, in the same folder tree: use QCoreApplication::applicationDirPath() and add a relative path between the application executable and the files you want to this path.
I was able to solve this by adding the files I want to reference to a qrc file within my library project. By doing this, I could then reference the qrc path and the files inside of them easily.

Resources