Clang64 on Windows using Mingw64 yields many linking errors?

Hi again,

I am regularly building my small STL-enabled C++ project using Mingw64 on Windows x64 and the program runs fine (it builds and runs fine on x64 Linux too).

Then I got brave and decided to try out Clang, because I’d eventually like to take a look at the quality of the generated code, and then did the following:

  1. Set CC to clang.
  2. Set CXX to clang++.
  3. Run cmake and let it figure out that its using Clang on Windows.
  4. Build and realize that the link doesn’t work because CMake pulls in the wrong libraries (per default it uses Microsoft naming conventions: foo.lib instead of libfoo.a).
  5. Search good old Google and find this VERY helpful article: http://thread.gmane.org/gmane.comp.programming.tools.cmake.user/42398
  6. Fix CMake as per the above.
  7. Build and realize that the CMake pulls in the right libraries but still a torrent of errors are reported:

…\libBackend.a(Backend.cpp.obj):fake:(.rdata$ZTIN10Braceless07BackendE+0xffffffffff6e88a0): Cannot get section contents - auto-impo
rt exception
…\libBackend.a(Backend.cpp.obj):fake:(.data+0xfffffffffffffd00): Cannot get section contents - auto-import exception
…\libDriver.a(Setup.cpp.obj):fake:(.text$ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPSsS1_EERKSs+0xffffffffffffb34d): Cannot get section contents - auto-import exception
…\libDriver.a(Setup.cpp.obj):fake:(.text$ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPSsS1_EERKSs+0xffffffffffffb42e): Cannot get section contents - auto-import exception
…\libDriver.a(Setup.cpp.obj):fake:(.text$ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPSsS1_EERKSs+0xffffffffffffb458): Cannot get section contents - auto-import exception
…\libDriver.a(Setup.cpp.obj):fake:(.text$ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPSsS1_EERKSs+0xffffffffffffb59a): Cannot get section contents - auto-import exception
…\libDriver.a(Setup.cpp.obj):fake:(.text$ZNSt6vectorISsSaISsEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPSsS1_EERKSs+0xffffffffffffb5c5): Cannot get section contents - auto-import exception
…\libDriver.a(Setup.cpp.obj):fake:(.text$ZNSt20__uninitialized_copyILb0EE13__uninit_copyIPSsS2_EET0_T_S4_S3_+0xffffffffffff6203): Cannot get section contents - auto-import exception

Is it because Clang does not support the Mingw64 ABI? I was under the impression that Clang DID support the Mingw32/64 toolchains, but perhaps it is only the 32-bit version?

Cheers,
Mikael
– Love Thy Frog!

Hello

First of all, note the "llvm" in the mailing list title. Clang
questions should be asked in other ML.

3. Run cmake and let it figure out that its using Clang on Windows.
4. Build and realize that the link doesn't work because CMake pulls in the
wrong libraries (per default it uses Microsoft naming conventions: foo.lib
instead of libfoo.a).

It seems like you're not targeting mingw32, but MS toolchains. Stuff
won't work smooth here and major WIP.
Everything else below can be ignored.

Hello

First of all, note the “llvm” in the mailing list title. Clang
questions should be asked in other ML.

(Cross-posting to cfe-dev)

  1. Run cmake and let it figure out that its using Clang on Windows.
  2. Build and realize that the link doesn’t work because CMake pulls in the
    wrong libraries (per default it uses Microsoft naming conventions: foo.lib
    instead of libfoo.a).

It seems like you’re not targeting mingw32, but MS toolchains. Stuff
won’t work smooth here and major WIP.
Everything else below can be ignored.

I don’t think that’s the issue here. CMake happily tries to link with the mingw libs, but as pointed out in that CMake ML post, the CMake scripts do not know how to pass GCC-style flags to Clang on Windows. Mikael, can you post the link line that CMake generates (make VERBOSE=1), preferably to just cfe-dev?

I don't think that's the issue here. CMake happily tries to link with the
mingw libs,

Right, but it seems via MS link...

Ouch, sorry, forgot about that other list. I’ll repost my question there. I am NOT using the MS toolchain, though.

Cheers,
Mikael
– Love Thy Frog!

2012/6/11 Anton Korobeynikov <anton@korobeynikov.info>

Nope, I didn’t include the part where it says “very long path pointing to Mingw64 ld.exe”. So it is not related to MS tools at all. In fact, the MS tools are not even in the path.

Here’s the linker command that CMake uses:

c:\Synology\clang-3.2-win64\bin\clang++.exe -O3 -DNDEBUG CMakeFiles/braceless0.dir/Braceless.cpp.obj -o …\braceless0.exe -Lc:\Synology\clang-3.2-WIN64\lib …\libAST.a …\libCompiler.a …\libDriver.a …\libBackend.a …\libFrontend.a …\libToolbox.a -lLLVMAsmParser -lLLVMInstrumentation -lLLVMLinker -lLLVMArchive -lLLVMBitReader -lLLVMDebugInfo -lLLVMJIT -lLLVMVectorize -lLLVMBitWriter -lLLVMTableGen -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86Desc -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMInterpreter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMMCJIT -lLLVMRuntimeDyld -lLLVMExecutionEngine -lLLVMTarget -lLLVMMC -lLLVMObject -lLLVMCore -lLLVMSupport -lDbgHelp -lPsapi -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

I must admit that I don't know how to get Clang to display what linker command it uses when asked to perform the link. The linker, ld.exe, is located in the Mingw64 directory as expected.

Cheers,
Mikael
– Love Thy Frog!

2012/6/11 Anton Korobeynikov <anton@korobeynikov.info>

Nope, I didn’t include the part where it says “very long path pointing to Mingw64 ld.exe”. So it is not related to MS tools at all. In fact, the MS tools are not even in the path.

Here’s the linker command that CMake uses:

c:\Synology\clang-3.2-win64\bin\clang++.exe -O3 -DNDEBUG CMakeFiles/braceless0.dir/Braceless.cpp.obj -o …\braceless0.exe -Lc:\Synology\clang-3.2-WIN64\lib …\libAST.a …\libCompiler.a …\libDriver.a …\libBackend.a …\libFrontend.a …\libToolbox.a -lLLVMAsmParser -lLLVMInstrumentation -lLLVMLinker -lLLVMArchive -lLLVMBitReader -lLLVMDebugInfo -lLLVMJIT -lLLVMVectorize -lLLVMBitWriter -lLLVMTableGen -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86Desc -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -lLLVMInterpreter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMMCJIT -lLLVMRuntimeDyld -lLLVMExecutionEngine -lLLVMTarget -lLLVMMC -lLLVMObject -lLLVMCore -lLLVMSupport -lDbgHelp -lPsapi -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32

I must admit that I don't know how to get Clang to display what linker command it uses when asked to perform the link. The linker, ld.exe, is located in the Mingw64 directory as expected.

Add -v to your CMAKE_CXX_FLAGS.

Here goes:

Linking CXX executable …\braceless0.exe
clang version 3.2 (trunk 157524)
Target: x86_64-w64-mingw32
Thread model: posix
“c:/Mingw64/bin/x86_64-w64-mingw32-g++.exe” -v -O3 -D NDEBUG “-Lc:\Synology\clang-3.2-WIN64\lib” -m64 -o “…\braceless0.exe” CM
akeFiles/braceless0.dir/Braceless.cpp.obj “…\libAST.a” “…\libCompiler.a” “…\libDriver.a” “…\libBackend.a” “…\libFrontend.a” “…\libToolbox.a” -lLLVMAsmParser -lLLVMInstrumentation -lLLVMLinker -lLLVMArchive -lLLVMBitReader -lLLVMDebugInfo -lLLVMJIT -lLLVMVectorize -LLVMBitWriter -lLLVMTableGen -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86Desc -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -LLVMInterpreter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMMCJIT -LLVMRuntimeDyld -lLLVMExecutionEngine -lLLVMTarget -lLLVMMC -lLLVMObject -lLLVMCore -lLLVMSupport -lDbgHelp -lPsapi -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32
Using built-in specs.
COLLECT_GCC=c:/Mingw64/bin/x86_64-w64-mingw32-g++.exe
COLLECT_LTO_WRAPPER=c:/mingw64/bin/…/libexec/gcc/x86_64-w64-mingw32/4.7.1/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: /home/drangon/work/mingw-w64-dgn/source/gcc/configure --host=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --disab
le-nls --enable-languages=c,c++,objc,obj-c++ --with-gmp=/home/drangon/work/mingw-w64-dgn/build/for_target --enable-twoprocess --disa
ble-libstdcxx-pch --disable-win32-registry --prefix=/home/drangon/work/mingw-w64-dgn/target --with-sysroot=/home/drangon/work/mingw-
w64-dgn/target
Thread model: win32
gcc version 4.7.1 20120524 (prerelease) (GCC)

4-w64-mingw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/bin/
LIBRARY_PATH=c:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/;c:/mingw64/bin/…/lib/gcc/;c:/mingw64/bin/…/lib/gcc/x86_64-w64-min
gw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/lib/…/lib/;c:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/lib/;c:/mingw64
/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/lib/;c:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/.
./…/
COLLECT_GCC_OPTIONS=’-v’ ‘-O3’ ‘-D’ ‘NDEBUG’ ‘-Lc:\Synology\clang-3.2-WIN64\lib’ ‘-m64’ ‘-o’ ‘…\braceless0.exe’ ‘-shared-libgcc’ ‘-
mtune=generic’ ‘-march=x86-64’
c:/mingw64/bin/…/libexec/gcc/x86_64-w64-mingw32/4.7.1/collect2.exe --sysroot=/home/drangon/work/mingw-w64-dgn/target -m i386pep -Bdynamic -o …\braceless0.exe c:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/lib/…/lib/crt2.o c:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/lib/…/lib/crtbegin.o -Lc:\Synology\clang-3.2-WIN64\lib -c:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1 -Lc:/mingw64/bin/…/lib/gcc -Lc:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/lib/…/lib -Lc:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/lib -Lc:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/lib -Lc:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/… CMakeFiles/braceless0.dir/Braceless.cpp.obj …\libAST.a …\libCompiler.a …\libDriver.a …\libBackend.a …\libFrontend.a …\libToolbox.a -lLLVMAsmParser -lLLVMInstrumentation -lLLVMLinker -lLLVMArchive -lLLVMBitReader -lLLVMDebugInfo -lLLVMJIT -LLVMVectorize -lLLVMBitWriter -lLLVMTableGen -lLLVMCppBackendCodeGen -lLLVMCppBackendInfo -lLLVMX86CodeGen -lLLVMSelectionDAG -lLLVMAsmPrinter -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86Desc -lLLVMX86Info -lLLVMX86AsmPrinter -lLLVMX86Utils -LLVMInterpreter -lLLVMCodeGen -lLLVMScalarOpts -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMMCJIT -LLVMRuntimeDyld -lLLVMExecutionEngine -lLLVMTarget -lLLVMMC -lLLVMObject -lLLVMCore -lLLVMSupport -lDbgHelp -lPsapi -lkernel32 -user32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 -lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -ladvapi32 -lshell32 -luser32 -lkernel32 -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt c:/mingw64/bin/…/lib/gcc/x86_64-w64-mingw32/4.7.1/…/…/…/…/x86_64-w64-mingw32/lib/…/lib/crtend.o
c:\Synology\clang-3.2-WIN64\lib/libLLVMJIT.a(JIT.cpp.obj):JIT.cpp:(.text$ZN4llvm15SmallVectorImplINS_15TargetAlignElemEEaSERKS2[ll
vm::SmallVectorImplllvm::TargetAlignElem::operator=(llvm::SmallVectorImplllvm::TargetAlignElem const&)]+0x0): multiple definitio
n of `llvm::SmallVectorImplllvm::TargetAlignElem::operator=(llvm::SmallVectorImplllvm::TargetAlignElem const&)’

I’m wondering if it can be related to the Drangon v4.7.1 release of Mingw64 that I am using? It is apparently a prerelease, although it works fine for my program. In any event, I’d be the last to be surprised if it is simply me doing something wrong…

Cheers,
Mikael
– Love Thy Frog, Dammit!

2012/6/11 Justin Holewinski <justin.holewinski@gmail.com>