What is the easiest way to link libraries in Qt projects?

I have two Qt4 Gui application projects and one library project together , all of which reference a .pro file with the subdirs template. So, he likes it:

  • exampleapp.pro
    • app1.pro
    • app2.pro
    • sharedlib.pro

Now what I want to do is the sharedlib link from app1 and app2, so every time I run app1.exe, I don’t need to manually copy sharedlib.dll from my folder to the app1.exe folder.

I can set the PATH environment variable in the project window, but it is not very portable. I looked at moving the LIBS variable to the app1.pro file, but I'm not sure if this applies only to statically linked libraries - I tried it with different syntaxes and doesn't seem to work with shared libraries.

+3
source share
2 answers

You can organize your project as follows:

  • Project1
    • Ben
    • Lib
    • app1
      • app2.pro
    • app2
      • app2.pro
    • Sharedlib
      • sharedlib.pro

in sharedlib.pro can add something like this:

TEMPLATE = lib
TARGET = sharedlibr
QT + = core \
      gui
DESTDIR = .. / lib

DESTDIR: ensures that the compilation result is copied to the "../lib" folder

as for apps app1 and app2:

TEMPLATE = app
TARGET = app1
QT + = core \
      gui
DESTDIR = .. / bin

This is only for development, when creating the installer, libraries and executable files are placed in the appropriate directories, depending on the operating system.

+5
source

To add to this (a bit late!), You can use it QMAKE_POST_LINKto copy files after the build is complete. Example:

defineReplace(formatpath) {
    path = $$1

    win32 {
        return(\"$$replace(path, "/", "\\")\")
    } else:unix {
        return($$replace(path, " ", "\\ "))
    } else {
        error("Unknown platform in formatpath!")
    }
}

win32:COPY_CMD = copy
unix:COPY_CMD = cp -P
macx:COPY_CMD = cp -R

win32:CMD_SEP = $$escape_expand(\n\t)
unix:CMD_SEP = ";"

win32:LIB_EXT = dll
unix:LIB_EXT = so*
macx:LIB_EXT = dylib

# Put here the directory of your library build dir, relative to the current directory
# A path is given for example...
MYLIB_BUILD_DIR = $$_PRO_FILE_PWD_/../lib/bin

QMAKE_POST_LINK += $$COPY_CMD $$formatpath($$MYLIB_BUILD_DIR/*.$$LIB_EXT) $$formatpath($$OUT_PWD/$$DESTDIR) $$CMD_SEP
0
source

Source: https://habr.com/ru/post/1746304/


All Articles