19
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CMakeの-Cオプションでコンパイラーを切り替える

Posted at

デフォルトではgccだけどたまにclangでビルドしてみたいときに使える方法。

確認した環境

Ubuntu16.04(をVirtualBoxで動かしたもの)

ファイル配置

Direcroty構成
.
├── Qiita
│   ├── CMakeLists.txt
│   └── main.cpp
└── clang-cmakeinit.cmake
Qiita/CMakeLists.txt
cmake_minimum_required(VERSION 3.5)

set(SOURCE_FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
    )

add_executable(Hello ${SOURCE_FILES})
Qiita/main.cpp
#include <iostream>

int main(void){
    std::cout << "Hello World!" << std::endl;
}
clang-cmakeinit.cmake
set(CMAKE_C_COMPILER "/usr/bin/clang" CACHE string "clang compiler" FORCE)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++" CACHE string "clang++ compiler" FORCE)

使い方

デフォルト(gcc)でビルド

mkdir build
cd build
cmake ../Qiita
make

-Cオプションを付けてclangでビルド

mkdir build
cd build
cmake -C ../clang-cmakeinit.cmake ../Qiita/
make

参考

cmake公式のヘルプ

本当は上記ではなくて、別のどこかで見つけたはずだけどもどこだったか忘れた・・

19
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?