Search code examples
c++clangclang++

How to use clang -cc1 to link object files


In clang, I can just use clang file.o -o file.exe and it links the object file to a executable.

But there's a reason I must link file.o using just -cc1 and that's because I have embedded it in a C++ application. How can I link it using C++?

When you give a .o file to clang with -cc1 option, clang thinks its a source file and produces errors for it.


Solution

  • You just can't do that. clang -cc1... invokes the compiler only. It does not do linking. If you input an object file it treats it as a source file because the compiler can only consume source files and output object files.

    Look at this - verbose output of compiling and linking a simple program with clang:

    $ clang -v -o prog main.cpp
    Ubuntu clang version 16.0.6 (15)
    Target: x86_64-pc-linux-gnu
    Thread model: posix
    InstalledDir: /usr/bin
    Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13
    Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/13
    Candidate multilib: .;@m64
    Selected multilib: .;@m64
     "/usr/lib/llvm-16/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/imk/develop/so/scrap1 -resource-dir /usr/lib/llvm-16/lib/clang/16 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/backward -internal-isystem /usr/lib/llvm-16/lib/clang/16/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir=/home/imk/develop/so/scrap1 -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-e80981.o -x c++ main.cpp
    clang -cc1 version 16.0.6 based upon LLVM 16.0.6 default target x86_64-pc-linux-gnu
    ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../x86_64-linux-gnu/include"
    ignoring nonexistent directory "/include"
    #include "..." search starts here:
    #include <...> search starts here:
     /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13
     /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/x86_64-linux-gnu/c++/13
     /usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../include/c++/13/backward
     /usr/lib/llvm-16/lib/clang/16/include
     /usr/local/include
     /usr/include/x86_64-linux-gnu
     /usr/include
    End of search list.
     "/usr/bin/ld" -pie -z relro --hash-style=gnu --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o prog /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/13/crtbeginS.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/13 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/13/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/lib -L/usr/lib /tmp/main-e80981.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/bin/../lib/gcc/x86_64-linux-gnu/13/crtendS.o /lib/x86_64-linux-gnu/crtn.o
    

    You see that:

    "/usr/lib/llvm-16/bin/clang" -cc1 ... -o /tmp/main-e80981.o -x c++ main.cpp
    

    compiles main.cpp, as C++, to the object file /tmp/main-e80981.o. Then the linker does the linking:

    "/usr/bin/ld" ... -o prog ... /tmp/main-e80981.o ...
    

    Perhaps it helps that the clang source file you pointed to appears to be the main source file for the frontend clang command, not clang -cc1, and that the main source file for the compiler appears to be this one. If that is right - and I have only taken a quick glance at the source - then you might hope to proceed to by embedding a call to clang, not clang -cc1.