2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

VSCodeとCMakeでwxWidgetsする

2
Posted at

C++でGUIやりたい!

私は今、C++を使って音諳というものを作っているのですが、こいつをGUIで使えるようにしたいということで、その方法を模索していました。
Windowsを使うならwindows APIの経験があるのでまだいけたのですが、大学用に買ったmacbookでのGUI開発はやったことがなかったので、色々調べていました。

調べていく中で出会ったのがこのwxWidgetsです。
クラスプラットフォームで使えるので、Windowsも使う私にとってもなんかいい感じ!
(実際のことを言うと、KiCadで使われてた以上の理由はありません。)

そんなわけでwxWidgetsを使えるように環境構築を進めていたのですが、ちょっと沼ったのでやり方を記しておきます。

方針

普通にインストーラーとかをダウンロードして使うのが一番手っ取り早いと思うのですが、私が自分のPCの環境を汚されるのを嫌う習性を持っていることと、最近githubを使うようになって、余計なものをインストールしないでもビルドができるような方法が好きになり始めたので、本家のホームページからインストールとかダウンロードとかしないでやることにします。

具体的には、GitHubのsubmodule機能を使います。これなら、自分のリポジトリにwxWidgetsのリポジトリを加えるような感じで使えます。

使うもの

  • Visual Studio Code
  • CMake(VSCodeの拡張)
  • GitHub

手順

1.プロジェクトを作成

CMakeが使えるように設定します。
普通に作ってくれればいいです。
ファイル構成はこんな感じ

Hello World!できるくらい準備してくれるといいかも。

2.wxWidgetsをサブモジュールとして加える

GiuHubからwxWidgetsを加えます。
VSCodeのターミナルを使います。

まずはlibディレクトリに移動します。

cd lib

次に、wxWidgetsをサブモジュールとして追加します。

git submodule add https://github.com/wxWidgets/wxWidgets

すると、libの中にwxWidgetsができます。
終わったら、とりあえず初期化しておきます。

git submodule update --init

次に、wxWidgetsのサブモジュールを初期化します。
wxWidgetsのディレクトリに移動して上と同じコマンドですね。

cd wxWidgets
git submodule update --init

終わったら、ライブラリの追加は完了です。
元のディレクトリに戻りましょう。

cd ../../

3.プログラムを書く

さて、これが完了したら、CMakeとmain.cppを書きます。
ここら辺は公式の文書をそのまま使えるので使いましょう。

CMakeLists.txt をいい感じにする(参考:https://docs.wxwidgets.org/3.2/overview_cmake.html)

...

add_subdirectory(libs/wxWidgets)
add_executable(myapp main.cpp)
target_link_libraries(myapp wx::net wx::core wx::base)

main.cpp を書く(参考:https://docs.wxwidgets.org/3.2.6/overview_helloworld.html)

// wxWidgets "Hello World" Program
 
// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
 
#ifndef WX_PRECOMP
    #include <wx/wx.h>
#endif
 
class MyApp : public wxApp
{
public:
    virtual bool OnInit();
};
 
class MyFrame : public wxFrame
{
public:
    MyFrame();
 
private:
    void OnHello(wxCommandEvent& event);
    void OnExit(wxCommandEvent& event);
    void OnAbout(wxCommandEvent& event);
};
 
enum
{
    ID_Hello = 1
};
 
wxIMPLEMENT_APP(MyApp);
 
bool MyApp::OnInit()
{
    MyFrame *frame = new MyFrame();
    frame->Show(true);
    return true;
}
 
MyFrame::MyFrame()
    : wxFrame(NULL, wxID_ANY, "Hello World")
{
    wxMenu *menuFile = new wxMenu;
    menuFile->Append(ID_Hello, "&Hello...\tCtrl-H",
                     "Help string shown in status bar for this menu item");
    menuFile->AppendSeparator();
    menuFile->Append(wxID_EXIT);
 
    wxMenu *menuHelp = new wxMenu;
    menuHelp->Append(wxID_ABOUT);
 
    wxMenuBar *menuBar = new wxMenuBar;
    menuBar->Append(menuFile, "&File");
    menuBar->Append(menuHelp, "&Help");
 
    SetMenuBar( menuBar );
 
    CreateStatusBar();
    SetStatusText("Welcome to wxWidgets!");
 
    Bind(wxEVT_MENU, &MyFrame::OnHello, this, ID_Hello);
    Bind(wxEVT_MENU, &MyFrame::OnAbout, this, wxID_ABOUT);
    Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
}
 
void MyFrame::OnExit(wxCommandEvent& event)
{
    Close(true);
}
 
void MyFrame::OnAbout(wxCommandEvent& event)
{
    wxMessageBox("This is a wxWidgets Hello World example",
                 "About Hello World", wxOK | wxICON_INFORMATION);
}
 
void MyFrame::OnHello(wxCommandEvent& event)
{
    wxLogMessage("Hello world from wxWidgets!");
}

これでデバッグをすると、ちっこいウィンドウが出てきます。
ちっこいウィンドウが出てきたら勝ちです。

おわり

VSCodeとCMakeとGitHubが使えるなら割と簡単に始められますが、日本語の文書が少ないのもあってちょっと沼ってしまったのでメモ代わりで書いておきました。
慣れてきたら、wxWidgetsに関する記事をいろいろ出していきたいですね。
未来の自分の役に立てればいいな。

そんなわけで、以上!

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?