1

I have been trying to follow the instructions at http://llvm.org/docs/GettingStartedVS.html#an-example-using-the-llvm-tool-chain, but I run into errors at the linking stage. For some reason, I can compile the "hello world" program all the way to an .exe inside the Visual Studio GUI, but when I run the compiler (either clang or cl) from the command line, I get strange errors. I can create .obj objects just fine, but linking produces errors like the following:

link /DEFAULTLIB:libcmt /VERBOSE hello.obj

Microsoft (R) Incremental Linker Version 12.00.31101.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Starting pass 1

Searching libraries
    Searching C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\libcmt.lib:

Finished searching libraries

Finished pass 1


Unused libraries:
  C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\LIB\libcmt.lib

hello.obj : error LNK2019: unresolved external symbol printf referenced in function main
LINK : error LNK2001: unresolved external symbol mainCRTStartup
hello.exe : fatal error LNK1120: 2 unresolved externals

For some reason, it is ignoring the specified library and is unable to locate the relevant libraries for linking. How can I get the linker to resolve the symbols properly?

rlee827
  • 1,853
  • 2
  • 13
  • 32
  • Was hello.obj compiled with /MT? – Ofek Shilon Oct 13 '16 at 07:08
  • I am relatively new to C, so I am not sure. I simply ran `clang -c hello.c -emit-llvm -o hello.bc` followed by `llc -filetype-obj hello.bc` before invoking the linker. What does `/MT` do for `cl`, and what would be its equivalent for `clang`? – rlee827 Oct 13 '16 at 19:41
  • in VC, /MT specifies linkage to the static version of the CRT. The clang equivalent is -static: http://stackoverflow.com/questions/34411995/static-link-libstdc-using-clang. Generally speaking, static linkage to the CRT should have a good reason - it is widely accepted that dynamic linking is preferred (if only for the sake of maintainability by the library vendor) – Ofek Shilon Oct 13 '16 at 19:51
  • When I created the `.obj` file, I just used whatever the default was for `clang`. How should I point `clang` or the other programs of the LLVM suite towards the CRT so that I can use `link` to link dynamically? The guide seems to say that `clang hello.c -o hello.exe` should be enough, but it still fails at the linking step. – rlee827 Oct 14 '16 at 01:04

0 Answers0