View Issue Details Jump to Notes ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0015495CMakeCMakepublic2015-04-04 01:312015-11-02 09:13
ReporterNorbert Pfeiler 
Assigned To 
PrioritynormalSeverityminorReproducibilityalways
StatusclosedResolutionsuspended 
PlatformOSOS Version
Product VersionCMake 3.2.1 
Target VersionFixed in Version 
Summary0015495: cmake prevents linking static runtime with absolute path to a static lib
DescriptionWhen 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.
Steps To Reproduceadd static link flags
add static lib via its absolute path
the final executable links against the dynamic runtime
Additional Information[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)
TagsNo tags attached.
Attached Files7z file icon cmake static lib test.7z [^] (639 bytes) 2015-04-04 01:31

 Relationships

  Notes
(0038406)
Brad King (manager)
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 (manager)
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 (manager)
2015-11-02 09:13

Closing resolved issues that have not been updated in more than 4 months.

 Issue History
Date Modified Username Field Change
2015-04-04 01:31 Norbert Pfeiler New Issue
2015-04-04 01:31 Norbert Pfeiler File Added: cmake static lib test.7z
2015-04-06 08:31 Brad King Note Added: 0038406
2015-04-06 08:32 Brad King Status new => resolved
2015-04-06 08:32 Brad King Resolution open => suspended
2015-04-07 09:03 Brad King Note Added: 0038462
2015-11-02 09:13 Robert Maynard Note Added: 0039759
2015-11-02 09:13 Robert Maynard Status resolved => closed


Copyright © 2000 - 2018 MantisBT Team