2

I am using Windows OS, and the clang version is 8.0.0. I have installed visual studio community.

I am trying on linker scripts for custom memory mapping for one of my programs.

I am compiling and executing a program from the command line. These are the option I tried on windows

clang main.c -ffreestanding -nostartfiles -nodefaultlibs -Wl,-Tlinker.ld -o main

Error Thrown was:

LINK : warning LNK4044: unrecognized option '/T'; ignored
linker.ld : fatal error LNK1107: invalid or corrupt file: cannot read at 0x76
clang: error: linker command failed with exit code 1107 (use -v to see invocation)

When I Tried with -fuse-ld=lld-link ,the error was different

lld-link: warning: ignoring unknown argument: -Tlinker.ld
lld-link: error: <root>: undefined symbol: mainCRTStartup 
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I want to know why the

same commands work on ubuntu but not on windows?

And also kindly let me know the solution to pass linker scripts.

Is there any command for clang on windows which should take linker scripts and clang linker lld-link at the same time?

Jon marsh
  • 279
  • 7
  • 12
  • 1
    I don't have the community edition of VS to try, so can't give an answer. I will make an observation - It almost looks as if CLANG in VS Community is using the Microsoft Linker which instead of `lld` (the error output seems to be that of the MS linker). The Microsoft Linker doesn't support linker scripts. – Michael Petch Aug 03 '19 at 16:47
  • Are you using VS 2019 Community? – Michael Petch Aug 03 '19 at 18:31
  • What happens if you add the `-fuse-ld=lld-link` option? – Michael Petch Aug 04 '19 at 02:29
  • yes VS 2019. I will try with -fuse-ld=lld-link – Jon marsh Aug 04 '19 at 04:24
  • I tried with lld-link and error was ```lld-link: warning: ignoring unknown argument: -Tlinker.ld lld-link: error: : undefined symbol: mainCRTStartup clang: error: linker command failed with exit code 1 (use -v to see invocation)``` – Jon marsh Aug 04 '19 at 04:52
  • I wont be able to execute the executable generated ```main```. It is leading into ```segmentation fault``` – Jon marsh Aug 14 '19 at 03:57

1 Answers1

0

Windows Visual Studio help with clang-cl

clang-cl   options files  -fuse-ld=lld-link -v

-v Show commands to run and use verbose output

-fuse-ld=lld-link use ld linker lld-link for Windows

-fuse-ld=lld use ld linker ld.lld for Linux

cmake -E env LDFLAGS="-fuse-ld=lld-link" PATH="<path\to\ninja>" 
  cmake -H. -G Ninja -Bbuild 
     -DCMAKE_C_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe" 
     -DCMAKE_CXX_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe" 
     -DCMAKE_C_COMPILER_ID="Clang" 
     -DCMAKE_CXX_COMPILER_ID="Clang" 
     -DCMAKE_SYSTEM_NAME="Generic"

LLVM Windows Linker support

Microsoft Visual Studio 2019 Linker options

cmake, ninja, clang-cl on Windows

Clang-cl manual on Windows

LLVM extensions for LLD and clang-cl

(solved) research on ld-link with Windows

I've figured it out by comparing clang's output LL file, and noticed a curious line at the beginning:

target triple = "x86_64-pc-windows-msvc" add to .LL file

Once I added it to my testinput.ll file, everything worked flawlessly with lld-link. Hurray!