I have researched more into the problem and posted a more detailed question with my findings here: Rust, how to use global variable from DLL? C++ equivalent requires __declspec(dllimport)
Original question:
Summary:
When linking my project with MSVC 2019's link.exe, I am getting errors such as unresolved external symbol jl_module_type. These symbols are defined in a file julia.lib, which I have verified using dumpbin /exports julia.lib. This file is passed as an argument to link.exe, and yet, it still complains about unresolved symbols. It looks like all the symbols that failed to be linked are variables rather than functions.
More information:
julia.lib has been renamed from libjulia.dll.a, and it corresponds to another file libjulia.dll. They were built with Cygwin/MinGW, but AFAIK this should not affect things. The actual project this is being used in is written in Rust, so link.exe is being invoked automatically by Rust's cargo tool. It is configured to build my project as a DLL.
Related
I have an application that links a static lib both build with visual studio (same version, same machine).
This lib contains multiple c files with mutliple functions (as usual :)
as an example
lib1.lib
file1.c
func_f1_1
func_f1_2
file2.c
func_f2_1
func_f2_2
prog1.exe
(links lib1.lib)
main.cpp
uses func_f2_2
strange.c
func_f1_1 (same signature, different definition)
func_f2_1 (same signature, different definition)
lib1 is independently build from prog1.exe
During link time the compiler complains about
lib1.lib(file2.obj): error LNK2005: _func_f2_1 already defined in strange.obj
The linker does not complain about func_f1_1!
I do not fully understand this kind of error since i expected that the linker only grabs symbols from a library that are not defined in the target executable.
It seems to me that the linker tries to put in the content of the whole obj from the lib instead just the function.
Are there any project settings that can i change to get the linker working on a function level? "function level linking" is activated in the compiler properties of both the lib and the program project.
If your program uses func_f2_2 then the linker adds the object file lib1:file2.o that defines func_f2_2 to the object list. All symbols in that object file are linked in to the program. This includes all variables that might be used by func_f2_2 as well as func_f2_1.
The linker doesn't complain about func_f1_1 since there was no reason to load the object file lib1:file1.o
If you need to replace func_f2_1 you have also to add the definition of func_f2_2 in strange.c.
I have a tlb files that contains some function declaration that I need to use.
If I use
#import "type_library.tlb"
I can correclty reference the function from my code:
tlb_namespace::required_function();
But when I compile the project the linker says that tlb_namespace::required_function is an unresolved external symbol.
How can I succesfully build this kind of project?
EDIT:
I have used the same type library in a Dummy VBA access project. I have added the reference to the type library and I have noticed that some of the function contained in the type library are correctly called. But some of them are not. VBA says It can't locate their entry point in the related dll.
Can this explain the unresolved external symbol when building c++ app?
I have also noticed that the failing function are declared in the tlb like this:
UPPER_function_name
but in the dll are declared like this:
Upper_function_name
Can this be the issue?
Is it possible to solve this kind of error directly modifying the binary tlb file or dll?
Use IDE to view TLB information.
Use this help : How to: View Type Library Information
At IDE : View-> Object Browser, click "..." Edit Custom Component Set, browse your TLB file and Add to view information.
Confirm namespace used for.
use the namespace to resolve the linker error:
example:
#import "<>" raw_interfaces_only
using namespace <>
this will resolve the problem
I already included Rpc.h, but I am still receiving error LNK2019: unresolved external symbol __im__RpcServerListen#12 referenced in function _main. Is there anything else I need to do to be able to call this function?
It's not enough to include the header file, that just tells the compiler about the types, functions and so on, that you want to use.
You also have to link with the relevant object files or libraries since they contain the actual code that you will be calling. You can tell this because the error starts with LNK, meaning you have a linker issue rather than a compiler one.
The actual files that you need to link to should be specified for the linking phase. For Windows RPC, this appears to be rpcrt4.lib (see here).
I needed help making sense out of the following LNK error which i am getting.
SwitchFunctions error LNK2019: unresolved external symbol "__declspec(dllimport) ??$?8DU?$char_traits#D#std##V?$allocator#D#1##std##YA_NABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##0#PBD#Z" (__imp_??$?8DU?$char_traits#D#std##V?$allocator#D#1##std##YA_NABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##0#PBD#Z) referenced in function "??$find#Viterator#?$vector#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##std##PBD#std##YA?AViterator#?$vector#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##0#V120#0ABQBD#Z" (??$find#Viterator#?$vector#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##std##PBD#std##YA?AViterator#?$vector#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##0#V120#0ABQBD#Z)
Have you checked whether you have included all necessary dlls or lib files in your project settings?
Such an error usually indicate that you have used a function which definition is found, but implementation is not. Usually in the case of external libraries, the implementation is inside a dll or a static lib. Failure to provide the dll or the static lib will cause the linking to fail.
Which version of Visual Studio are you using? This might be relevant for VS2005:
Link error using a CString derived class from a DLL
I am getting the following error.
error LNK2001: unresolved external symbol __localtime64_s
On searching on the internet i found that there is a library WINMM.LIB which is needed to be included. So i added this library in the project options.
But,it still does not help.
Can somebody help in this?
The _s suffix indicates that its actually probably one of the "safe" apis added to Visual Studio 2005 or 2008. There are two leading _'s - the first indicates the function decoration - an extern "C" function using the __cdecl calling convention. The next '_' inidcates that the functions is an OEM (Microsoft in this case) extension to the c-runtime library.
Specifically it seems to be a worker function invoked from 'Program Files\Visual Studio\VS\include\time.inl' when the safe 'localtime' function is called.
So, at a guess I would say that you are not linking in the c-runtime libraries correctly into your application. Are you using any options like "exclude default libraries" or are excluding any specific libraries in your projects settings?