VS2015 Linking .dll/.so in C++ - c++

I have the following files (https://www.dropbox.com/s/2bfhylzb2evrggp/Lib.rar?dl=0)
It contains the header file, .so and .dll file.
I added the aditional paths under C/C++ and Linker (to the dll.s and .h files)
I have also used the sample code in the rar file and VS2015 picks up the code
include "swi32.h"
But as soon as I try to use a method defined in the headerfile I get the following
Severity Code Description Project File Line Suppression State
Error LNK1120 1 unresolved externals ConsoleApplication1 C:\Users\Donald Jansen\Desktop\CPPTest\Swi32Test\ConsoleApplication1\Debug\ConsoleApplication1.exe 1
Error LNK2019 unresolved external symbol _WiCreateRawImage#0 referenced in function _main ConsoleApplication1 C:\Users\Donald Jansen\Desktop\CPPTest\Swi32Test\ConsoleApplication1\Console.obj 1
I am not sure what I am missing

If you are implicitly linking the dll, then you must have also received a .lib file which you need to link with your console application. This should remove the unresolved symbol warning. The operating system loads the DLL when the executable using it is loaded.
If you are explicitly linking the dll, then your code should explictly load and unload the DLL to access the exported functions.
The .so you mention probably is a linux shared object file and not useful on windows platform. I may be wrong though.

Related

Tensorflow 2.3 unresolved external symbols in machine-generated files when building C++ project on Windows

I was wondering if anyone has had success linking against Tensorflow 2.3 DLL on Windows.
I am trying to integrate some tensorflow functionality in a tiny VS2019 test project to see if I can use tensorflow C++ API at all.
I managed to build tensorflow with MSVC 14.16.27023 and followed the instructions given in the official links:
https://docs.bazel.build/versions/master/windows.html#build-c
https://www.tensorflow.org/install/source_windows
I built .dll and .lib using the following Bazel commands:
bazel build --config=opt //tensorflow:tensorflow.dll
bazel build --config=opt //tensorflow:tensorflow.lib
VS2019 project properties:
add relevant Additional Include Directories
add Additional Library Directories, which is only path-to-tensorflow-source\bazel-bin\tensorflow, where bazel-bin directory is generated by the build system (ie Bazel)
in Linker->Input->Additional Dependencies add generated tensorflow.lib
The source code is tiny and is comprised of a few lines I looked up in Joe Antognini's example:
#pragma once
#define NOMINMAX
#include "tensorflow/core/public/session.h"
#include "tensorflow/cc/ops/standard_ops.h"
int main()
{
tensorflow::Scope root = tensorflow::Scope::NewRootScope();
auto X = tensorflow::ops::Placeholder(root.WithOpName("x"), tensorflow::DT_FLOAT,
tensorflow::ops::Placeholder::Shape({ -1, 2 }));
}
As explained in Ashley Tharp's guide, building right away results in linker errors complaining about unresolved external symbols:
LNK2001 unresolved external symbol "private: class tensorflow::Scope __cdecl tensorflow::Scope::WithOpNameImpl(...
LNK2001 unresolved external symbol "public: static class tensorflow::Scope __cdecl tensorflow::Scope::NewRootScope(void)"...
LNK2001 unresolved external symbol "public: __cdecl tensorflow::Scope::~Scope(void)"...
LNK2001 unresolved external symbol "public: __cdecl tensorflow::ops::Placeholder::Placeholder(...
Here is the explanation from her guide:
The reason this is happening is because you can only expose 60,000 symbols in a dll. This is just some limitation of the dll format. The tensorflow library code has more than 60000 symbols, so as the programmer building this dll (a dll is just a binary file for accessing a library at runtime) you will have to manually indicate which symbols you want exposed if they are not already. Google has chosen some default set, but it may not work for everyone.
And, as suggested in the guide, I went into tensorflow headers and prepended relevant symbols with TF_EXPORT macro, which is your usual DLL import-export pattern:
#ifdef TF_COMPILE_LIBRARY
#define TF_EXPORT __declspec(dllexport)
#else
#define TF_EXPORT __declspec(dllimport)
#endif // TF_COMPILE_LIBRARY
The above-mentioned workaround works for the first 3 errors:
// scope.h
~Scope(); // change to: TF_EXPORT ~Scope();
...
static Scope NewRootScope(); // change to: TF_EXPORT static Scope NewRootScope();
...
Scope WithOpNameImpl(const string& op_name) const; // change to TF_EXPORT Scope...
After I am done editing, I re-run bazel like so bazel build --config=opt //tensorflow:tensorflow.lib, and errors are gone.
However, the issue arises when I try to perform similar manipulation to fix the last remaining unresolved symbol, namely Placeholder. The symbol is located inarray_ops.h, which is a machine-generated file. So whatever I edit inside the file is overwritten and lost as soon as I try to build .lib.
Question: How can I expose symbols that appear in machine-generated files? Pointers in the right direction would be much appreciated, perhaps I'm missing something obvious.
The folder containing dynamic library tensorflow_cc.dll and import library tensorflow_cc.dll.if.lib has also two files:
tensorflow_filtered_def_file.def : contains import symbols
tensorflow_cc.dll-2.params : has all built libraries
now if you have some unresolved symbols when building your app, all you have to do is to rebuild dynamic-library with updated tensorflow_filtered_def_file.def file. To this file you have to add missing symbols, for your sample code, it is:
EXPORTS
??1Scope#tensorflow##QEAA#XZ
?NewRootScope#Scope#tensorflow##SA?AV12#XZ
??0Placeholder#ops#tensorflow##QEAA#AEBVScope#2#W4DataType#2#AEBUAttrs#012##Z
?WithOpNameImpl#Scope#tensorflow##AEBA?AV12#AEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z
the rest symbols ...
After you updated .def file, you rebuild dll by calling the following command:
link.exe /nologo /DLL /SUBSYSTEM:CONSOLE -defaultlib:advapi32.lib -DEFAULTLIB:advapi32.lib
-ignore:4221 /FORCE:MULTIPLE /MACHINE:X64
#bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_cc.dll-2.params /OPT:ICF /OPT:REF
/DEF:bazel-out/x64_windows-opt/bin/tensorflow/tensorflow_filtered_def_file.def /ignore:4070
before rebuilding dll/lib remove these files (they are only read-only).
More info here , see rafix07 comment.

Linker error while linking some windows APIs

I have a makefile project in my system. Recently, I added some new functions which makes use of the following Windows APIs:
RegOpenKeyEx
RegEnumKeyEx
RegCloseKey
RegGetValue
For having those APIS I added the windows.h header file as well.
The code compiles and links fine in my machine. But, linking fails in my colleagues machine. We all are working on 64 bit windows machine.
In his PC I get the error:
error LNK2001: unresolved external symbol __imp_RegOpenKeyExW
error LNK2001: unresolved external symbol __imp_RegGetValueW
error LNK2001: unresolved external symbol __imp_RegCloseKey
error LNK2001: unresolved external symbol __imp_RegEnumKeyExW
What I tried:
Since the library being used was Advapi32.lib in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64
I tried adding following line:
LINKFLAGS += -L "C:\Program Files (x86)\Microsoft
SDKs\Windows\v7.0A\Lib\x64"
I added the path of the library to environment variable PATH
I copied the lib to the output folder.
Nothing worked.
As I said earlier, the code works fine in my PC but fails in another one.
The registry functions require you to pass Advapi32.lib to the linker. This is the step that you have missed.
If you are using visual stdio, check your project property vs your friend's project property and check link library path+ library name with .lib shoul be included. It seems in your friends computer is not getting the library to resolve the symbols. Compile time checking is done only with header files. While linking it should find the definition of yor function in library.
for command line:
from:http://social.msdn.microsoft.com/Forums/vstudio/en-US/6bcae3d1-85b6-471d-a4ee-7b455b21460b/how-do-i-link-libraries-that-are-sitting-in-different-directories-from-the-command-line?forum=vcgeneral
cl main.obj ab1.lib ab2.lib de1.lib de2.lib gh1.lib /Fetestmain.exe /link /LIBPATH:C:\test\ab /LIBPATH:C:\test\de /LIBPATH:C:\test\gh
The "/link" is very important...do not use "/LINK", the uppercase LINK is not recognised.

How do I build and link libCurl in VS project

I am trying to use cURL in a C++ project I am working on in VS2010.
I downloaded the latest cURL source and I am building the solution included in the archive. When I build this project the only output lib appears to be "libcurld_imp.lib". I was expecting a "libcul.lib". In any case I have pointed VS to the include directory where the headers live and added the above mentioned lib to my linker's dependencies. Finally, I add the folder path of this "libcurld_imp.lib" to the links additional libraries search locations.
I cannot build my project because of the following linker error:
error LNK1104: cannot open file 'libcurld_imp.lib'
This file does exist in a folder that I added to the linker's additional libraries search path. What am I missing? Thanks
update
Ok.. So was able to get a libcurl.lib file by editing the libcurl proj. The default build settings were set to output a DLL. I changed this to as static library. Now that I have a libcurl.lib I am getting these linking errors:
Error 3 error LNK2019: unresolved external symbol _imp_curl_easy_setopt referenced in function _main
Error 2 error LNK2019: unresolved external symbol _imp_curl_easy_perform referenced in function _main
Anyone know how to link this friggen thing?
Another update
The documentation included in the source download includes a file, "build.windows". The directions say to build the lib using the following command:
nmake /f makefile.vc mode=
I have done this using "static" as my mode and "VC=10" as my option. This builds the library but it is called libcurl_a.lib. Linking to this lib gives the same errors :(
Thanks
the _imp suffix in libcurld_imp.lib most likely stands for Import Library. You are building a DLL and this file is the lib you need to link to your exe to call the DLL implicitely.
The solution should contain other configurations that will let you build a static library.
You need both to succeed. Try this suggestion (it's for vs2008 but should work):
http://curl.haxx.se/mail/lib-2009-05/0097.html

Unresolved external symbol

Help me guys,
I've been searching for a really long time.
I'm using visual studio 2010 and I tried to include an external lib but I get an unresolved external error.
The external project is composed of files in the following way
backend.cpp
//some functions
frontend.cpp
//some functions
header.h
I build this project using SCons then I includes the .lib file in the project and refered to its path and I copied the .h file to msvc directory .
VS seems to access and read the functions in backend.cpp(from the auto complete) but when I run the project it says that there is an unresolved external # the called function
I'm not a Visual Studio expert, but I guess that it being able to auto complete your code is associated with it finding the headers of your source code.
Unresolved external means that the compiler can't find the object files in the linking process. This could mean that you failed to add the lib files correctly, or that you are missing some .cpp file.
That may happen for a variety of reasons. But all of them end up being one of the following:
You are not linking the correct .lib file.
The symbol name you are using in your program is not identical to the one provided in the .lib.
The first one is easy enough to check, so please, double check it.
The second one is trickier. The symbol name used in your program is output in the error message "unresolved external symbol abc", or whatever. The symbols available in the library can be listed with the command: dumpbin /all name.lib.
If you cannot find the problem, please post the exact error you are getting and the output of the dumpbin program.

Debugging Visual Studio linker

I am trying to build an xll addin for Excel using appropriate framwork downloaded from MS website. I am getting the following linker errors:
1>FRAMEWRK.obj : error LNK2001: unresolved external symbol _Excel4
1>FRAMEWRK.obj : error LNK2001: unresolved external symbol _Excel4v#16
I think that these should be defined in XLCALL32.LIB included in the package from MS, but I don't know how to check it. Moreover, added the folder containing this library to the Additional Library Directories, without luck.
Hence my questions:
(1) How can I check what is in XLCALL32.LIB?
(2) How can I see which files the linker is using to see if my library is actually included?
In your project properties, Under Configuration Properties->Linker->Input, you will see 'Additional Dependencies' - these are the files the linker is using. Assuming that _Excel4 and _Excel4v are defined in XLCALL32.LIB (which I think they are), you simply need to ensure that this file is specified in this list.