Home > Software design >  How do I create a static library (.lib) which depends on the dynamic (.dll) tensorflow library?
How do I create a static library (.lib) which depends on the dynamic (.dll) tensorflow library?

Time:09-17

I have created a static C library in Visual Studio 2019 on Windows 10 which depends on the tensorflow library, which is dynamic (.dll). My library, lets call it A.lib, contains a function which takes data, pass it to a tensorflow model and returns the model's output. The compilation seems to work well and creates an A.lib file.

Now I want to use my static library in another project to create an .exe. Lets call it B. I copied the header A.h and the A.lib into the B project and adapt the project properties so that my library can be found.

The problem is that I get LNK2001 errors, because the linker can not find the definitions of the tensorflow functions which I call in my A.lib.

I tried to copy the tensorflow lib into my project B as well. But that did not help.

What do I have to do to include the libraries correctly? Or is there a simpler alternative to deploy a convolutional neural network in C?

CodePudding user response:

Here's a [SO]: How to create a Minimal, Reproducible Example (reprex (mcve)).

dll00.h:

#if defined(_WIN32)
#  if defined(DLL00_EXPORTS)
#    define DLL00_EXPORT_API __declspec(dllexport)
#  else
#    define DLL00_EXPORT_API __declspec(dllimport)
#  endif
#else
#  define DLL00_EXPORT_API
#endif


#if defined(__cplusplus)
extern "C" {
#endif

DLL00_EXPORT_API int dll00Func00();

#if defined(__cplusplus)
}
#endif

dll00.c:

#define DLL00_EXPORTS
#include "dll00.h"

#include <stdio.h>


int dll00Func00() {
    printf("%s - %d - %s\n", __FILE__, __LINE__, __FUNCTION__);
    return -3;
}

lib00.h:

#if defined(__cplusplus)
extern "C" {
#endif

int lib00Func00();

#if defined(__cplusplus)
}
#endif

lib00.c:

#include "lib00.h"

#include "dll00.h"

#include <stdio.h>


int lib00Func00() {
    printf("%s - %d - %s\n", __FILE__, __LINE__, __FUNCTION__);
    return dll00Func00() - 3;
}

main00.c:

#include "lib00.h"

#include <stdio.h>


int main() {
    printf("%s - %d - %s\n", __FILE__, __LINE__, __FUNCTION__);
    int res = lib00Func00();
    printf("Lib func returned: %d\n", res);
    printf("\nDone.\n");
    return 0;
}

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q069197545]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]> "c:\Install\pc032\Microsoft\VisualStudioCommunity\2019\VC\Auxiliary\Build\vcvarsall.bat" x64 >nul

[prompt]> dir /b
dll00.c
dll00.h
lib00.c
lib00.h
main00.c

[prompt]> :: Build .dll (1 step)
[prompt]> cl /nologo /MD /DDLL dll00.c  /link /NOLOGO /DLL /OUT:dll00.dll
dll00.c
   Creating library dll00.lib and object dll00.exp

[prompt]> :: Build .lib (2 steps)
[prompt]> cl /c /nologo /MD /Folib00.obj lib00.c
lib00.c

[prompt]> lib /NOLOGO /OUT:lib00.lib lib00.obj

[prompt]> :: Build .exe (1 step)
[prompt]> cl /nologo /MD /W0 main00.c  /link /NOLOGO /OUT:main00_pc064.exe lib00.lib dll00.lib
main00.c

[prompt]> dir /b
dll00.c
dll00.dll
dll00.exp
dll00.h
dll00.lib
dll00.obj
lib00.c
lib00.h
lib00.lib
lib00.obj
main00.c
main00.obj
main00_pc064.exe

[prompt]> main00_pc064.exe
main00.c - 7 - main
lib00.c - 9 - lib00Func00
dll00.c - 8 - dll00Func00
Lib func returned: -6

Done.

So, it works (at least this trivial example).
As seen, when building the .exe I also passed the .dll's .lib to the linker (meaning that the .dll (together with all its (recurring) dependents) is required at runtime). For info on how to do it on the VStudio project, check [SO]: How to include OpenSSL in Visual Studio (@CristiFati's answer).

  • Related