1

i am has an object file(a.obj) and i am need to get executable(a.exe) file via linker call from command line. I am received a.obj file from this program:

#include "stdio.h"
int main(){
    puts("Hello world");
}

and i am used clang compiler for generating a.obj file with follow up arguments: "clang.exe -c a.cpp".

My problem is using the "puts" method, which is defined in the standard library(may be libvcruntime.lib) and I don't know which arguments to use for linked to the standard library.

My linker this is Microsoft link.exe and me also available the lld linker, from llvm project(it is more preferables).

My global target - this is to get executable file from llvm ir and call lld linker from code but theis other history :)

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
PavelDev
  • 579
  • 4
  • 12

2 Answers2

1

If you're building for Windows with Clang, and you want to use Visual C++'s standard libraries, I suggest you use clang-cl, which is a driver that converts a Visual C++ cl command line options into clang's native options.

You said you're writing:

clang -c a.cpp

The -c option asks the compiler to just produce and object file and stop (rather than sending the object file to the linker). It sounds like you want clang to call the linker, so you should omit the -c.

To use the static version of the standard library, specify /MT (or /MTd if you want the debug version of the standard library).

Putting it all together, this should work for you:

clang-cl /MT a.cpp

clang-cl will translate the /MT to the equivalent option(s) for clang and then run clang. When clang finishes compiling the object file, it'll then automatically call lld (the LLVM linker) with options compatible with the ones used for compiling, which should result in a working executable file.

For a while, when using clang to compile for Windows, you needed to use Microsoft's LINK instead of lld. But recent versions can use lld, and, in fact, will use lld by default.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Thanks for answer! In fact, I have one big project, but during the implementation I ran into the problem described in the question. My project is a llvm-based compiler. This compiler can generate object files. My target is to generating the executable files and therefore I think use lld in code with this command: "lld::coff::link(args, false, llvm::errs());". What should be args parameter for resolve "puts" symbol – PavelDev Apr 01 '19 at 18:00
0

Visual Studio

Specify /MT(d) instead of /MD(d) in project config. docs

clang

-static-libstdc++ -static-libgcc. docs

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • Thanks for answer, but in my problem i can use only the linker from command line. Because I have a my small compiler project, but it does generate only obj file – PavelDev Apr 01 '19 at 14:55