IMPORTED DLL-only Target on MSVC

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

IMPORTED DLL-only Target on MSVC

Marek Vojtko (Firaxis)
Hi,

Is it not possible to wrap an external DLL-only library in an IMPORTED target?

I have an external dependency that is a single DLL (shared library) and a header file, but no LIB (static library) file. When I create an IMPORTED target and depend on it via target_link_libraries() I run into linkage issues, because on MSVC CMake puts the DLL into the linker command (the "Input" text field in the "Linker" settings of the Project Properties).

add_library( MyDependency::MyDependency MODULE IMPORTED )
set_target_properties( MyDependency::MyDependency
    PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${MyDependency_INCLUDE_DIR}"
        IMPORTED_LOCATION "${MyDependency_SHARED_LIB}"
)
[..]
add_executable( MyExecutable ... )
target_link_libraries( MyExecutable PRIVATE MyDependency::MyDependency )

I tried changing the library type in my add_library() call:
- STATIC obviously didn't work, because I don't have a .lib file and the .dll file in IMPORTED_LOCATION was added to MyExecutable's linker line.
- SHARED did not work because CMake expects a .dll in IMPORTED_LOCATION and a .lib in IMPORTED_IMP_LOCATION, so MyExecutable's linker line had a MyDependency::MyDependency-NOTFOUND entry in it.
- MODULE seemed like the right choice when reading the documentation, but MyExecutable's linker line still contains the .dll file specified in IMPORTED_LOCATION.

What am I doing wrong?

Thanks,
Marek

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: IMPORTED DLL-only Target on MSVC

Stuermer, Michael  SP/HZA-ZSEP
> -----Ursprüngliche Nachricht-----
> Von: CMake [mailto:[hidden email]] Im Auftrag von Marek
> Vojtko (Firaxis)
> Gesendet: Dienstag, 3. April 2018 00:49
> An: [hidden email]
> Betreff: [CMake] IMPORTED DLL-only Target on MSVC
>
> Hi,
>
> 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. When I create an IMPORTED target

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.

> and depend on it via target_link_libraries() I run into linkage issues, because
> on MSVC CMake puts the DLL into the linker command (the "Input" text field
> in the "Linker" settings of the Project Properties).
>
> add_library( MyDependency::MyDependency MODULE IMPORTED )
> set_target_properties( MyDependency::MyDependency
>     PROPERTIES
>         INTERFACE_INCLUDE_DIRECTORIES "${MyDependency_INCLUDE_DIR}"
>         IMPORTED_LOCATION "${MyDependency_SHARED_LIB}"
> )
> [..]
> add_executable( MyExecutable ... )
> target_link_libraries( MyExecutable PRIVATE
> MyDependency::MyDependency )
>
> I tried changing the library type in my add_library() call:
> - STATIC obviously didn't work, because I don't have a .lib file and the .dll file
> in IMPORTED_LOCATION was added to MyExecutable's linker line.
> - SHARED did not work because CMake expects a .dll in
> IMPORTED_LOCATION and a .lib in IMPORTED_IMP_LOCATION, so
> MyExecutable's linker line had a MyDependency::MyDependency-
> NOTFOUND entry in it.
> - MODULE seemed like the right choice when reading the documentation,
> but MyExecutable's linker line still contains the .dll file specified in
> IMPORTED_LOCATION.
>
> What am I doing wrong?
>
> Thanks,
> Marek
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: IMPORTED DLL-only Target on MSVC

Mueller-Roemer, Johannes Sebastian
In reply to this post by Marek Vojtko (Firaxis)
From the CMake side of things yes, but MSVC requires an import library. You could use a tool such as Digital Mars' implib tool (https://digitalmars.com/ctg/implib.html), or use dumpbin to list all exports, put those in a .def file and use lib to create an import library from the def file. If I am not mistaken, CMake's GNUtoMS switch (for the Mingw Makefiles generator) does something like this automatically, so you could have a look at how it is done there. But I assume it's simply parsing the dumpbin /exports output to write a def file and running lib afterwards.

Regards,
Johannes

Fraunhofer-Institut für Graphische Datenverarbeitung IGD
Fraunhoferstr. 5  |  64283 Darmstadt  |  Germany
Tel +49 6151 155-606  |  Fax +49 6151 155-139
[hidden email] | www.igd.fraunhofer.de

-----Original Message-----
From: CMake <[hidden email]> On Behalf Of Marek Vojtko (Firaxis)
Sent: Tuesday, April 3, 2018 00:49
To: [hidden email]
Subject: [CMake] IMPORTED DLL-only Target on MSVC

Hi,

Is it not possible to wrap an external DLL-only library in an IMPORTED target?

I have an external dependency that is a single DLL (shared library) and a header file, but no LIB (static library) file. When I create an IMPORTED target and depend on it via target_link_libraries() I run into linkage issues, because on MSVC CMake puts the DLL into the linker command (the "Input" text field in the "Linker" settings of the Project Properties).

add_library( MyDependency::MyDependency MODULE IMPORTED ) set_target_properties( MyDependency::MyDependency
    PROPERTIES
        INTERFACE_INCLUDE_DIRECTORIES "${MyDependency_INCLUDE_DIR}"
        IMPORTED_LOCATION "${MyDependency_SHARED_LIB}"
)
[..]
add_executable( MyExecutable ... )
target_link_libraries( MyExecutable PRIVATE MyDependency::MyDependency )

I tried changing the library type in my add_library() call:
- STATIC obviously didn't work, because I don't have a .lib file and the .dll file in IMPORTED_LOCATION was added to MyExecutable's linker line.
- SHARED did not work because CMake expects a .dll in IMPORTED_LOCATION and a .lib in IMPORTED_IMP_LOCATION, so MyExecutable's linker line had a MyDependency::MyDependency-NOTFOUND entry in it.
- MODULE seemed like the right choice when reading the documentation, but MyExecutable's linker line still contains the .dll file specified in IMPORTED_LOCATION.

What am I doing wrong?

Thanks,
Marek

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: IMPORTED DLL-only Target on MSVC

Marek Vojtko (Firaxis)
In reply to this post by Marek Vojtko (Firaxis)
> > 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

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: IMPORTED DLL-only Target on MSVC

Stuermer, Michael  SP/HZA-ZSEP
> -----Ursprüngliche Nachricht-----
> Von: CMake [mailto:[hidden email]] Im Auftrag von Marek
> Vojtko (Firaxis)
> Gesendet: Dienstag, 3. April 2018 23:24
> An: [hidden email]
> Betreff: Re: [CMake] IMPORTED DLL-only Target on MSVC
>
> > > 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():

Ok, I missed this case :-). But in my opinion MyDependency.dll is not a TARGET in
the sense of CMake targets. It's just a variable/filename which is used in the project
somewhere. This is actually also how Visual Studio treats the .dll: you only pass
the filename somewhere, not mentioning "link to this" or something else.

If you want to deploy/copy the .dll to your binary directory I'd simply add a
POST_BUILD custom command which copies the file over so you don't have to
do it manually.

>
> 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.

Adding include directories/files only via target could be done by putting the
Include files in a INTERFACE library. Not sure if this is an elegant solution for
your case but I'd probably start like this.

>
> 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
>
> --
>
> Powered by www.kitware.com
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Kitware offers various services to support the CMake community. For more
> information on each offering, please visit:
>
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Follow this link to subscribe/unsubscribe:
> https://cmake.org/mailman/listinfo/cmake
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake