"undefined reference to `llvm::raw_" when using ninja

There are songthing wrong when I use “ninja”

$cmake -G Ninja -DCMAKE_BUILD_TYPE=release \-DCMAKE_INSTALL_PREFIX=../build \-DLLVM_DIR="/home/learner/下载/llvm-project/build/lib/cmake/llvm" ../src

THE ANSWER IS——

CMake Warning (dev) in CMakeLists.txt:
  No project() command is present.  The top-level CMakeLists.txt file must
  contain a literal, direct call to the project() command.  Add a line of
  code such as

    project(ProjectName)

  near the top of the file, but after cmake_minimum_required().

  CMake is pretending there is a "project(Project)" command on the first
  line.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - 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/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
CMake Warning:
  Manually-specified variables were not used by the project:

    LLVM_DIR

-- Build files have been written to: /home/learner/下载/llvm-project/Learn-LLVM-12/Chapter03/calc/build

THEN I START NINJA

$ninja

BUT WRONG!!!

......
/usr/bin/ld: CMakeFiles/calc.dir/Sema.cpp.o: in function `(anonymous namespace)::DeclCheck::visit(WithDecl&)':
Sema.cpp:(.text+0xb0): undefined reference to `llvm::errs()'
/usr/bin/ld: Sema.cpp:(.text+0x1a7): undefined reference to `llvm::StringMapImpl::LookupBucketFor(llvm::StringRef)'
/usr/bin/ld: Sema.cpp:(.text+0x1dd): undefined reference to `llvm::allocate_buffer(unsigned long, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x20c): undefined reference to `llvm::StringMapImpl::RehashTable(unsigned int)'
/usr/bin/ld: Sema.cpp:(.text+0x280): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x2a0): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x2c0): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x2da): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x300): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: CMakeFiles/calc.dir/Sema.cpp.o: in function `(anonymous namespace)::DeclCheck::visit(Factor&)':
Sema.cpp:(.text+0x385): undefined reference to `llvm::StringMapImpl::FindKey(llvm::StringRef) const'
/usr/bin/ld: Sema.cpp:(.text+0x3c9): undefined reference to `llvm::errs()'
/usr/bin/ld: Sema.cpp:(.text+0x4b0): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x4d0): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x4f0): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x510): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: Sema.cpp:(.text+0x52a): undefined reference to `llvm::raw_ostream::write(char const*, unsigned long)'
/usr/bin/ld: CMakeFiles/calc.dir/Sema.cpp.o: in function `Sema::semantic(AST*)':
Sema.cpp:(.text+0x60c): undefined reference to `llvm::deallocate_buffer(void*, unsigned long, unsigned long)'
/usr/bin/ld: CMakeFiles/calc.dir/Sema.cpp.o: in function `Sema::semantic(AST*) [clone .cold]':
Sema.cpp:(.text.unlikely+0x3b): undefined reference to `llvm::deallocate_buffer(void*, unsigned long, unsigned long)'
/usr/bin/ld: CMakeFiles/calc.dir/Sema.cpp.o:(.data.rel+0x0): undefined reference to `llvm::DisableABIBreakingChecks'
/usr/bin/ld: CMakeFiles/calc.dir/Lexer.cpp.o:(.data.rel+0x0): undefined reference to `llvm::DisableABIBreakingChecks'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I am a beginner, and I don’t konw what happened.
I think whether the problem is from the command “DLLVM_DIR”.
Is it the place where I set my llvm source dir?

Seems like you’re using a tutorial written for a specific version of LLVM, have you made sure you’re using the actual LLVM version that goes with this tutorial?

You should probably provide more info as your problem does not obviously seem to be with LLVM but rather with this particular tutorial.

Thank you!
Looks like you were right.
I have an assignment right now, so I want to learn how to use llvm as soon as possible, but the tutorials are mostly out of date. Do you know where to find the differences between the old and new versions?

There is no good way. But I’d try to start with the Kaleidoscope tutorial (in the LLVM doc) and other examples that are in-tree: they will be “guaranteed” to actually work.

Alternatively: just make sure to use LLVM 12 and the relevant doc About — LLVM 12 documentation

If you just want a tutorial on how to build LLVM, there’s one provided by LLVM itself that’s kept up to date with reality: Getting Started with the LLVM System — LLVM 18.0.0git documentation

Thank you! I am now ready to use the latest tutorial to learn.