質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

2726閲覧

implotライブラリを使ってとりあえず簡単なグラフの描画を行いたい.C++によるプログラム.

kaerazu

総合スコア1

Visual Studio Code

Visual Studio Codeとは、Microsoft社が開発したマルチプラットフォーム対応のテキストエディタです。Visual Studioファミリーの一員でもあります。拡張性とカスタマイズ性が高く、テキストエディタでありながら、IDEと遜色ない機能を備えることができます。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2022/07/26 11:10

編集2022/07/26 12:21

前提

C++でグラフを描画できたらと思い,リンク先にあるImPlotというライブラリ( https://github.com/epezent/implot )を使って,リンク先のgithubの中でdemoとして公開されているような,正弦波のグラフと棒グラフを出力してみようと思い下のようなコードで"CMakeLists.txt"を書いて"cmake --build ."のコマンドでビルドしようとしたところ以下のようなメッセージが出ました.

ファイル構成に関しては以下のような感じです.
test_implot
| main.cpp
| CMakeLists.txt
├─build
└─imgui

buildに関してはwindowsのコマンドプロンプトでbuildファイル内に移動し,"cmake .."コマンドを実行後,続いて"cmake --build ."のコマンドを打ったというような状況です.

imguiファイルについてはimguiのライブラリ( https://github.com/ocornut/imgui )をクローンした後,implot
( https://github.com/epezent/implot )内のファイルをすべてコピペして貼り付けたものになります.

実現したいこと

・ImPlotを使ってとりあえず正弦波のグラフ,棒グラフをウィンドウに出力してみたい.

発生している問題・エラーメッセージ

CMake is re-running because C:/Users/PCUSER/Documents/cpp/test_implot/build/CMakeFiles/generate.stamp is out-of-date. the file 'C:/Users/PCUSER/Documents/cpp/test_implot/CMakeLists.txt' is newer than 'C:/Users/PCUSER/Documents/cpp/test_implot/build/CMakeFiles/generate.stamp.depend' result='-1' -- Configuring done -- Generating done -- Build files have been written to: C:/Users/PCUSER/Documents/cpp/test_implot/build Consolidate compiler generated dependencies of target plotTest [ 12%] Building CXX object CMakeFiles/plotTest.dir/main.cpp.obj [ 25%] Building CXX object CMakeFiles/plotTest.dir/imgui/implot.cpp.obj C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp: In function 'ImPlotTime ImPlot::MkGmtTime(tm*)': C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp:882:11: error: '_mkgmtime' was not declared in this scope; did you mean 'gmtime'? 882 | t.S = _mkgmtime(ptm); | ^~~~~~~~~ | gmtime C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp: In function 'tm* ImPlot::GetGmtTime(const ImPlotTime&, tm*)': C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp:893:7: error: 'gmtime_s' was not declared in this scope; did you mean 'gmtime'? 893 | if (gmtime_s(ptm, &t.S) == 0) | ^~~~~~~~ | gmtime C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp: In function 'tm* ImPlot::GetLocTime(const ImPlotTime&, tm*)': C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp:912:7: error: 'localtime_s' was not declared in this scope; did you mean 'localtime'? 912 | if (localtime_s(ptm, &t.S) == 0) | ^~~~~~~~~~~ | localtime C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp: In function 'void ImPlot::Locator_Time(ImPlotTicker&, const ImPlotRange&, float, bool, ImPlotFormatter, void*)': C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp:1232:68: warning: NULL used in arithmetic [-Wpointer-arith] 1232 | ftd.Time = t1; ftd.Spec = last_major_offset < 0 == NULL ? fmtf : fmt1; | ^~~~ C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp: In function 'tm* ImPlot::GetGmtTime(const ImPlotTime&, tm*)': C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp:900:1: warning: control reaches end of non-void function [-Wreturn-type] 900 | } | ^ C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp: In function 'tm* ImPlot::GetLocTime(const ImPlotTime&, tm*)': C:\Users\PCUSER\Documents\cpp\test_implot\imgui\implot.cpp:919:1: warning: control reaches end of non-void function [-Wreturn-type] 919 | } | ^ CMakeFiles\plotTest.dir\build.make:88: recipe for target 'CMakeFiles/plotTest.dir/imgui/implot.cpp.obj' failed mingw32-make.exe[2]: *** [CMakeFiles/plotTest.dir/imgui/implot.cpp.obj] Error 1 CMakeFiles\Makefile2:837: recipe for target 'CMakeFiles/plotTest.dir/all' failed mingw32-make.exe[1]: *** [CMakeFiles/plotTest.dir/all] Error 2 Makefile:119: recipe for target 'all' failed mingw32-make.exe: *** [all] Error 2

該当のソースコード

C++

1#include "imgui/implot.h" 2#include <iostream> 3#include <math.h> 4 5int main() { 6 static float xs1[1001], ys1[1001]; 7 for (int i = 0; i < 1001; ++i) { 8 xs1[i] = i * 0.001f; 9 ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10)); 10 } 11 static double xs2[20], ys2[20]; 12 for (int i = 0; i < 20; ++i) { 13 xs2[i] = i * 1/19.0f; 14 ys2[i] = xs2[i] * xs2[i]; 15 } 16 if (ImPlot::BeginPlot("Line Plots")) { 17 ImPlot::SetupAxes("x","y"); 18 ImPlot::PlotLine("f(x)", xs1, ys1, 1001); 19 ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); 20 ImPlot::PlotLine("g(x)", xs2, ys2, 20,ImPlotLineFlags_Segments); 21 ImPlot::EndPlot(); 22 } 23}

CMakeLists.txt

1cmake_minimum_required(VERSION 3.0.0) 2project(plotTest VERSION 0.1.0) 3 4include(CTest) 5enable_testing() 6 7add_executable(plotTest main.cpp imgui/implot.cpp imgui/implot_items.cpp imgui/imgui.cpp imgui/imgui_widgets.cpp imgui/imgui_tables.cpp imgui/imgui_draw.cpp) 8 9set(CPACK_PROJECT_NAME ${PROJECT_NAME}) 10set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) 11include(CPack) 12

CMakeLists.txtに関しては何とかネットで集めた知識を寄せ集めて書いたものです.もしかして,ここに問題があるのでしょうか?
ライブラリを使うのは初めてで,何とかbuildしようとしてたどり着いた結果がこれでした.

試したこと

implot.cpp ないで,time.h が include されていないのが原因かと思い,#include <time.h> を加えて同じ手順でbuildしようとしてみたのですがうまくいきませんでした.上記のエラーメッセージはtime.hのincludeを書き加えたうえで出力されたメッセージです.

補足情報(FW/ツールのバージョンなど)

windows 11 pro 21H2
VSCODE 1.69.2
C++17

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kaerazu

2022/07/26 12:04 編集

一度,buildファイルを消して作り直し,buildファイル内で"cmake .." ,"cmake --build ."と順番に実行したところ,特にエラーメッセージなくbuildされるようになりました. しかし,buildファイル内に作られたDebug内にある実行ファイルを実行したところ一瞬コマンドプロンプトのウィンドウが表示された後,すぐに終了してしまいました.グラフはたぶん描画できていなかったように見えました.今のところ原因はわかっていないです. 実現したいことは相変わらず,"ImPlotを使ってとりあえず正弦波,棒グラフをウィンドウに出力してみたい"となっています.
guest

回答1

0

ベストアンサー

ImPlotを使用するにはImGui周りの初期化が必要です。
ImGuiが行うのは描画データを作成するところまでなので、画面に表示するにはglfwやSDL2等のどれかも必要になります。

以下はWindows 10, Visual Studio 2022,glfwで環境を構築した例です。
https://github.com/glfw/glfw から glfw-3.3.8.bin.WIN64.zipをダウンロードして解凍しています。
imguiディレクトリとビルドの手順は同じです。

描画部分のソースはimgui/examples/example_glfw_opengl3/main.cpp
ImPlotの初期化と描画処理を入れています。
(デモウィンドウの描画処理は長くて入らなかったので省いています)

test_implot ├── build ├── glfw-3.3.8.bin.WIN64 ├── imgui ├── main.cpp └── CMakeLists.txt

CMake

1cmake_minimum_required(VERSION 3.0.0) 2 3project(plotTest VERSION 0.1.0) 4 5set(GLFW_DIR glfw-3.3.8.bin.WIN64) 6 7add_executable(${PROJECT_NAME} 8 main.cpp 9 imgui/implot.cpp 10 imgui/implot_items.cpp 11 imgui/imgui.cpp 12 imgui/imgui_widgets.cpp 13 imgui/imgui_tables.cpp 14 imgui/imgui_draw.cpp 15 imgui/imgui_demo.cpp 16 imgui/backends/imgui_impl_glfw.cpp 17 imgui/backends/imgui_impl_glfw.h 18 imgui/backends/imgui_impl_opengl3.cpp 19 imgui/backends/imgui_impl_opengl3.h 20 ) 21 22target_include_directories(${PROJECT_NAME} PRIVATE 23 ${GLFW_DIR}/include 24 imgui 25 imgui/backends 26 ) 27 28target_link_directories(${PROJECT_NAME} PRIVATE ${GLFW_DIR}/lib-vc2022) 29 30target_link_libraries(${PROJECT_NAME} PRIVATE glfw3.lib)

cpp

1// Dear ImGui: standalone example application for GLFW + OpenGL 3, using programmable pipeline 2// (GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan/Metal graphics context creation, etc.) 3// If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp. 4// Read online: https://github.com/ocornut/imgui/tree/master/docs 5 6#include "imgui.h" 7#include "imgui_impl_glfw.h" 8#include "imgui_impl_opengl3.h" 9#include <stdio.h> 10#if defined(IMGUI_IMPL_OPENGL_ES2) 11#include <GLES2/gl2.h> 12#endif 13#include <GLFW/glfw3.h> // Will drag system OpenGL headers 14 15// [Win32] Our example includes a copy of glfw3.lib pre-compiled with VS2010 to maximize ease of testing and compatibility with old VS compilers. 16// To link with VS2010-era libraries, VS2015+ requires linking with legacy_stdio_definitions.lib, which we do using this pragma. 17// Your own project should not be affected, as you are likely to link with a newer binary of GLFW that is adequate for your version of Visual Studio. 18#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) 19#pragma comment(lib, "legacy_stdio_definitions") 20#endif 21 22#include "imgui/implot.h" 23#include <cmath> 24 25static void glfw_error_callback(int error, const char* description) 26{ 27 fprintf(stderr, "Glfw Error %d: %s\n", error, description); 28} 29 30int main(int, char**) 31{ 32 // Setup window 33 glfwSetErrorCallback(glfw_error_callback); 34 if (!glfwInit()) 35 return 1; 36 37 // Decide GL+GLSL versions 38#if defined(IMGUI_IMPL_OPENGL_ES2) 39 // GL ES 2.0 + GLSL 100 40 const char* glsl_version = "#version 100"; 41 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); 42 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 43 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); 44#elif defined(__APPLE__) 45 // GL 3.2 + GLSL 150 46 const char* glsl_version = "#version 150"; 47 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 48 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); 49 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only 50 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac 51#else 52 // GL 3.0 + GLSL 130 53 const char* glsl_version = "#version 130"; 54 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); 55 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 56 //glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // 3.2+ only 57 //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // 3.0+ only 58#endif 59 60 // Create window with graphics context 61 GLFWwindow* window = glfwCreateWindow(1280, 720, "Dear ImGui GLFW+OpenGL3 example", NULL, NULL); 62 if (window == NULL) 63 return 1; 64 glfwMakeContextCurrent(window); 65 glfwSwapInterval(1); // Enable vsync 66 67 // Setup Dear ImGui context 68 IMGUI_CHECKVERSION(); 69 ImGui::CreateContext(); 70 ImPlot::CreateContext(); 71 ImGuiIO& io = ImGui::GetIO(); (void)io; 72 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 73 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 74 75 // Setup Dear ImGui style 76 ImGui::StyleColorsDark(); 77 //ImGui::StyleColorsLight(); 78 79 // Setup Platform/Renderer backends 80 ImGui_ImplGlfw_InitForOpenGL(window, true); 81 ImGui_ImplOpenGL3_Init(glsl_version); 82 83 // Load Fonts 84 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them. 85 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. 86 // - If the file cannot be loaded, the function will return NULL. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). 87 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. 88 // - Read 'docs/FONTS.md' for more instructions and details. 89 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! 90 //io.Fonts->AddFontDefault(); 91 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); 92 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); 93 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); 94 //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); 95 //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese()); 96 //IM_ASSERT(font != NULL); 97 98 // Our state 99 bool show_demo_window = true; 100 bool show_another_window = false; 101 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 102 103 // Main loop 104 while (!glfwWindowShouldClose(window)) 105 { 106 // Poll and handle events (inputs, window resize, etc.) 107 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. 108 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. 109 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. 110 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. 111 glfwPollEvents(); 112 113 // Start the Dear ImGui frame 114 ImGui_ImplOpenGL3_NewFrame(); 115 ImGui_ImplGlfw_NewFrame(); 116 ImGui::NewFrame(); 117 118 static float xs1[1001], ys1[1001]; 119 for (int i = 0; i < 1001; ++i) { 120 xs1[i] = i * 0.001f; 121 ys1[i] = 0.5f + 0.5f * sinf(50 * (xs1[i] + (float)ImGui::GetTime() / 10)); 122 } 123 static double xs2[20], ys2[20]; 124 for (int i = 0; i < 20; ++i) { 125 xs2[i] = i * 1/19.0f; 126 ys2[i] = xs2[i] * xs2[i]; 127 } 128 129 ImGui::Begin("My Window"); 130 if (ImPlot::BeginPlot("Line Plots")) { 131 ImPlot::SetupAxes("x","y"); 132 ImPlot::PlotLine("f(x)", xs1, ys1, 1001); 133 ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle); 134 ImPlot::PlotLine("g(x)", xs2, ys2, 20,ImPlotLineFlags_Segments); 135 ImPlot::EndPlot(); 136 } 137 ImGui::End(); 138 139 // Rendering 140 ImGui::Render(); 141 int display_w, display_h; 142 glfwGetFramebufferSize(window, &display_w, &display_h); 143 glViewport(0, 0, display_w, display_h); 144 glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); 145 glClear(GL_COLOR_BUFFER_BIT); 146 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); 147 148 glfwSwapBuffers(window); 149 } 150 151 // Cleanup 152 ImGui_ImplOpenGL3_Shutdown(); 153 ImGui_ImplGlfw_Shutdown(); 154 ImPlot::DestroyContext(); 155 ImGui::DestroyContext(); 156 157 glfwDestroyWindow(window); 158 glfwTerminate(); 159 160 return 0; 161}

投稿2022/07/27 06:09

編集2022/07/27 06:18
mah

総合スコア591

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

kaerazu

2022/07/27 09:01

ありがとうございます.とりあえず,mah さんの環境と合わせてやったところうまくいきました.ImGUIの使い方についてもよく確認しておくべきでした. VScodeの方はまたちょっとうまくいっていないのですがこちらはもう少しまた自分で調べてみようと思います.
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問