[CMake] IMPORTED DLL-only Target on MSVC

Marek Vojtko (Firaxis) Marek.Vojtko at firaxis.com
Tue Apr 3 17:23:47 EDT 2018


> > Is it not possible to wrap an external DLL-only library in an IMPORTED target?
> > 
> 
> Yes it is.
> 
> > I have an external dependency that is a single DLL (shared library) and a
> > header file, but no LIB (static library) file.
> 
> How does this work in Visual Studio C++? If I am not completely mistaken you
> ALWAYS need a .lib file along with your .dll to link to the shared library. And you
> Need a header with the API as well of course.
> 
> The only case I know with .dll and without .lib file is when you have managed
> C++ or C# targets.
>

The DLL gets loaded using LoadLibrary("MyDependency.dll") at runtime and its functions are accessed through GetProcAddress():

HINSTANCE hLib = LoadLibrary(DLL_NAME);
If(hLib)
{
    typedef int (*DLL_FUNC)();
    myFunc = (DLL_FUNC)GetProcAddress(hLib, "DLLFunction");
}

Like I said, it's an external dependency and I don't have any control over it. The dependency's include directories have to be added to my executable's include directories and I need to copy the DLL next to the executable. I thought I'd solve (at least the include directories) by creating an IMPORTED target, but no matter what I try, the DLL ends up in the linker command line, wreaking havoc.

Of course I can work around this issue by not creating an IMPORTED target and just relying on two variables (MyDependency_INCLUDE_DIR and MyDependency_SHARED_LIB). Before I do, however, I wanted to make sure that CMake does not in fact support a DLL-and-header-only library set up.

Thanks,
Marek



More information about the CMake mailing list