conda-forge / ctng-compiler-activation-feedstock

A conda-smithy repository for ctng-compiler-activation.
BSD 3-Clause "New" or "Revised" License
13 stars 27 forks source link

Linker error while using clang-17 inside anaconda environment #111

Closed chudur-budur closed 6 months ago

chudur-budur commented 6 months ago

Solution to issue cannot be found in the documentation.

Issue

I need to use clang-17 for a project, but I don't have administrative permission to the machine that I am working on. So I am using anaconda environment for this purpose. I also need python3.11.

I have created a conda environment like this:

(base) user@pc:/localdisk/user|> conda create -n clangdev python=3.11

Then installed clang-17 through conda, clangxx_linux-64:

(base) user@pc:/localdisk/user|> conda activate clangdev
(clangdev) user@pc:/localdisk/user|> conda install -c conda-forge clangxx_linux-64==17

It seems like clang is working, we can verify:

(clangdev) user@pc:/localdisk/user|> which clang
/localdisk/user/opt/miniconda3/envs/clangdev/bin/clang
(clangdev) user@pc:/localdisk/user|> clang -v
clang version 17.0.6 (https://github.com/conda-forge/clangdev-feedstock 71dc3d053bf53a7cb6294eba448c059a3060dbe6)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /localdisk/user/opt/miniconda3/envs/clangdev/bin
Found candidate GCC installation: /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0
Selected GCC installation: /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

Now, let's take a simple c++ code:

#include <iostream>

int main() {
    std::cout << "hello world!" << std::endl;
    return 0;
}

But when I try to compile, I get this linker error:

(clangdev) user@pc:/localdisk/user|> clang -Wall -pedantic -v test.cpp -o test
clang version 17.0.6 (https://github.com/conda-forge/clangdev-feedstock 71dc3d053bf53a7cb6294eba448c059a3060dbe6)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /localdisk/user/opt/miniconda3/envs/clangdev/bin
Found candidate GCC installation: /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0
Selected GCC installation: /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/localdisk/user/opt/miniconda3/envs/clangdev/bin/clang-17" -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -mrelax-all -dumpdir test- -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name test.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 -debugger-tuning=gdb -v -fcoverage-compilation-dir=/localdisk/user -resource-dir /localdisk/user/opt/miniconda3/envs/clangdev/lib/clang/17 -isysroot /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot -internal-isystem /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../gcc/x86_64-conda-linux-gnu/13.2.0/include/c++/ -internal-isystem /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../gcc/x86_64-conda-linux-gnu/13.2.0/include/c++//x86_64-conda-linux-gnu -internal-isystem /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../gcc/x86_64-conda-linux-gnu/13.2.0/include/c++//backward -internal-isystem /localdisk/user/opt/miniconda3/envs/clangdev/lib/clang/17/include -internal-isystem /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/local/include -internal-isystem /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/include -internal-externc-isystem /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/include -internal-externc-isystem /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/include -Wall -pedantic -fdeprecated-macro -fdebug-compilation-dir=/localdisk/user -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/test-87144b.o -x c++ test.cpp
clang -cc1 version 17.0.6 based upon LLVM 17.0.6 default target x86_64-unknown-linux-gnu
ignoring nonexistent directory "/localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/local/include"
ignoring nonexistent directory "/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/include"
ignoring nonexistent directory "/localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/include"
#include "..." search starts here:
#include <...> search starts here:
 /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../gcc/x86_64-conda-linux-gnu/13.2.0/include/c++
 /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../gcc/x86_64-conda-linux-gnu/13.2.0/include/c++//x86_64-conda-linux-gnu
 /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../gcc/x86_64-conda-linux-gnu/13.2.0/include/c++//backward
 /localdisk/user/opt/miniconda3/envs/clangdev/lib/clang/17/include
 /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/include
End of search list.
 "/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/bin/ld" --sysroot=/localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot -pie -z relro --hash-style=gnu --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o test /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/lib/../lib64/Scrt1.o /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/lib/../lib64/crti.o /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/crtbeginS.o -L/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0 -L/localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/lib/../lib64 -L/localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/lib/../lib64 -L/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/lib -L/localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/lib -L/localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/lib /tmp/test-87144b.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/crtendS.o /localdisk/user/opt/miniconda3/envs/clangdev/x86_64-conda-linux-gnu/sysroot/usr/lib/../lib64/crtn.o
/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/bin/ld: /tmp/test-87144b.o: in function `main':
test.cpp:(.text+0x12): undefined reference to `std::cout'
/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/bin/ld: test.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/bin/ld: test.cpp:(.text+0x28): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
/localdisk/user/opt/miniconda3/envs/clangdev/bin/../lib/gcc/x86_64-conda-linux-gnu/13.2.0/../../../../x86_64-conda-linux-gnu/bin/ld: test.cpp:(.text+0x2d): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

It looks like I need to install a conda package that provides a linker.

How do I fix this?

Installed packages

# packages in environment at /localdisk/user/opt/miniconda3/envs/clangdev:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
binutils_impl_linux-64    2.40                 ha885e6a_0    conda-forge
binutils_linux-64         2.40                 hdade7a5_3    conda-forge
bzip2                     1.0.8                h5eee18b_6
ca-certificates           2024.3.11            h06a4308_0
clang                     17.0.6          default_h90ac42e_4    conda-forge
clang-17                  17.0.6          default_h127d8a8_4    conda-forge
clang_linux-64            17                   h9dc8132_3    conda-forge
clangxx                   17.0.6          default_h127d8a8_4    conda-forge
clangxx_linux-64          17                   h5a997db_3    conda-forge
compiler-rt               17.0.6               h00ab1b0_1    conda-forge
compiler-rt_linux-64      17.0.6               h00ab1b0_1    conda-forge
kernel-headers_linux-64   2.6.32              he073ed8_17    conda-forge
ld_impl_linux-64          2.40                 h55db66e_0    conda-forge
libclang-cpp17            17.0.6          default_h127d8a8_4    conda-forge
libffi                    3.4.4                h6a678d5_1
libgcc-devel_linux-64     13.2.0             h6ddb7a1_107    conda-forge
libgcc-ng                 13.2.0               h77fa898_7    conda-forge
libgomp                   13.2.0               h77fa898_7    conda-forge
libllvm17                 17.0.6               hc9c083f_0
libstdcxx-devel_linux-64  13.2.0             h6ddb7a1_107    conda-forge
libstdcxx-ng              13.2.0               hc0a3c3a_7    conda-forge
libuuid                   1.41.5               h5eee18b_0
lz4-c                     1.9.4                hcb278e6_0    conda-forge
ncurses                   6.4                  h6a678d5_0
openssl                   3.3.0                hd590300_0    conda-forge
pip                       24.0            py311h06a4308_0
python                    3.11.9               h955ad1f_0
readline                  8.2                  h5eee18b_0
setuptools                69.5.1          py311h06a4308_0
sqlite                    3.45.3               h5eee18b_0
sysroot_linux-64          2.12                he073ed8_17    conda-forge
tk                        8.6.14               h39e8969_0
tzdata                    2024a                h04d1e81_0
wheel                     0.43.0          py311h06a4308_0
xz                        5.4.6                h5eee18b_1
zlib                      1.2.13               h5eee18b_1
zstd                      1.5.5                hc292b87_2

Environment info

active environment : clangdev
    active env location : /localdisk/user/opt/miniconda3/envs/clangdev
            shell level : 2
       user config file : /home/user/.condarc
 populated config files : /home/user/.condarc
          conda version : 24.1.2
    conda-build version : not installed
         python version : 3.12.1.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=icelake
                          __conda=24.1.2=0
                          __glibc=2.35=0
                          __linux=5.15.0=0
                          __unix=0=0
       base environment : /localdisk/user/opt/miniconda3  (writable)
      conda av data dir : /localdisk/user/opt/miniconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
          package cache : /localdisk/user/opt/miniconda3/pkgs
                          /home/user/.conda/pkgs
       envs directories : /localdisk/user/opt/miniconda3/envs
                          /home/user/.conda/envs
               platform : linux-64
             user-agent : conda/24.1.2 requests/2.31.0 CPython/3.12.1 Linux/5.15.0-105-generic ubuntu/22.04.4 glibc/2.35 solver/libmamba conda-libmamba-solver/23.12.0 libmambapy/1.5.3
                UID:GID : 1022:1023
             netrc file : None
           offline mode : False
h-vetinari commented 6 months ago

You have a linker in the environment already:

ld_impl_linux-64          2.40                 h55db66e_0    conda-forge

What you're missing is the correct include path for the C++ standard library, which clang doesn't have by default. However, if you use clangxx as your invocation, this should work. This is modeled after how gcc is the C compiler and gxx is the C++ compiler.

chudur-budur commented 6 months ago

However, if you use clangxx as your invocation, this should work.

But there is no executable called clangxx, you mean clang-17? I tried with clang-17 but getting the same linker error.

h-vetinari commented 6 months ago

Sorry, I misremembered, the executable (provided by the package clangxx) is called clang++.

chudur-budur commented 6 months ago

It's working now. Thanks @h-vetinari Closing.