MantisBT - CMake
View Issue Details
0015495CMakeCMakepublic2015-04-04 01:312015-11-02 09:13
Norbert Pfeiler 
 
normalminoralways
closedsuspended 
CMake 3.2.1 
 
0015495: cmake prevents linking static runtime with absolute path to a static lib
When an absolute path to a static lib i.e. ${ZLIB_LIBRARIES}, which expands to D:/msys64/mingw64/lib/libz.a is added to target_link_libraries, the library is linked using its name (-lz), preprended with -Wl,-Bstatic and appended with -Wl,-Bdynamic. Because the libraries appear after any link flags, the latter one cancels out any flags to link statically (-static -static-libstdc++ -static-libgcc) and the final executable links against the dynamic runtime.
Using an imported target i.e. ZLIB::ZLIB, the library is added using its absolute path without any additional flags.
add static link flags
add static lib via its absolute path
the final executable links against the dynamic runtime
[1/4] D:\msys64\mingw64\bin\c++.exe -MMD -MT CMakeFiles/static_lib_test1.dir/main.cpp.obj -MF CMakeFiles/static_lib_test1.dir/main.cpp.obj.d -o CMakeFiles/static_lib_test1.dir/main.cpp.obj -c ../main.cpp
[2/4] D:\msys64\mingw64\bin\c++.exe -isystem D:/msys64/mingw64/include -MMD -MT CMakeFiles/static_lib_test2.dir/main.cpp.obj -MF CMakeFiles/static_lib_test2.dir/main.cpp.obj.d -o CMakeFiles/static_lib_test2.dir/main.cpp.obj -c ../main.cpp
[3/4] cmd.exe /C "cd . && D:\msys64\mingw64\bin\c++.exe CMakeFiles/static_lib_test1.dir/main.cpp.obj -o static_lib_test1.exe -Wl,--out-implib,libstatic_lib_test1.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -static -static-libstdc++ -static-libgcc -Wl,-Bstatic -lz -Wl,-Bdynamic -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
[4/4] cmd.exe /C "cd . && D:\msys64\mingw64\bin\c++.exe CMakeFiles/static_lib_test2.dir/main.cpp.obj -o static_lib_test2.exe -Wl,--out-implib,libstatic_lib_test2.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -static -static-libstdc++ -static-libgcc D:/msys64/mingw64/lib/libz.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
static_lib_test1.exe:
    ntdll.dll => /c/Windows/SYSTEM32/ntdll.dll (0x777d0000)
    kernel32.dll => /c/Windows/system32/kernel32.dll (0x776b0000)
    KERNELBASE.dll => /c/Windows/system32/KERNELBASE.dll (0x7fefd7b0000)
    msvcrt.dll => /c/Windows/system32/msvcrt.dll (0x7fefde30000)
    libstdc++-6.dll => /mingw64/bin/libstdc++-6.dll (0x6fc40000)
    libgcc_s_seh-1.dll => /mingw64/bin/libgcc_s_seh-1.dll (0x61440000)
    libwinpthread-1.dll => /mingw64/bin/libwinpthread-1.dll (0x64940000)
    USER32.dll => /c/Windows/system32/USER32.dll (0x775b0000)
    GDI32.dll => /c/Windows/system32/GDI32.dll (0x7fefe110000)
    LPK.dll => /c/Windows/system32/LPK.dll (0x7feffad0000)
    USP10.dll => /c/Windows/system32/USP10.dll (0x7fefd950000)
    IMM32.DLL => /c/Windows/system32/IMM32.DLL (0x7fefde00000)
    MSCTF.dll => /c/Windows/system32/MSCTF.dll (0x7fefdca0000)
static_lib_test2.exe:
    ntdll.dll => /c/Windows/SYSTEM32/ntdll.dll (0x777d0000)
    kernel32.dll => /c/Windows/system32/kernel32.dll (0x776b0000)
    KERNELBASE.dll => /c/Windows/system32/KERNELBASE.dll (0x7fefd7b0000)
    msvcrt.dll => /c/Windows/system32/msvcrt.dll (0x7fefde30000)
No tags attached.
7z cmake static lib test.7z (639) 2015-04-04 01:31
https://public.kitware.com/Bug/file/5420/cmake%20static%20lib%20test.7z
Issue History
2015-04-04 01:31Norbert PfeilerNew Issue
2015-04-04 01:31Norbert PfeilerFile Added: cmake static lib test.7z
2015-04-06 08:31Brad KingNote Added: 0038406
2015-04-06 08:32Brad KingStatusnew => resolved
2015-04-06 08:32Brad KingResolutionopen => suspended
2015-04-07 09:03Brad KingNote Added: 0038462
2015-11-02 09:13Robert MaynardNote Added: 0039759
2015-11-02 09:13Robert MaynardStatusresolved => closed

Notes
(0038406)
Brad King   
2015-04-06 08:31   
CMake converts "/path/to/system/lib/libfoo.a" into "-lfoo" when it is in a system path because some compiler front-ends substitute alternatives under the hood. This historical behavior may some day be revised, but that is expected for now.

The workaround is to always use an IMPORTED library target.
(0038462)
Brad King   
2015-04-07 09:03   
These may also be helpful:

 http://www.cmake.org/cmake/help/v3.2/prop_tgt/LINK_SEARCH_END_STATIC.html [^]
 http://www.cmake.org/cmake/help/v3.2/prop_tgt/LINK_SEARCH_START_STATIC.html [^]
(0039759)
Robert Maynard   
2015-11-02 09:13   
Closing resolved issues that have not been updated in more than 4 months.