LoginSignup
43
36

More than 5 years have passed since last update.

C++11インタプリタ Clingのビルド

Last updated at Posted at 2013-12-03

Cling

CERNのROOTではCINTというC++インタプリタが使用されています。
その存在を知ったときは興奮しましたが、CINTの情報が基本的に古く、そして極端に少なかったため、その存在をすっかり忘れていました。

そのCINTの後継のものとしてLLVMを用いて作成されたClingが2011年に公開されていたようです。

Build

以下はREADMEのBuildingです。

Building LLVM and CLANG you must:

  • Check out the sources:
    git clone http://root.cern.ch/git/llvm.git src
    cd src
    git checkout cling-patches
    cd tools
    git clone http://root.cern.ch/git/cling.git
    git clone http://root.cern.ch/git/clang.git
    cd clang
    git checkout cling-patches
  • Configure, build and install them, either using CMake:
    cd ..
    mkdir build
    cd build
    cmake -DCMAKE_INSTALL_PREFIX=/some/install/dir \
          -DLLVM_TARGETS_TO_BUILD=CBackend\;CppBackend\;X86 \
          -DCMAKE_BUILD_TYPE=Debug \
          ../src
    make
    make install
  • or GNU Make (see ../src/configure --help for all options):
    cd ..
    mkdir build
    cd build
    ../src/configure --prefix=/some/install/dir
    make
    make install

--enable-optimized

src/configureを実行する際、--enable-optimizedを指定します。
指定しない場合、make後に以下のメッセージが表示されます。

llvm[0]: ***** Completed Debug+Asserts Build
llvm[0]: ***** Note: Debug build can be 10 times slower than an
llvm[0]: ***** optimized build. Use make ENABLE_OPTIMIZED=1 to
llvm[0]: ***** make an optimized build. Alternatively you can
llvm[0]: ***** configure with --enable-optimized.

configure

実際のconfigureは次のようなものになります。

../src/configure --prefix=/some/install/dir --enable-optimized

Usage

Hello World

Hello Worldをインタプリタで実行する。

$ cling

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <iostream>
[cling]$ std::cout << "Hello World!" << std::endl;
Hello World!
[cling]$ .q

lambda

C++11のlambdaをインタプリタで実行する。

※C++11がデフォルトで有効。

$ cling

****************** CLING ******************
* Type C++ code and press enter to run it *
*             Type .q to exit             *
*******************************************
[cling]$ #include <iostream>
[cling]$ auto func = [](int n){ std::cout << n << std::endl; };
[cling]$ func(72);
72
[cling]$ .q

execute

ファイル名と同じ名前の関数を作り、clingに渡す。

$ echo '#include <iostream>' > helloworld.cpp
$ echo 'void helloworld() {' >> helloworld.cpp
$ echo '    std::cout << "Hello World!" << std::endl;' >> helloworld.cpp
$ echo '}' >> helloworld.cpp
$ cat helloworld.cpp
#include <iostream>
void helloworld() {
    std::cout << "Hello World!" << std::endl;
}
$ cling helloworld.cpp
Hello World!

Reference

43
36
1

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
43
36