how to use imgui+glfw+opengl with vcpkg+clion+cmake on win11 - c++

I use vcpkg manifest to isntall packages, vcpkg.json:
{
"name": "vcpkgtest",
"version": "1.0.0",
"description": "learn vkpg & glfw & imgui & cmake",
"dependencies": [
"glfw3",
{
"name": "imgui",
"features": ["glfw-binding", "opengl3-binding"]
}
]
}
and installed them succesfully, CMakeLists.txt:
cmake_minimum_required(VERSION 3.25)
project(vcpkg_test)
set(CMAKE_CXX_STANDARD 17)
add_executable(vcpkg_test main.cpp)
## link packages
find_package(OpenGL REQUIRED)
target_link_libraries(vcpkg_test PRIVATE opengl32)
find_package(glfw3 CONFIG REQUIRED)
target_link_libraries(vcpkg_test PRIVATE glfw)
find_package(imgui CONFIG REQUIRED)
target_link_libraries(vcpkg_test PRIVATE imgui::imgui)
Run the folloing code main.cpp:
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <GLFW/glfw3.h>
int main(void)
{
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::Begin("My name is window, ImGUI window");
ImGui::Text("Hello there new born!");
ImGui::End();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}
I get many undefined reference to 'ImGui::xxx':
[ 50%] Linking CXX executable vcpkg_test.exe
CMakeFiles\vcpkg_test.dir/objects.a(main.cpp.obj): In function `main':
E:/Fdisk/programming/vcpkg_test/main.cpp:26: undefined reference to `ImGui::DebugCheckVersionAndDataLayout(char const*, unsigned long long, unsigned long long, unsigned long long, unsigned long long, unsigned long long, unsigned long long)'
E:/Fdisk/programming/vcpkg_test/main.cpp:27: undefined reference to `ImGui::CreateContext(ImFontAtlas*)'
E:/Fdisk/programming/vcpkg_test/main.cpp:28: undefined reference to `ImGui::GetIO()'
E:/Fdisk/programming/vcpkg_test/main.cpp:29: undefined reference to `ImGui_ImplGlfw_InitForOpenGL(GLFWwindow*, bool)'
E:/Fdisk/programming/vcpkg_test/main.cpp:30: undefined reference to `ImGui_ImplOpenGL3_Init(char const*)'
E:/Fdisk/programming/vcpkg_test/main.cpp:38: undefined reference to `ImGui_ImplOpenGL3_NewFrame()'
E:/Fdisk/programming/vcpkg_test/main.cpp:39: undefined reference to `ImGui_ImplGlfw_NewFrame()'
E:/Fdisk/programming/vcpkg_test/main.cpp:40: undefined reference to `ImGui::NewFrame()'
E:/Fdisk/programming/vcpkg_test/main.cpp:42: undefined reference to `ImGui::Begin(char const*, bool*, int)'
E:/Fdisk/programming/vcpkg_test/main.cpp:43: undefined reference to `ImGui::Text(char const*, ...)'
E:/Fdisk/programming/vcpkg_test/main.cpp:44: undefined reference to `ImGui::End()'
E:/Fdisk/programming/vcpkg_test/main.cpp:45: undefined reference to `ImGui::Render()'
E:/Fdisk/programming/vcpkg_test/main.cpp:46: undefined reference to `ImGui::GetDrawData()'
E:/Fdisk/programming/vcpkg_test/main.cpp:46: undefined reference to `ImGui_ImplOpenGL3_RenderDrawData(ImDrawData*)'
E:/Fdisk/programming/vcpkg_test/main.cpp:56: undefined reference to `ImGui_ImplOpenGL3_Shutdown()'
E:/Fdisk/programming/vcpkg_test/main.cpp:57: undefined reference to `ImGui_ImplGlfw_Shutdown()'
E:/Fdisk/programming/vcpkg_test/main.cpp:58: undefined reference to `ImGui::DestroyContext(ImGuiContext*)'
collect2.exe: error: ld returned 1 exit status
I've directly used the source code of imgui before to run the main.cpp successfully.
It's my first time to use vcpkg+imgui, but it seems that the lib missed something. I'm sure that ["glfw-binding", "opengl3-binding"] are installed because I can see the corresponding header files in ./cmake-build-debug/vcpkg_installed/x64-windows/include. So what's the problem?

Related

C++ project crashes after glewinit()

(Im using Clion and Cmake on Macosx Intel chip)
I wan't to make a Window Application with GLEW. But i get this error:
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
I heard that you should define "GLEW_STATIC" in the preprocessing. But I have no idea how Clion works
My main.cpp:
#include "GL/glew.h"
#include <GLFW/glfw3.h>
int main()
{
GLFWwindow* window;
if (!glfwInit())
return -1;
glewInit();
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
and my cmake:
cmake_minimum_required(VERSION 3.22)
project(renderer)
set(CMAKE_CXX_STANDARD 14)
include_directories(/usr/local/Cellar/glfw/3.3.8/include)
include_directories(/usr/local/Cellar/glew/2.2.0_1/include)
add_definitions(-DGLEW_STATIC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -framework CoreFoundation -framework OpenGL -framework GLUT ")
add_executable(renderer main.cpp)
target_link_libraries(renderer /usr/local/Cellar/glfw/3.3.8/lib/libglfw.3.3.dylib)
target_link_libraries(renderer /usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.dylib)
How do i fix this problem?
glewInit() requires a current OpenGL context to operate correctly. As written in your code glewInit() will fail and leave its glClear() function-pointer set to nullptr.
Call glewInit() after glfwMakeContextCurrent() 'returns' GLFW_NO_ERROR and verify that it returns GLEW_OK.

GTk and open GL Not working at compile time

I am trying to make an application using Gtk 3 and OpenGl but when I try to compile the file, the Gl commands are not recognized. Any suggestions? Code and Errors provided below. I am just trying to setup and render a empty gl area.
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
static gboolean
render (GtkGLArea *area, GdkGLContext *context)
{
glClearColor(0, 0, 0, 0);
return TRUE;
}
static void activate (GtkApplication *app, gpointer user_data){
GtkWidget *window;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Calculator");
gtk_window_resize(GTK_WINDOW(window), 290, 300);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
GtkWidget *gl = gtk_gl_area_new();
g_signal_connect (gl, "render", G_CALLBACK(render), NULL);
gtk_widget_show_all(window);
gtk_main();
}
int main(int argc, char **argv){
GtkApplication *app;
int status;
app = gtk_application_new("com.calculate", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref(app);
return status;
}
Compiled with:
gcc OpenGl.c `pkg-config --cflags gtk+-3.0` -o gl `pkg-config --libs gtk+-3.0`
Error Message:
/usr/bin/ld: /tmp/ccmKOqOJ.o: in function `render':
OpenGl.c:(.text+0x21): undefined reference to `glClearColor'
/usr/bin/ld: OpenGl.c:(.text+0x2b): undefined reference to `glClear'
collect2: error: ld returned 1 exit status
You need to link OpenGL with your program, just like you have linked GTK+. -lOpenGL in your compiler invocation may be all you need.

Linker error in Dev C++: undefined reference to 'glfwInit' [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I'm trying to follow this tutorial for OpenGL. I originally copied the code by hand, but that wasn't working, so I've copy-pasted the code straight from the website. I keep getting this error:
[Linker error] undefined reference to 'glfwInit'
from this code (which feels longer than necessary):
//C++ standard headers
#include <stdio.h>
#include <stdlib.h>
//GLEW header
#include <GL/glew.h>
//GLFW header
#include <GL/glfw3.h>
int main()
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL
// Open a window and create its OpenGL context
GLFWwindow* window; // (In the accompanying source code, this variable is global)
window = glfwCreateWindow( 1024, 768, "Tutorial 01", NULL, NULL);
if( window == NULL )
{
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); // Initialize GLEW
glewExperimental=true; // Needed in core profile
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
}
// Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
}
I've got no idea why it's not compiling. Anyone know what's going on?
EDIT: I'm using Dev-C++, as stated in the title.
undefined reference to 'glfwInit'
means that the linker did not find the library where glfwInit() is defined. You have to add glfw3.a to your linker input. Indeed, Dev-C++ use MinGW so unlike Visual Studio, the libraries can not be .lib.
To do that with Dev-C++, go to your 'project options', 'parameters', and 'add a library'. Then browse the explorer to find the glfw3.a I mentioned (usually in GLFW-<version>/lib/).

GLEW and glfw compile error: undefined reference to symbol 'XConvertSelection'

I'm trying to compile this code:
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
// Initialise GLFW
if( !glfwInit() )
{
fprintf( stderr, "Failed to initialize GLFW\n" );
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_RESIZABLE,GL_FALSE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open a window and create its OpenGL context
window = glfwCreateWindow( 1024, 768, "Playground", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
// Initialize GLEW
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}
// Ensure we can capture the escape key being pressed below
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
do{
// Draw nothing, see you in tutorial 2 !
// Swap buffers
glfwSwapBuffers(window);
glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );
// Close OpenGL window and terminate GLFW
glfwTerminate();
return 0;
}
It's a code from a tutorial I found about OpenGL, I program mostly in Java when it comes to OpenGL, but I wanted to try something new so I went to try in C++.
I'm using QtCreator for this project.
At first I included GLEW and glfw3 libraries:
And the same for the glfw library file.
And then, when I try compiling the program I get this error:
In text:
$ /home/sapir/Qt/5.4/gcc_64/bin/qmake -spec linux-g++ CONFIG+=debug -o Makefile ../Test/Test.pro
$ g++ -Wl,-rpath,/home/sapir/Qt/5.4/gcc_64 -o Test main.o -L/home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib/ -lglfw3 -lGLEW -lGLEWmx
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(glx_context.c.o): undefined reference to symbol 'glXQueryExtension'
//usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make: *** [Test] Error 1
23:12:13: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project Test (kit: Desktop Qt 5.4.0 GCC 64bit)
When executing step "Make"
23:12:13: Elapsed time: 00:00.
I tired searching for an answer in forums and here, but I couldn't find anything that solved this problem.
Anybody got any Ideas?
After adding
-lXxf86vm -lXrandr -lGL -lGLU -lXi
to the gcc compiler, I get a different error, which contains:
/usr/bin/ld: /home/sapir/Dropbox/Development/Computer/openGLTest/Test/../../../../../../../usr/local/lib//libglfw3.a(x11_window.c.o): undefined reference to symbol 'XConvertSelection'
This is my make file: http://pastebin.com/xL5Hpwsf
And this is my .pro file: http://pastebin.com/yhkV7nn7
Ok, after some research I found that the DSO error which I got means that the order of the includes I've implemented is incorrect and cause the compilation to fail.
So what I did is I used the command:
pkg-config --static --libs x11 xrandr xi xxf86vm glew glfw3
To get the packages I need for them to run and in the right order.
Then I compiled the project accordingly.
That's it :)
I got same error "undefined reference to symbol 'XConvertSelection'" while compiling example from Irrlicht 3D, solved by adding "-lX11".
Then I got error "undefined reference to symbol 'XF86VidModeGetGamma'", solved by adding "-lXxf86vm"

(C++)Code::Blocks possible wrong setup for Qt4

OS : windows xp SP2 ,
compiler : Code::Blocks ver. 10.05 ,
Qt 4.6
I recently started to learn Qt. At first all went well with simple tut examples.
I soon came across an example that can not compile and and realized that something is wrong.
Here is the code :
#include <QWidget>
#include <QApplication>
#include <QPushButton>
#include <QLabel>
#include <QDesktopWidget>
class Communicate : public QWidget
{
Q_OBJECT
public:
Communicate(QWidget *parent = 0);
private slots:
void OnPlus();
void OnMinus();
private:
QLabel *label;
};
void center(QWidget *widget, int w, int h)
{
int x, y;
int screenWidth;
int screenHeight;
QDesktopWidget *desktop = QApplication::desktop();
screenWidth = desktop->width();
screenHeight = desktop->height();
x = (screenWidth - w) / 2;
y = (screenHeight - h) / 2;
widget->move( x, y );
}
Communicate::Communicate(QWidget *parent)
: QWidget(parent)
{
int WIDTH = 350;
int HEIGHT = 190;
resize(WIDTH, HEIGHT);
QPushButton *plus = new QPushButton("+", this);
plus->setGeometry(50, 40, 75, 30);
QPushButton *minus = new QPushButton("-", this);
minus->setGeometry(50, 100, 75, 30);
label = new QLabel("0", this);
label->setGeometry(190, 80, 20, 30);
connect(plus, SIGNAL(clicked()), this, SLOT(OnPlus()));
connect(minus, SIGNAL(clicked()), this, SLOT(OnMinus()));
center(this, WIDTH, HEIGHT);
}
void Communicate::OnPlus()
{
int val = label->text().toInt();
val++;
label->setText(QString::number(val));
}
void Communicate::OnMinus()
{
int val = label->text().toInt();
val--;
label->setText(QString::number(val));
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Communicate window;
window.setWindowTitle("Communicate");
window.show();
return app.exec();
}
When I try to open it, I get this message:
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| undefined reference to `vtable for Communicate'|
obj\Release\main.o:main.cpp|| more undefined references to `vtable for Communicate' follow|
||=== Build finished: 6 errors, 0 warnings ===|
I was looking for a solution to the code:: blocks forum and learned that there should be Qt plugin installed.
So , I install QtWorkbench 0.6.0 alpha -> qt plugin but nothing has changed.
Any suggestion is welcome.
Did you moc this file and include the moc output to get compiled?
Whenever you use the Q_OBJECT macro, you must use Qt's moc command on that file to generate a new cpp file which should also be included in the files to be compiled with your project. In addition, I believe that you can only moc a header file, so you will have to move your class definition to a separate file and moc that file.
I don't know how it works for your IDE, but on the command line you would call something like
<QT4 directory>\bin\moc.exe myfile.h -o moc_myfile.cpp
Then include the file moc_myfile.cpp in the project as well.
In some IDEs that is what the Qt plugin does for you; It automates all those steps or uses qmake which does not require explicit moc'ing. In Visual Studio I just use Custom Build Steps.
Try taking out the call to center() in the Communication constructor. I believe this might be causing your errors.