10
12

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.

UbuntuでC++を実行する

Last updated at Posted at 2019-05-19

そういえば

Ubuntuにすっかり馴染んできて、家でもUbuntuを使っているのですが、
Pythonが主流になってからUbuntuを使い始めたので、今までUbuntuでC++を書いたことがないな〜と思い。
実行方法をまとめてみました。
※C++久しぶりに書いたら、セミコロン忘れ地獄発生しました。

会社で必要になったからじゃあないぜ☆

前準備

前準備としてホームにcpp_projectというディレクトリを作成して、そこにコードを保存してきます。

$ mkdir ~/cpp_project
$ cd ~/cpp_project

cmakeのバージョンアップをします。

$ wget https://github.com/Kitware/CMake/releases/download/v3.13.5/cmake-3.13.5.tar.gz
$ tar -xf cmake-3.13.5.tar.gz
$ cd cmake-3.13.5
$ ./bootstrap && make && sudo make install
$ sudo ldconfig
# 確認 インストールしたcmakeと同じバージョンなら成功
$ cmake --version 
cmake version 3.13.5

CMake suite maintained and supported by Kitware (kitware.com/cmake).

方法1:gccでビルドして実行

一番簡単な実行方法かなと。

Sample code

test.cpp
# include<iostream>

using namespace std;

int main(void){
    int a = 1;
    int b = 2;
    int c = a + b;
    cout << "test:" << c << endl;
    return 0;
}

ビルド&実行

$ g++ -o test test.cpp #ビルド
$ ./test
test:3

-oが出力オプションであり、出力する実行ファイル名を指定できます。その後にビルドするCPPファイルを入力します。
すると、同じフォルダに指定した実行ファイル名と同じ実行ファイルができるので、それを実行します。
実行すると、結果が出力されます。

方法2:cmakeして実行する

CMakeLists.txt
# CMakeのバージョンを設定する
cmake_minimum_required(VERSION 3.13.5)

# プロジェクト名と使用する言語を設定
project(cpp_project CXX)
# 実行ファイルをtest.cppから作成
add_executable(cpp_project test.cpp)

このCMakeLists.txtを作成した後に、buildディレクトリを作成して、その中でcmakeを実行します。
その後に、makeすることで、``test.out`が作成されるので、それを実行することで結果が得られます。

$ mkdir build && cd build
$ cmake ..
$ make
$ ./test.out
test:3

ヘッダーがある場合

以下のヘッダーファイルをtest.cppと同じディレクトリに
保存してください。

test.hpp
# include <iostream>

さらに、test.cpp を以下のように変更します。

test.cpp
- #include <iostream>
+ #include "test.hpp"

using namespace std;

int main(void){
    int a = 1;
    int b = 2;
    int c = a + b;
    cout << "test:" << c << endl;
    return 0;
}

CMakeLists.txtは、ヘッダーファイルのtest.hppも一緒に書きます。

CMakeLists.txt
# CMakeのバージョンを設定する
cmake_minimum_required(VERSION 3.13.5)

# プロジェクト名と使用する言語を設定
project(cpp_project CXX)
# 実行ファイルをtest.hppとtest.cppから作成
- add_executable(cpp_project test.cpp)
+ add_executable(cpp_project test.hpp test.cpp)

方法3:OpenCVなどの他のライブラリを含むビルド&実行

ここからが、自分的な本題なのですが、OpenCVなどの他のライブラリを参照するプログラムをどうビルドするか書いていきます。

img_test.cpp
# include <iostream>
# include <opencv2/core.hpp>
# include <opencv2/highgui.hpp>

using namespace std;

int main(void){
    cout << "opencv test" << endl;
    cv::Mat img = cv::imread("img.jpg");
    cv::imshow("img",img);
    cv::waitKey();
    return 0;
}
CMakeLists.txt
# CMakeのバージョンを設定
cmake_minimum_required(VERSION 3.13.5)

# プロジェクト名と使用する言語を設定
project(cpp_project CXX)
# 実行ファイルをimg_test.cppから作成
add_executable(test3.out img_test.cpp)

# C++のバージョン指定
target_compile_features(test3.out
  PUBLIC cxx_std_11)

# OpenCVのパッケージを探す
find_package(OpenCV REQUIRED)

target_include_directories(test3.out
  PUBLIC ${OpenCV_INCLUDE_DIRS})
target_link_libraries(test3.out
  PUBLIC ${OpenCV_LIBS})

これで

基本的なことはできるようになりました。
あとは、VScode使えるようになりたいですね。

参考サイト

[1] コマンドラインで C++ コードをビルド(コンパイルとか)して実行してみよう
[2] CMakeLists.txt から始めよう
[3] CMakeを使ってみた(2)もう少しまともなプロジェクト
[4] [Linux][cmake] 最新のcmakeをインストールする方法

10
12
8

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
10
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?