'Static linking search for dynamic symbols (VS 2015)
I'm trying to compile a project and link it with a static library that I compiled before. My file compile, however it doesn't link.
I'm getting these messages (I truncated it, otherwise it's too long):
error LNK2019: unresolved external symbol __imp_flann_build_index referenced in function ....
error LNK2019: unresolved external symbol __imp_flann_find_nearest_neighbors_index referenced in function ...
error LNK2019: unresolved external symbol __imp_flann_free_index referenced in function ...
error LNK2019: unresolved external symbol __imp_DEFAULT_FLANN_PARAMETERS referenced in function ...
From my understanding is that __imp_ symbols means the linker is trying to find the dll. However, I compiled the library as static, and I properly set the 'Additional Library directories' and 'Additional Dependencies'.
My library is compiled with /MD as my executable. I tried to switch to /MT but because I have other dependencies etc... it just starts messing everything up.
I checked in the library and the symbols are there, but without the __imp_ prefix.
I also put my library at the end so it should resolve symbols, but it still doesn't work.
I don't know what's going on here. Any help is welcome.
Thanks.
Edit: Actually it's a bit less straight forward than I mentionned. Here is what I have:
FLANN library ---> file1.cpp --
file1.h | ---> files.lib ----
file2.cpp | |
file2.h -- |---> program.exe
FLANN.lib ------------------------------------------
Basically I have files using FLANN library (include headers and call functions of the FLANN library). I want to create a static library of these files, and link it to myprogram.exe (and this is where I get the errors mentionned earlier). But I noticed that already in files.lib I have the symbols __imp_ . I don't understand why it uses the dynamic symbols there though.
What I'm trying to do is to tell files.lib to use the static symbols of FLANN.lib, because this is what I will link myprogram.exe to, but so far, it seems the linker doesn't care, and just assume FLANN will be a dynamic library.
Solution 1:[1]
I finally figure it out.
It turns out that the compiler cannot know in advance if I will be linking FLANN with a static or dynamic library.
So I looked into FLANN library itself, and deep there, there is a MACRO (FLANN_STATIC) that should be passed as a compile definition (target_compile_definitions in cmake) to let the compiler know that it will be a static library (as we say, by default, it assumes it will be a dynamic library).
Then I had to add into my CMakeLists.txt for files.lib:
target_compile_definitions(files.lib PUBLIC FLANN_STATIC)
and it worked. I know nobody answered, but it took me hours to find this, so hopefully someday, it might save somebody's hours !!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | whiteShadow |