Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Trying to link an external library with Qmake. Works with absolute path, but not with relative path.
QtWS25 Last Chance

Trying to link an external library with Qmake. Works with absolute path, but not with relative path.

Scheduled Pinned Locked Moved Unsolved Qt Creator and other tools
qt4.8qmake
10 Posts 3 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Curtwagner1984
    wrote on 7 Mar 2022, 14:26 last edited by
    #1

    Hello.

    I have an external library with the absolute path: "C:\temp\dx2\ni4882\2012\lib\libni4882.a".

    In my Qmake the library is linked in the following way:

    win32 {
        LIBS += -L"C:/temp/dx2/ni4882/2012/lib/" -lni4882
    }
    

    I wanted to include the library with the project source files so it won't be dependent on an external path. So I created a folder named ThirdParty at the root of my project. And added the relative path to the qmake instead what is written above, like so:

    win32 {
        #LIBS += -L"C:/temp/dx2/ni4882/2012/lib/" -lni4882
        LIBS += -L../../../ThirdParty/dx2/ni4882/2012/lib -lni4882
        message($$LIBS)
    }
    

    However this doesn't work and I get an error:

    :-1: error: cannot find -lni4882
    :-1: error: collect2.exe: error: ld returned 1 exit status

    This does not happen when I revert to using the absolute path.

    As far as I can tell I have the correct amount of ../ in the path. And the file does exist there. I tried to see what the path is evaluated to by printing out a message message($$LIBS) but it just prints the unevaluated strings I've inputted.

    Here is my folder structure:
    explorer_r7QOueuomp.png

    It's worth noting this is under Qt4.8.

    I would be really glad if someone has any insights on why this doesn't work.

    Also, another thing I don't understand is that the file's name is libni4882 yet in Qmake it's ni4882. Why does it work with this name, if it isn't the actual name of the file?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kkoehne
      Moderators
      wrote on 7 Mar 2022, 15:16 last edited by kkoehne 3 Jul 2022, 15:17
      #2

      I would be really glad if someone has any insights on why this doesn't work.

      Can you show what is actually passed to the linker line?

      You should probably make the path absolute from within qmake by using "$$absolute_path(...)": https://doc.qt.io/qt-5/qmake-function-reference.html#absolute-path-path-base .

      Also, another thing I don't understand is that the file's name is libni4882 yet in Qmake it's ni4882

      This is just how the -l argument for e.g. gcc / ln works, see e.g. https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Link-Options.html#Link-Options.

      Director R&D, The Qt Company

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Curtwagner1984
        wrote on 7 Mar 2022, 15:24 last edited by
        #3

        @kkoehne said in Trying to link an external library with Qmake. Works with absolute path, but not with relative path.:

        Can you show what is actually passed to the linker line? I assume I think it might be best if you ask qmake to make the path absolute with "$$absolute_path(...)": https://doc.qt.io/qt-5/qmake-function-reference.html#absolute-path-path-base .

        I looked at this before but it says this was added in Qt5, and I'm in Qt4.8. When I try to do message($$absolute_path(../../../ThirdParty/dx2/ni4882/2012/lib)) I'm getting:

        17: error: Unknown replace function: absolute_path

        @kkoehne said in Trying to link an external library with Qmake. Works with absolute path, but not with relative path.:

        This is just how the -l argument for e.g. gcc / ln works, see e.g. https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Link-Options.html#Link-Options.

        Thank you!

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Curtwagner1984
          wrote on 8 Mar 2022, 06:47 last edited by
          #4

          I've managed to solve this using $${PWD} like so:

          ROOT_DIR        = $${PWD}/../../..
          THIRD_PARTY_DIR = $${ROOT_DIR}/ThirdParty
          NI_4883_DIR     = $${THIRD_PARTY_DIR}/dx2/ni4882/2012/lib/
          

          And then:

          win32 {
              LIBS += -L$${NI_4883_DIR} -lni4882
              message(NI_4883_DIR = "$${NI_4883_DIR}")
          }
          

          And it seems to compile just fine.

          1 Reply Last reply
          1
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 8 Mar 2022, 19:40 last edited by
            #5

            Hi,

            Your issue came from the fact that you were generating the path as if the build would happen within the source folder. Hence it could not find the library because the path was now used as relative to said build folder.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            C 1 Reply Last reply 9 Mar 2022, 06:12
            2
            • S SGaist
              8 Mar 2022, 19:40

              Hi,

              Your issue came from the fact that you were generating the path as if the build would happen within the source folder. Hence it could not find the library because the path was now used as relative to said build folder.

              C Offline
              C Offline
              Curtwagner1984
              wrote on 9 Mar 2022, 06:12 last edited by Curtwagner1984 3 Sept 2022, 06:16
              #6

              @SGaist Hello! Thank you for your reply.
              Can you please elaborate on this? This is not a qMake file I created. I inherited this project and trying to figure it out.

              There are other lines in the file that have relative paths that work without an issue, like this

              HEADERS +=  ../../../include/acutronic.h
              

              This ../../../ points to the root folder. Though from what you're saying, it should point three levels up from the build folder.

              Regardless, can you suggest the correct/recommended way of doing? (pointing to files in the root dir).
              I've read this guide. And it seems that the correct way is to make a config.pri file in the root folder that defines the root dir, something like ROOT_DIR = $${PWD} and then import this file in the sub projects.

              But this import will also use ../ So I'm not completely sure how to solve this issue.

              Though I don't think this is sufficient because if I import this in the sub-project, wouldn't $${PWD} resolve to the current dir? As in the dir of the sub-project and not the root of project?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 9 Mar 2022, 07:11 last edited by SGaist 3 Sept 2022, 07:15
                #7

                The HEADERS variable is processed by qmake to generate the content of the Makefile while the LIBS variable content is going to get forwarded to the linker (still through qmake generating the Makefile but it's more "as is" than for HEADERS).

                So, indeed, the usual way is to have .pri files that provides the information with include/library paths. These paths should use PWD. The PWD content is .pro/.pri file dependent so it always matches the folder where the file is located.

                As for including them using a chain of ../ is perfectly fine.

                One more thing, just in case do not use parenthesis for PWD, you want it to be the qmake variable and not an environment variable.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                C 1 Reply Last reply 9 Mar 2022, 12:38
                2
                • S SGaist
                  9 Mar 2022, 07:11

                  The HEADERS variable is processed by qmake to generate the content of the Makefile while the LIBS variable content is going to get forwarded to the linker (still through qmake generating the Makefile but it's more "as is" than for HEADERS).

                  So, indeed, the usual way is to have .pri files that provides the information with include/library paths. These paths should use PWD. The PWD content is .pro/.pri file dependent so it always matches the folder where the file is located.

                  As for including them using a chain of ../ is perfectly fine.

                  One more thing, just in case do not use parenthesis for PWD, you want it to be the qmake variable and not an environment variable.

                  C Offline
                  C Offline
                  Curtwagner1984
                  wrote on 9 Mar 2022, 12:38 last edited by
                  #8

                  Thank you so much! I'll try to integrate your advise into this project.

                  @SGaist said in Trying to link an external library with Qmake. Works with absolute path, but not with relative path.:

                  One more thing, just in case do not use parenthesis for PWD, you want it to be the qmake variable and not an environment variable.

                  You mean $$PWD and not $${PWD}?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 9 Mar 2022, 12:46 last edited by
                    #9

                    No no, $${PWD} is fine as well since it uses the curly brackets. I just wanted to make you aware of the fact that $$(PWD) with parenthesis will yield a different result than what you could expect as it means you want the content of the corresponding environment variable.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    C 1 Reply Last reply 9 Mar 2022, 13:24
                    2
                    • S SGaist
                      9 Mar 2022, 12:46

                      No no, $${PWD} is fine as well since it uses the curly brackets. I just wanted to make you aware of the fact that $$(PWD) with parenthesis will yield a different result than what you could expect as it means you want the content of the corresponding environment variable.

                      C Offline
                      C Offline
                      Curtwagner1984
                      wrote on 9 Mar 2022, 13:24 last edited by
                      #10

                      @SGaist Thank you! This is very helpful to know.

                      1 Reply Last reply
                      0

                      8/10

                      9 Mar 2022, 12:38

                      • Login

                      • Login or register to search.
                      8 out of 10
                      • First post
                        8/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved