Trying to link an external library with Qmake. Works with absolute path, but not with relative path.
-
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 statusThis 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 messagemessage($$LIBS)
but it just prints the unevaluated strings I've inputted.Here is my folder structure:
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'sni4882
. Why does it work with this name, if it isn't the actual name of the file? -
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.
-
@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!
-
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.
-
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.
-
@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 aconfig.pri
file in the root folder that defines the root dir, something likeROOT_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? -
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.
-
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}
? -
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.