title: Link clang++ modules: Function exported using C++20 modules is not visible (clang++): Cannot compile executable file that uses module
body: <p>I am exporting a function in the following C++20 module. Although the main program can import the module, but it cannot see the exported function:</p>
  <p>f1.hpp</p>
  <pre class="lang-cpp prettyprint-override"><code>export module f1_module;
  export void f1() {
  }
  </code></pre>
  <p>f1_demo.cpp</p>
  <pre class="lang-cpp prettyprint-override"><code>import f1_module;
  int main() {
     f1();
     return 0;
  }
  </code></pre>
  <p>The build script is:</p>
  <pre class="lang-bash prettyprint-override"><code>#!/bin/bash
  mkdir -p ./target
  
  FLAGS=&quot;-std=c++20 -stdlib=libc++ -fmodules -fbuiltin-module-map&quot;
  
  clang++ $FLAGS \
      -fprebuilt-module-path=./target \
      -Xclang -emit-module-interface  \
      -c \
      f1.hpp \
      -o ./target/f1.module.o
  
  clang++ $FLAGS \
      -fprebuilt-module-path=./target \
          -fmodule-file=f1_module=./target/f1.module.o \
      f1_demo.cpp \
      -o ./target/f1_demo.o
  </code></pre>
  <p>The output error: <code>bash build.bash</code>:</p>
  <pre><code>ld: error: undefined symbol: f1()
  &gt;&gt;&gt; referenced by f1_demo.cpp
  &gt;&gt;&gt;               /tmp/f1_demo-286314.o:(main)
  clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  <p>The exported function <code>f1()</code> is not visible in the main program. How to make it work?</p>
  <p>I can provide the output to <code>-v</code>.</p>
  <p>I am running above build script in <code>conanio/clang14</code> docker container:</p>
  <pre class="lang-bash prettyprint-override"><code>docker run -it --rm -v $(pwd):/sosi conanio/clang14-ubuntu16.04:latest  bash
  </code></pre>
  <h3>Update: Cannot link</h3>
  <p>I tried @DavisHerring 's suggestion, adding <code>-c</code> to the second command, however, it does not generate executable. What I have is a regular object file, and a compiled module file. (The outcome of of <code>-c</code> is not executable). But I need an executable: I cannot link them. When tried to link using clang++, the problem is about linking:</p>
  <p>I cannot link an object file with a compiled module file:</p>
  <pre><code>clang++ $FLAGS \
      -Xclang -emit-module-interface  \
      -c \
      f1_module.cpp \
      -o ./target/f1_module.o
  
  clang++ $FLAGS \
      -fmodule-file=f1_module=./target/f1_module.o \
      -c \
      f1_demo.cpp \
      -o ./target/f1_demo.o
  
  clang \
      -v \
      ./target/f1_module.o \
      ./target/f1_demo.o \
      -o ./target/exe.o
  </code></pre>
  <p>output:</p>
  <pre><code>&quot;/usr/local/bin/ld&quot; -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o ./target/exe.e /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/local/lib/clang/14.0.0/lib/linux/clang_rt.crtbegin-x86_64.o -L/usr/local/bin/../lib/gcc/x86_64-linux-gnu/10.3.0 -L/usr/local/bin/../lib/gcc/x86_64-linux-gnu/10.3.0/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/local/bin/../lib -L/usr/local/bin/../lib64 -L/lib -L/usr/lib ./target/f1_module.module ./target/f1_demo.o /usr/local/lib/clang/14.0.0/lib/linux/libclang_rt.builtins-x86_64.a --as-needed -l:libllvm-unwind.so --no-as-needed -lc /usr/local/lib/clang/14.0.0/lib/linux/libclang_rt.builtins-x86_64.a --as-needed -l:libllvm-unwind.so --no-as-needed /usr/local/lib/clang/14.0.0/lib/linux/clang_rt.crtend-x86_64.o /usr/lib/x86_64-linux-gnu/crtn.o
  </code></pre>
  <p>followed by error: <strong>unclosed quote</strong></p>
  <pre><code>ld: error: ./target/f1_module.o:1427: unclosed quote
  clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
  </code></pre>
  <p>Note: I am running it on MacBook M1 (arm64) with MacOS Monterey 12.4 and run <code>clang++</code> via Docker 4.9.1 ( Engine: 20.10.16 ).</p>