Linking

Mox compiles your code to an .obj file. An outside linker turns it into an executable. This page shows how that works and how to change it.

The default linker command

The default is:

linker="clang -o :MOX_LINKER_OUT:"

So when you run:

mox ./example.mox

the compiler builds example.mox.obj and then runs:

clang -o ./example.mox.exe ./example.mox.obj

The compiler adds the .obj file and any libraries to the end of the command. It expects the linker to add the C runtime by itself, and clang does.

Use another linker

Pass your own command:

mox ./example.mox linker="g++ -o :MOX_LINKER_OUT:"

Turn the linker off. The compiler will only write the .obj file:

mox ./example.mox linker=false

Then link it with any tool you like.

You can add to the linker command from Mox code. This runs at compile time with #run (more about it in Compile time).

__compiler_link_lib adds a library with -l. Write the name without .a, .lib, .dll or .so:

#run __compiler_link_lib("user32", false);

The linker command becomes:

clang -o ./example.mox.exe ./example.mox.obj -luser32

If the second argument is true, the compiler loads the library into itself. Then you can call its functions at compile time. See Build scripts in code.

__compiler_append_linker adds any text to the command:

#run __compiler_append_linker("-passed_from_comptime");

Problems on Windows

If you installed clang by hand, you also need the Windows SDK and VC Tools. They come with Visual Studio.

If you get a strange linker error:

  1. Build with linker=false and try to link the .obj file by hand first.

  2. If MSVC clang can not find system libraries, run it from the x64 Native Tools Command Prompt, or run vcvarsall.bat x64 first.

  3. You can also give the paths by hand:

    clang.exe -o ./app.exe ./app.mox.obj -luser32 "-Wl,/vctoolsdir:C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/" "-Wl,/winsdkdir:C:/Program Files (x86)/Windows Kits/10/"
  4. Or install MSYS2 clang, see Setup.

With MSVC clang, always pass llvm_target_triple="x86_64-pc-windows-msvc" to the compiler.