How to build c++ with llvm clang in mac?

Answered

 

Hi, I'm a beginner study c++ by CLion(2021.2.2) in MacOS(10.13.6).

I want to use clang instead of default gcc, so I brew install llvm@8, and export PATH="/usr/local/opt/llvm@8/bin:$PATH"' >> /Users/kingdelee/.bash_profile

brew info llvm@8
llvm@8: stable 8.0.1 (bottled) [keg-only]
Next-gen compiler infrastructure
https://llvm.org/
/usr/local/Cellar/llvm@8/8.0.1_3 (6,843 files, 3.4GB)
Poured from bottle on 2021-09-19 at 20:20:15
From: https://mirrors.ustc.edu.cn/homebrew-core.git/Formula/llvm@8.rb
License: NCSA
==> Dependencies
Build: cmake ✘
Required: swig ✔, libffi ✔
==> Requirements
Build: Xcode ✔
Required: x86_64 architecture ✔
==> Caveats
To use the bundled libc++ please add the following LDFLAGS:
LDFLAGS="-L/usr/local/opt/llvm@8/lib -Wl,-rpath,/usr/local/opt/llvm@8/lib"

llvm@8 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.

If you need to have llvm@8 first in your PATH, run:
echo 'export PATH="/usr/local/opt/llvm@8/bin:$PATH"' >> /Users/kingdelee/.bash_profile

For compilers to find llvm@8 you may need to set:
export LDFLAGS="-L/usr/local/opt/llvm@8/lib"
export CPPFLAGS="-I/usr/local/opt/llvm@8/include"

==> Analytics
install: 226 (30 days), 617 (90 days), 4,902 (365 days)
install-on-request: 217 (30 days), 526 (90 days), 4,511 (365 days)
build-error: 0 (30 days)

 

Then I new a  c++ project and config like this:

 

Toolchains:

C Compiler: /usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8
C++ Compiler: /usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8

 

 

 

Cmake options:

-DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=/usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8 -DCMAKE_CXX_COMPILER=/usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8

 

CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(cxx1)
set(CMAKE_CXX_STANDARD 17)
add_executable(cxx1 main.cpp)

 

 

The question is , when I build, it throw error:

Cmake:


/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_COMPILER=/usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8 -DCMAKE_CXX_COMPILER=/usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8 -DCMAKE_DEPENDS_USE_COMPILER=FALSE -G "CodeBlocks - Unix Makefiles" /Users/kingdelee/CLionProjects/cxx1
-- The C compiler identification is Clang 8.0.1
-- The CXX compiler identification is Clang 8.0.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8 - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/local/Cellar/llvm@8/8.0.1_3/bin/clang-8 - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/kingdelee/CLionProjects/cxx1/cmake-build-debug
[Finished]

Messages:

====================[ Build | cxx1 | Debug ]====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/kingdelee/CLionProjects/cxx1/cmake-build-debug --target cxx1 -- -j 3
Scanning dependencies of target cxx1
[ 50%] Building CXX object CMakeFiles/cxx1.dir/main.cpp.o
[100%] Linking CXX executable cxx1
Undefined symbols for architecture x86_64:
"std::__1::locale::use_facet(std::__1::locale::id&) const", referenced from:
std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&) in main.cpp.o
"std::__1::ios_base::getloc() const", referenced from:
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in main.cpp.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__init(unsigned long, char)", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::basic_string(unsigned long, char) in main.cpp.o
"std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::~basic_string()", referenced from:
std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in main.cpp.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::put(char)", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in main.cpp.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::flush()", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::endl<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in main.cpp.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::sentry(std::__1::basic_ostream<char, std::__1::char_traits<char> >&)", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in main.cpp.o
"std::__1::basic_ostream<char, std::__1::char_traits<char> >::sentry::~sentry()", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in main.cpp.o
"std::__1::cout", referenced from:
_main in main.cpp.o
"std::__1::ctype<char>::id", referenced from:
std::__1::ctype<char> const& std::__1::use_facet<std::__1::ctype<char> >(std::__1::locale const&) in main.cpp.o
"std::__1::locale::~locale()", referenced from:
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in main.cpp.o
"std::__1::ios_base::__set_badbit_and_consider_rethrow()", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in main.cpp.o
"std::__1::ios_base::clear(unsigned int)", referenced from:
std::__1::ios_base::setstate(unsigned int) in main.cpp.o
"std::terminate()", referenced from:
___clang_call_terminate in main.cpp.o
"___cxa_begin_catch", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in main.cpp.o
___clang_call_terminate in main.cpp.o
"___cxa_end_catch", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in main.cpp.o
"___gxx_personality_v0", referenced from:
std::__1::basic_ostream<char, std::__1::char_traits<char> >& std::__1::__put_character_sequence<char, std::__1::char_traits<char> >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, char const*, unsigned long) in main.cpp.o
std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> > std::__1::__pad_and_output<char, std::__1::char_traits<char> >(std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >, char const*, char const*, char const*, std::__1::ios_base&, char) in main.cpp.o
std::__1::ostreambuf_iterator<char, std::__1::char_traits<char> >::ostreambuf_iterator(std::__1::basic_ostream<char, std::__1::char_traits<char> >&) in main.cpp.o
std::__1::basic_ios<char, std::__1::char_traits<char> >::widen(char) const in main.cpp.o
Dwarf Exception Unwind Info (__eh_frame) in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [CMakeFiles/cxx1.dir/build.make:93: cxx1] Error 1
make[2]: *** [CMakeFiles/Makefile2:83: CMakeFiles/cxx1.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:90: CMakeFiles/cxx1.dir/rule] Error 2
gmake: *** [Makefile:124: cxx1] Error 2

  

How to resolve the problem, thank you for your help.

0
2 comments

what in no.5?

you don't know what is my answer 

0

Hello!

>I want to use clang instead of default gcc

Usually the default toolchain on macOS is clang, please read https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-macos.html.

As for your toolchain: you specified a C compiler (clang-8) in the "C++ Compiler" field, it's incorrect. You need to specify clang++-8 (or something like that) in this field.

0

Please sign in to leave a comment.