Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clang "hello, world!" link errors in windows

I just downloaded the CLang sources, made a Visual C++ 10 IDE workspace by using CMake, and built everything from Visual C++ 10.0 (express).

Now I get a bunch of linker errors on hello world:

d:\dev\test> type con >foo.cpp
#include <iostream>
using namespace std;
int main() { cout << "Hello, cling-clong world!" << endl; }
^Z

d:\dev\test> clang++ foo.cpp
foo-839435.o : error LNK2019: unresolved external symbol __ZSt4cout referenced in function _main
foo-839435.o : error LNK2019: unresolved external symbol __ZdlPv referenced in function __ZNSt14error_categoryD0Ev
foo-839435.o : error LNK2019: unresolved external symbol __ZSt18uncaught_exceptionv referenced in function __ZNSo6sentry
D2Ev
foo-839435.o : error LNK2019: unresolved external symbol ___cxa_rethrow referenced in function __ZNSt8ios_base5clearEib
foo-839435.o : error LNK2019: unresolved external symbol ___cxa_allocate_exception referenced in function __ZNSt8ios_base5clearEib
foo-839435.o : error LNK2019: unresolved external symbol ___cxa_throw referenced in function __ZNSt8ios_base5clearEib
foo-839435.o : error LNK2019: unresolved external symbol __ZSt17iostream_categoryv referenced in function __ZSt15make_er

So what can I do about it?

like image 661
Cheers and hth. - Alf Avatar asked Nov 19 '11 13:11

Cheers and hth. - Alf


People also ask

Does Clang work on Windows?

Clang/LLVM support for both CMake and MSBuild projects is available in Visual Studio 2019 and Visual Studio 2022. You can use Visual Studio 2019 version 16.2 and later with Clang/LLVM to edit, build, and debug C++ Visual Studio projects (MSBuild) that target Windows or Linux.

Which linker does clang use?

Clang can be configured to use one of several different linkers: GNU ld. GNU gold. LLVM's lld.

What is the difference between clang and clang ++?

for clang , compiles code as C. for clang++ , compiles code as C++


2 Answers

If you want to experiment with Clang on Windows, I suggest using a MinGW built version, like the one I provide here (or build it yourself using your favourite MinGW toolchain):

  • 32-bit

  • 64-bit

You will need both the gcc and clang packages (those without linux/mac/cygwin suffixes) and extract them to the same directory. Clang uses gcc to link, and can link to GCC's libstdc++, pretty much giving you access to the C++ standard library. Exceptions work for the 32-bit version. I haven't had any luck with debug info though.

Work is being done to bring better MS support to Clang, but it's a slowly progressing task.

like image 192
rubenvb Avatar answered Oct 04 '22 17:10

rubenvb


I think there is a misunderstanding here.

Clang is (slowly) being taught how to parse MFC headers. As far as I know, François Pichet is about alone on this project but there are only a few errors in the whole headers lot (!), and obviously the standard library shipped with VC++ parsing has been completed a long time ago.

However, this is about AST generation, not Code Generation. Clang is currently unable to properly generate code to interact with VC++ libraries. Not only is the name mangling incomplete, but the ABI is still being shaped (there have been numerous patches in the last 2 months to get the right padding/alignment) and there is a long standing issue with exceptions.

If you wish to use Clang on Windows, you should use MinGW or MinGW64, see ruben's answer.

like image 37
Matthieu M. Avatar answered Oct 04 '22 16:10

Matthieu M.