I have a strange error that has bothered me all day. I have a library function that I have compiled. I statically link the library to test it in a separate console application and everything builds and works ok. It is just a simple sqrt function called sqrtval at the moment. However, when I include this library in a larger graphical project then I get linker errors. I have set the Additional library directories and Additional dependency settings identically in both the console app (that works) and the graphical main app (that does not work). The error is as follows.
1
Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.
All rights reserved. 1>Linking...
1>MainAppWindow.obj : error LNK2019:
unresolved external symbol "double
__cdecl sqrtval(double)" (?sqrtval##YANN#Z) referenced in
function "public: virtual void
__thiscall MainComponent::sliderValueChanged(class
juce::Slider *)"
(?sliderValueChanged#MainComponent##UAEXPAVSlider#juce###Z)
1>....\bin\Debug\tutorial.exe : fatal
error LNK1120: 1 unresolved externals
If your library is written in plain C, you have to declare sqrtval as:
extern "C" double sqrtval(double);
What does the linker command line look like? Is your library listed on the linker command for the GUI application?
Related
I would like to integrate Lua into my current C++ application I build with VisualStudio 2015 (x64 mode).
I grabbed a simple Lua wrapper from here, and the Lua binaries for windows x64 from here (the lua-5.1.5_Win32_dll14_lib.zip one).
Next, I added the include and library path to the project preferences. Works so far in the IDE. However, when I try to compile the code it throws 24 errors (like these first two):
1>LuaScript.obj : error LNK2019: unresolved external symbol "lua_close" referenced in function ""public: __cdecl LuaScript::~LuaScript(void)" (??1LuaScript##QEAA#XZ)".
1>LuaScript.obj : error LNK2019: unresolved external symbol "lua_gettop" referenced in function ""public: void __cdecl LuaScript::clean(void)" (?clean#LuaScript##QEAAXXZ)".
which tells me next to nothing. Seems that the linker is unable to find the library but the includes are working and the path to library is set, too. Any suggestions what I might have missed or should look into? Any hint would be appreciated!
Well i basically inherited a bunch of code that i was told to fix because it used to work a few months ago but it doesn't at the moment. The program itself seems to be riddled with linking errors and i was able to fix some of them. However I have run across a LNK2019 unresolved external _imp error where some function utilized in the main is not resolved. due to the "_imp" i am assuming that it is a problem related to importing from .dll or .lib files.
First of all i have three .lib files which i believe am importing them correctly into VS2010 and i have configured the platform to be 64x. There is also .dll files that correspond with the .lib files. The .h file that contains the declaration for these error ridden functions contain something like
ILAPI void ILAPIENTRY ilDeleteImage(const ILuint Num);
unfortunately i would be guessing that the definition is defined in the .dll file which i did not write myself so i am unsure. But since this is code that worked before i believe that i am getting this error because the linker can't find the definition rather than def/decl not matching reason.
when i hover over the ILAPI it states: "ILAPI __declspec(dllimport)"
My current guess is that the program imports the .lib files and the .lib files use the .dll files to get the definition of the functions. I believe that i am importing the .lib files since the compiler no longer keeps on telling me that it can't find specific .lib files. However i am concerned that it may not be connecting the the .dll files. I am some what unsure. I have opened the .lib files and the .lib files contain the names of the functions that are giving errors. I have also used the dependency walker program to look at my DLL files and it has been giving me some of the following errors:
Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.
Error: Modules with different CPU types were found.
Based on other peoples comments i feel like i can ignore the 2nd error. But i am unsure of the first error. I am also unsure if that would be the root cause of the problem. It may or may not be.
I have also looked inside the .lib files using VS cmd and dependency walker and it seems like the names of the functions that can't be found are listed in one of the .lib and .dll.
In terms of configuration I am running on release mode x64 platform.
I have added "DevIL.lib ILU.lib ILUT.lib " library functions in the proj -> prop -> linker -> commandline. I have also added the path for linker -> general -> additional library directory. I have also tried messing with input additional dependency but it has no effect. The .lib and .dll files are put in the same directory as well. In the proj property configuration i do not mention .dll anywhere (am i supposed to? I've tried in various locations but just creates more error) I understand that there are a ton of posts regarding link 2019 error but i have not had good luck so far in my search for my particular problem. I would appreciate any suggestions, comment, or a link where i may find a clue as in to why this is occuring
here is the linker command from log:
here is the linker command from the build log itself:
Link:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin\x86_amd64\link.exe /ERRORREPORT:PROMPT /OUT:"x64\Release\dff.exe" /VERBOSE /INCREMENTAL /NOLOGO
/LIBPATH:C:\Users\Sub2\Desktop\dff\x64\Release /MANIFEST
/ManifestFile:"x64\Release\dff.exe.intermediate.manifest"
/MANIFESTUAC:"level='asInvoker' uiAccess='false'" /DEBUG
/PDB:"C:\Users\Sub2\Desktop\dff\x64\Release\dff.pdb"
/SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /TLBID:1 /DYNAMICBASE /NXCOMPAT
/IMPLIB:"x64\Release\dff.lib" /MACHINE:X64
x64\Release\dff.exe.embed.manifest.res
x64\Release\acquisition.obj
x64\Release\azmemutil.obj
x64\Release\dff.obj
x64\Release\fft.obj
x64\Release\FocusMeasure.obj
x64\Release\ge.obj
x64\Release\stdafx.obj DevIL.lib ILU.lib ILUT.lib
1>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/OPT:ICF' specification
// This is from Win32's <wingdi.h> and <winnt.h>
#if defined(__LCC__)
#define ILAPI __stdcall
#elif defined(_WIN32) //changed 20031221 to fix bug 840421
#ifdef IL_STATIC_LIB
#define ILAPI
#else
#ifdef _IL_BUILD_LIBRARY
#define ILAPI __declspec(dllexport)
#else
#define ILAPI __declspec(dllimport)
#endif
#endif
#elif __APPLE__
#define ILAPI extern
#else
#define ILAPI
#endif
Also:
#define ILAPIENTRY __stdcall
build log info when it gets close to error:
Found KERNEL32_NULL_THUNK_DATA
Referenced in kernel32.lib(KERNEL32.dll)
Loaded kernel32.lib(KERNEL32.dll)
Searching C:\Users\Sub2\Desktop\dff\x64\Release\DevIL.lib:
Searching C:\Users\Sub2\Desktop\dff\x64\Release\ILU.lib:
Searching C:\Users\Sub2\Desktop\dff\x64\Release\ILUT.lib:
Searching C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\MSVCRT.lib:
Searching C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\OLDNAMES.lib:
Searching C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\msvcprt.lib:
Finished searching libraries
Finished pass 1
Invoking CVTRES.EXE:
/machine:amd64
/verbose
/out:"C:\Users\Sub2\AppData\Local\Temp\lnk92ED.tmp"
/readonly
"x64\Release\dff.exe.embed.manifest.res"
Microsoft (R) Windows Resource To Object Converter Version 10.00.30319.01
Copyright (C) Microsoft Corporation. All rights reserved.
adding resource. type:MANIFEST, name:1, language:0x0409, flags:0x30, size:2
1>dff.obj : error LNK2019: unresolved external symbol __imp_iluGetImageInfo referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_iluImageParameter referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilDeleteImages referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilSaveImage referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_iluFlipImage referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_iluScale referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilTexImage referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilCopyPixels referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilGetError referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilLoadImage referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilBindImage referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilGenImages referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilInit referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilGetInteger referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilEnable referenced in function main
1>dff.obj : error LNK2019: unresolved external symbol __imp_ilOriginFunc referenced in function main
1>x64\Release\dff.exe : fatal error LNK1120: 16 unresolved externals
1>Done Building Project "C:\Users\Sub2\Desktop\dff\dff.vcxproj" (rebuild target(s)) -- FAILED.
Build FAILED.
Just in case i have also tried writing #define _IL_BUILD_LIBRARY but has no effect.
I'm adding an answer here to summarize a bit on the above comments, the probable root cause and finding a solution to the unresolved errors. I'll preface this by saying linkers tend to be dumb low level tools. If the symbols referenced in the object files don't match exactly with the library then the build process bails out with those unresolved errors. Therefore, the author of the dll library and clients using it will need to put in some effort to ensure the symbols agree.
Root Causes
Unresolved errors are usually caused by one of the follow:
Neither the project nor the libraries linked against implement the function for that symbol.
The libraries actually do implement the function but under a differing reference symbol.
The headers for the library aren't preprocessed or decorated correctly thereby causing #2.
The Hunt
Base on what the OP described the dll libraries linked against exposes a C API. It is unclear what calling convention is being followed by the library in question. The exported names in the dll contain plain undecorated function names suggesting a __cdecl convention. The provided corresponding headers, OTOH has a function like:
ILAPI void ILAPIENTRY ilDeleteImage(const ILuint Num);
which expands into this after preprocessing:
__declspec(dllimport) void __stdcall ilDeleteImage(const ILuint Num);
From this you can conclude three possible scenarios:
The unresolved functions follow a __cdecl convention and the provided headers are wrong.
The unresolved functions follow a __stdcall convention and the exported dll names are wrong.
The functions follow __stdcall but are undecorated to look like __cdecl in the dll. This suggest that a .def file might have been used to build the dll in question.
A Sad State of Affair
Unfortunately, the function call convention followed in a win32 dll is in a confusing state of affairs. There's nothing in the language standard, C or C++, that addresses this ABI issue. See my other answer here. The toolchain vendor is free to decorate the names however they like but typcially for __cdecl functions it's plain undecorated or with a prefixed leading _ underscore.
WinAPI functions you find, like in kernel32.dll user32.dll gdi32.dll etc. are also undecorated but yet follow __stdcall. However, MSVC itself decorates __stdcall with a trailing ampersand with total bytes of the parameters(eg. ilDeleteImage#4) thus contributing to the confusion. To override how LINK decorates the functions, you have to provide a .def file that specifies the new name alias. See here for more details.
Finding the Real Convention
Assuming you don't have access to the source used to build the dll, there are two approachs I can think of to identify the real convention used.
Create a minimal test application that calls into the dll functions as if it uses __cdecl and see if it crashes and burns. This is the easier more straightforward method and you don't have to understand assembly to do it.
Second approach, you do the same but insert assembly breakpoints into your test application and run it through the debugger doing single step instructions. Make sure to choose a dll function taking at least one parameter. For example:
// pullin dll headers
// etc..
int main()
{
__asm int 3;
ilDeleteImage(0xdecafbad);
}
This will break and give control back to the debugger right before the dll function call. From here single step at the assembly level until you reach ilDeleteImage's function prologue.
; function prologue
push ebp
mov ebp, esp
; function implemention
; more opcodes here
; ...
; function epilogue
mov esp, ebp
pop ebp
ret 0x8
Check what form of ret is being used on function return. The number argument following the ret mnemonic indicates how much to increment the esp stack pointer. Any number >0 suggestes a __stdcall function. The hypothical disassembly above shows a __stdcall function freeing 8 bytes on the stack which also hints that this function takes 2 arguments.
I had this compile error while building a project using external static libraries, QDBM and PCRE:
_main.obj : error LNK2001: unresolved external symbol __imp__dpversion
_main.obj : error LNK2001: unresolved external symbol __imp__regcomp
I had correctly configured these external projects to build static libraries, but I had forgotten to define the correct preprocessor defines in my own code, which used these libraries, to enable static linking.
So when they imported the headers from QDBM and PCRE, they added a __declspec(dllimport) that shouldn't be there (for static linking), so they were trying to import symbols from DLLs that didn't exist.
I added the missing preprocessor defines to my build system, which fixed the error:
-DQDBM_STATIC -DPCRE_STATIC
Which meant adding the following lines to my CMakeLists.txt file:
# Tell QDBM not to build itself as a DLL, because we want to link statically to it.
target_compile_definitions(qdbm PUBLIC -DQDBM_STATIC)
target_compile_definitions(lib_common PUBLIC -DPCRE_STATIC)
I've added the .dll to Windows/System32, the .lib to VS/lib, and the .h to the VS/include.
All that would be left is linking it through the project. This is what I have:
#include <glfw.h>
int main(void)
{
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
if (!glfwOpenWindow(640, 480, 8, 8, 8, 0, 24, 0, GLFW_WINDOW))
return -1;
/* Loop until the user closes the window */
while (glfwGetWindowParam(GLFW_OPENED))
{
/* Render here */
/* Swap front and back buffers and process events */
glfwSwapBuffers();
}
return 0;
}
1>Main.obj : error LNK2019: unresolved external symbol _glfwSwapBuffers referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwGetWindowParam referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwOpenWindow referenced in function _main
1>Main.obj : error LNK2019: unresolved external symbol _glfwInit referenced in function _main
1>C:\Users\Danny\documents\visual studio 2010\Projects\OGL2\Debug\OGL2.exe : fatal error LNK1120: 4 unresolved externals
I've went to Project > Properties > Configuration Properties > Linker >
Added the following to Additional Dependencies:
opengl32.lib;GLFW.lib;glu32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)
And for good measure, I also set Ignore All Default Libraries to "No".
I know I should have linked: opengl32.lib, GLFW.lib, glu32.lib, kernel32.lib and user32.lib
Unfortunately, the LNK 2019 problems persist. Really am unsure why. All help is greatly appreciated.
The external names indicate that your project is building for a 64-bit target (32-bit DLL exports generally have the #nn suffix used by the stdcall calling convention). I'll bet your libraries are build for a 32-bit target.
Change the project settings to Win32 instead of x64.
I guessed incorrectly - the unresolved external symbols such as _glfwSwapBuffers indicate that you're building for a 32-bit target. When building for a 64-bit target, the external names do not get decorated with an underscore.
But the basic solution is the same - make sure that the target that is being built is the same as the target that the libraries you have are. So if you've downloaded the 64-bit GLFW libraries, make sure your VS project is building for a Win64 target.
I was able to compile and link your test program after extracting the 64-bit GLFW binary distribution into c:\temp:
C:\temp>cl /I c:\temp\glfw-2.7.7.bin.WIN64\include\GL test.cpp c:\temp\glfw-2.7.7.bin.WIN64\lib-msvc110\GLFWDLL.lib
Microsoft (R) C/C++ Optimizing Compiler Version 17.00.50727.1 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
test.cpp
Microsoft (R) Incremental Linker Version 11.00.50727.1
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
test.obj
c:\temp\glfw-2.7.7.bin.WIN64\lib-msvc110\GLFWDLL.lib
C:\temp>
I got exactly the same error message . I notice there is no .lib file in my glfw folder ,Because I download source package, You need download pre-compile binaries version,
Then I got it to work;
I am using Visual Studio 10 set to use the v90 platform toolset. I have three applications that use several of the same classes. So, rather than have three copies of each of those classes, I am trying to move them out into a common static lib. One of them is giving me link problems. The others seem to link fine. Here's the problem class:
HEADER
#ifndef LIMIT_SINGLE_INSTANCE_INCLUDED
#define LIMIT_SINGLE_INSTANCE_INCLUDED
#include <windows.h>
class CLimitSingleInstance
{
protected:
DWORD m_dwLastError;
HANDLE m_hMutex;
public:
CLimitSingleInstance(TCHAR *strMutexName);
~CLimitSingleInstance();
BOOL IsAnotherInstanceRunning();
};
#endif
BODY
#include "LimitSingleInstance.h"
CLimitSingleInstance::CLimitSingleInstance(TCHAR *strMutexName)
{
//Make sure that you use a name that is unique for this application otherwise
//two apps may think they are the same if they are using same name for
//3rd parm to CreateMutex
m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
m_dwLastError = GetLastError(); //save for use later
}
CLimitSingleInstance::~CLimitSingleInstance()
{
if (m_hMutex) //Do not forget to close handles.
{
CloseHandle(m_hMutex); //Do as late as possible.
m_hMutex = NULL; //Good habit to be in.
}
}
BOOL CLimitSingleInstance::IsAnotherInstanceRunning()
{
return (ERROR_ALREADY_EXISTS == m_dwLastError);
}
When this class is directly part of one of my main application solutions, there is no problem. I have now moved it into my static lib solution, and that solution builds fine. However, I find that I can no longer link my main application solution against my new static lib. Here is the result of attempting to build both debug and release versions of my main application:
------ Rebuild All started: Project: WCCJ, Configuration: ReleaseTENA Win32 ------ CAssetEntity.cpp main.cpp ReadWCCJParameters.cpp
WCCJ.cpp WCCJParameters.cpp Generating Code...
DCTUtilsRel.lib(MessageWrapper.obj) : MSIL .netmodule or module
compiled with /GL found; restarting link with /LTCG; add /LTCG to the
link command line to improve linker performance
Creating library ....\bin\WCCJ-TENA.lib and object ....\bin\WCCJ-TENA.exp main.obj : error LNK2001: unresolved external
symbol "public: __thiscall
CLimitSingleInstance::CLimitSingleInstance(char *)"
(??0CLimitSingleInstance##QAE#PAD#Z) ....\bin\WCCJ-TENA.exe : fatal
error LNK1120: 1 unresolved externals
------ Rebuild All started: Project: WCCJ, Configuration: DebugTENA Win32 ------ CAssetEntity.cpp main.cpp ReadWCCJParameters.cpp
WCCJ.cpp WCCJParameters.cpp Generating Code... CAssetEntity.obj :
warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/INCREMENTAL:NO'
specification
Creating library ....\bin\WCCJ-TENA-d.lib and object ....\bin\WCCJ-TENA-d.exp main.obj : error LNK2019: unresolved
external symbol "public: __thiscall
CLimitSingleInstance::CLimitSingleInstance(char *)"
(??0CLimitSingleInstance##QAE#PAD#Z) referenced in function "void
_cdecl `dynamic initializer for 'gSingleInstanceObj''(void)" (??_EgSingleInstanceObj##YAXXZ) ....\bin\WCCJ-TENA-d.exe : fatal
error LNK1120: 1 unresolved externals
========== Rebuild All: 0 succeeded, 2 failed, 0 skipped ==========
When I open the .lib in a binary editor and search for the mangled name the linker wants (??0CLimitSingleInstance##QAE#PAD#Z), I find that it is indeed not found. The closest matches I can find are:
??0CLimitSingleInstance##QAE#PA_W#Z
??1CLimitSingleInstance##QAE#XZ
#CLimitSingleInstance##QAEHXZ
Is anybody able to tell me why this is happening and how to fix it? Thanks in advance.
Dave
The closest matches I can find are: ??0CLimitSingleInstance##QAE#PA_W#Z
Use the undname.exe utility from the VS command line on that symbol:
C:\>undname ??0CLimitSingleInstance##QAE#PA_W#Z
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.
Undecoration of :- "??0CLimitSingleInstance##QAE#PA_W#Z"
is :- "public: __thiscall CLimitSingleInstance::CLimitSingleInstance(wchar_t *)"
Note the argument type, wchar_t*, not char*. Your lib project has UNICODE #defined, your exe project does not. The relevant setting is General + Character Set.
Seems like there is no CLimitSingleInstance::CLimitSingleInstance(char_t *). in your library. Have you checked your defines for both projects?
There is Microsoft utility: undname.exe which is part of VisualStudio:
C:\Program Files\Microsoft Visual Studio\COMMON\Tools>UNDNAME.EXE
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998
usage: UNDNAME [-f] decorated-names...
-f Undecorate fully. Default is to only undecorate the class::member
So the linker wants:
>UNDNAME.EXE -f ??0CLimitSingleInstance##QAE#PAD#Z
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998
>> ??0CLimitSingleInstance##QAE#PAD#Z == public: __thiscall CLimitSingleInstance::CLimitSingleInstance(char *)
and you have in the library:
>UNDNAME.EXE -f ??0CLimitSingleInstance##QAE#PA_W#Z
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998
>> ??0CLimitSingleInstance##QAE#PA_W#Z == public: __thiscall CLimitSingleInstance::CLimitSingleInstance(wchar_t *)
>UNDNAME.EXE -f ??1CLimitSingleInstance##QAE#XZ
Microsoft(R) Windows NT(R) Operating System
UNDNAME Version 5.00.1768.1Copyright (C) Microsoft Corp. 1981-1998
>> ??1CLimitSingleInstance##QAE#XZ == public: __thiscall CLimitSingleInstance::~CLimitSingleInstance(void)
Link errors like this sometimes occur when compiling C++ using Microsoft Visual C++ 6.0:
error LNK2001: unresolved external symbol __imp__close
error LNK2001: unresolved external symbol __imp__read
error LNK2001: unresolved external symbol __imp__lseek
error LNK2001: unresolved external symbol __imp__open
My fix, which I found after searching fruitlessly using Google for a long time, is this:
Do NOT disable language extensions. In the Project Settings dialog, in the C/C++ tab, make sure that the 'Disable language extensions' checkbox is not checked.
The functions close, read, lseek, open, etc., are not standard parts of the C library and the declarations are skipped by conditional compilation using #if !__STDC__ in <io.h>. This happens if you disable language extensions.
The answer is that the __imp prefix refers to function stubs in the OBJ library for the DLL version of the CRT. This means your linker options are incompatible with your compiler options (linker set to static CRT or no CRT and compiler set to dynamic CRT).
In your case rather than the CRT itself it is another library, but the same idea.