tartarus's bolg tartarus's bolg
  • Linux and Unix Guide
  • CMake
  • gcc
  • gdb
  • bash
  • GNU Make
  • DDCA-ETH
  • CS106L
  • CS144
  • NJU PA
  • NJU OS(jyy)
  • C
  • C++
  • Python
  • reveal-md
  • LaTex
  • Paper Reading
  • TBD
  • Linux and Unix Guide
  • CMake
  • gcc
  • gdb
  • bash
  • GNU Make
  • DDCA-ETH
  • CS106L
  • CS144
  • NJU PA
  • NJU OS(jyy)
  • C
  • C++
  • Python
  • reveal-md
  • LaTex
  • Paper Reading
  • TBD
  • pdb

  • make

  • cmake

    • Introduction
    • Basic Intro
    • Basic 01-hello-cmake
    • Basic 02-hello-headers
    • Basic 03-static-library
    • Basic 04-shared-library
    • Basic 05-installing
    • Basic 06-build-type
    • Basic 07-complie-flags
    • Basic 08-third-party-library
    • Basic 09-compiling-with-clang
      • 介绍
      • 概念解释
        • 在CMakeList.txt中指定编译器和链接器
        • 在命令行中告知CMake构建时所用的编译器
      • 构建本例
    • Basic 10-building-with-ninjia
    • Basic 11-cpp-standard
    • Intermediate sub-projects
    • Intermediate static-analysis
    • packge-management 04-conan
    • packge-management 05-vcpkg
    • Offical Tutorial(未完成)
  • Linux and Unix

  • Basic_Software
  • cmake
tartarus
2023-04-19
目录

Basic 09-compiling-with-clang

# 介绍

本例的目标是将 Linux 平台下默认使用的编译器 gcc 改为使用 clang 进行编译。

本例的目录🌲:

.
├── CMakeLists.txt
├── main.cpp
1
2
3

CMakeLists.txt:

# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)

# Set the project name
project (hello_cmake)

# Add an executable
add_executable(hello_cmake main.cpp)

1
2
3
4
5
6
7
8
9
10
11

# 概念解释

# 在 CMakeList.txt 中指定编译器和链接器

CMake 使用三个变量来控制编译和链接时使用的编译器和汇编器:

  • CMAKE_C_COMPILER - 指定编译 C 所使用的编译器
  • CMAKE_CXX_COMPILER - 指定编译 C++ 时所使用的编译器
  • CMAKE_LINKER - 指定链接二进制目标文件使用的链接器

# 在命令行中告知 CMake 构建时所用的编译器

cmake .. -DCMAKE_C_COMPILER=clang-3.6 -DCMAKE_CXX_COMPILER=clang++-3.6

# 构建本例

$ mkdir build

$ cd build

$ cmake .. -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++

$ make VERBOSE=1

$ ./hello_cmake
1
2
3
4
5
6
7
8
9
上次更新: 12/27/2023, 8:55:47 AM
Basic 08-third-party-library
Basic 10-building-with-ninjia

← Basic 08-third-party-library Basic 10-building-with-ninjia→

Theme by Vdoing | Copyright © 2023-2023 tartarus | CC BY-NC-SA 4.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式