Jon marsh
Jon marsh

Reputation: 339

How can I fix this linker problem in Clang when compiling a C program?

I'm trying to compile a C program with Clang. While building, it is showing the error message linker command failed to execute with exit code 1.

How can I solve this?

I'm using Windows 10. I have installed the latest version of LLVM. I'm trying to compile the program with the command line.

// main.c
#include <stdio.h>

int main()
{
    printf("hello");
    return 0;
}

I'm using this command

clang main.c  -o main.exe -v

And I'm getting an error:

   "C:\\Program Files\\LLVM\\bin\\clang.exe" -cc1 -triple x86_64-pc-windows-msvc19.11.0 -emit-obj -mrelax-all -mincremental-linker-compatible -disable-free -disable-llvm-verifier -discard-value-names -main-file-name main.c -mrelocation-model pic -pic-level 2 -mthread-model posix -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -momit-leaf-frame-pointer -v -resource-dir "C:\\Program Files\\LLVM\\lib\\clang\\7.1.0" -internal-isystem "C:\\Program Files\\LLVM\\lib\\clang\\7.1.0\\include" -internal-isystem "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\WDExpress\\VC\\Tools\\MSVC\\14.16.27023\\include" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\Include\\10.0.17763.0\\ucrt" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.17763.0\\shared" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.17763.0\\um" -internal-isystem "C:\\Program Files (x86)\\Windows Kits\\10\\include\\10.0.17763.0\\winrt" -fdebug-compilation-dir "C:\\Users\\amith.ks\\Desktop" -ferror-limit 19 -fmessage-length 120 -fno-use-cxa-atexit -fms-extensions -fms-compatibility -fms-compatibility-version=19.11 -fdelayed-template-parsing -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -o "C:\\Users\\amith.ks\\AppData\\Local\\Temp\\main-ecf8c2.o" -x c main.c
clang -cc1 version 7.1.0 based upon LLVM 7.1.0 default target x86_64-pc-win32
#include "..." search starts here:
#include <...> search starts here:
 C:\Program Files\LLVM\lib\clang\7.1.0\include
 C:\Program Files (x86)\Microsoft Visual Studio\2017\WDExpress\VC\Tools\MSVC\14.16.27023\include
 C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt
 C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\shared
 C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\um
 C:\Program Files (x86)\Windows Kits\10\include\10.0.17763.0\winrt
End of search list.
 "link.exe" -out:main.exe -defaultlib:libcmt "-libpath:C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\WDExpress\\VC\\Tools\\MSVC\\14.16.27023\\lib\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.17763.0\\ucrt\\x64" "-libpath:C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.17763.0\\um\\x64" -nologo "C:\\Users\\amith.ks\\AppData\\Local\\Temp\\main-ecf8c2.o"

clang.exe: error: unable to execute command: program not executable
clang.exe: error: linker command failed with exit code 1 (use -v to see invocation)

I am not able to get that linker error. What is it is looking for?

Upvotes: 1

Views: 1745

Answers (2)

Jon marsh
Jon marsh

Reputation: 339

I am not sure about this answer. But there are a lot of differences between the Visual Studio Community and Express edition.

I installed Visual Studio Community and dependency tools like MSVC, WINDOWS SDK, etc. And added link.exe to %PATH%. Everything worked fine.

Upvotes: 0

Benjamin T
Benjamin T

Reputation: 8311

link.exe is Microsoft Visual C++'s (MSVC) linker. It does not come with Clang, and you should install it by installing Visual C++.

Note that link.exe does not necessarily need to be in PATH, clang seems to have some autodetection capabilities.

If it is already installed, that could mean that clang failed to locate your MSVC installation. In this case, updating your PATH with your MSVC installation directory, or calling vcvarsall.bat before using Clang could solve your issue.

Upvotes: 2

Related Questions