[CMake] Linking an entire static library

Eric Noulard eric.noulard at gmail.com
Mon Sep 10 03:49:42 EDT 2012


2012/9/10 Petr Kmoch <petr.kmoch at gmail.com>:
> Hi all.
>
> I'm on Windows, and I need a DLL composed of multiple static
> libraries, any of which can define dllexport functions not otherwise
> referenced in the DLL. The problem is that the Visual Studio linker
> does not provide flags to link an entire static library (i.e. there is
> no equivalent to ld's --whole-archive). I tried /OPT:NOREF, it didn't
> help.
>
> The only solution I can think of is to link all of the static
> library's object files instead of the library itself (I verified
> manually that this works). I cannot put source files into the DLL
> directly, because the DLL is C and the static lib(s) are Fortran.

Why can't you do that?
I do not use fortran since a long time ago but I do not see why you can't do

add_library(mymixedllib foo.c blah.f90)


> My question is thus: is there a way to retrieve the list of a target's
> object files from cmake?

Since CMake 2.8.9 you can build "OBJECT" library,
see:
cmake --help-command add_library

[...]
         add_library(<name> OBJECT <src>...)

       creates a special "object library" target.  An object library compiles
       source files but does not archive or link their object files into a
       library.  Instead other targets created by add_library or
       add_executable may reference the objects using an expression of the
       form $<TARGET_OBJECTS:objlib> as a source, where "objlib" is the
       object library name.  For example:

         add_library(... $<TARGET_OBJECTS:objlib> ...)
         add_executable(... $<TARGET_OBJECTS:objlib> ...)

       will include objlib's object files in a library and an executable
       along with those compiled from their own sources.
[...]


>
> If there isn't any, I will resort to 'lib /list' and
> pre-build/post-build magic, but I'd prefer a simpler solution. Or if
> anyone happens to know a better way than linking all the object files,
> please share it. However, .def files are not an option.

that I don't know.


-- 
Erk
Le gouvernement représentatif n'est pas la démocratie --
http://www.le-message.org


More information about the CMake mailing list