Undefined references to GLFW / Vulkan although libraries seem to be linked - c++

I'm a C++ beginner with some experience in Java trying to set up a project using GLM, GLFW, and Vulkan on Windows. This is going to be my first time getting my hands dirty in a lower-level language like C++. I'm having a lot of trouble getting the compiler to link the Vulkan and GLFW libraries to my project. I'm following the tutorial here at vulkan-tutorial.org to get started. Here's the code in main.cpp:
#define GLFW_INCLUDE_VULKAN
#include <GLFW/glfw3.h>
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <iostream>
int main() {
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
GLFWwindow* window = glfwCreateWindow(800, 600, "Vulkan window", nullptr, nullptr);
uint32_t extensionCount = 0;
vkEnumerateInstanceExtensionProperties(nullptr, &extensionCount, nullptr);
std::cout << extensionCount << " extensions supported\n";
glm::mat4 matrix;
glm::vec4 vec;
auto test = matrix * vec;
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Here are the commands being used to compile it:
g++ -std=c++11 -fexceptions -g -IC:/glfw-3.2.1/include -IC:/glm-0.9.9.1/glm -IC:/VulkanSDK/1.1.82.1/Include -IC:/glfw-3.2.1/include -c "src/main.cpp"
g++ -LC:/glfw-3.2.1/lib-mingw-w64 -LC:/VulkanSDK/1.1.82.1/Lib -o VulkanTest.exe main.o -lglfw3 -lvulkan-1
The first command compiles the .cpp into a .o successfully, but the second command gives me errors from the linker. Every single reference I made to a member from Vulkan or GLFW is undefined. (Path has been shortened for easier reading)
[omitted]/src/main.cpp:12: undefined reference to `glfwInit'
[omitted]/src/main.cpp:14: undefined reference to `glfwWindowHint'
[omitted]/src/main.cpp:15: undefined reference to `glfwCreateWindow'
[omitted]/src/main.cpp:18: undefined reference to `vkEnumerateInstanceExtensionProperties#12'
[omitted]/src/main.cpp:26: undefined reference to `glfwWindowShouldClose'
[omitted]/src/main.cpp:27: undefined reference to `glfwPollEvents'
[omitted]/src/main.cpp:30: undefined reference to `glfwDestroyWindow'
[omitted]/src/main.cpp:32: undefined reference to `glfwTerminate'
It seems like the linker can't find the library files I provided with -L and -l, but if I change -lglfw3 to -llibglfw3.a or -lglwf3.dll, I get this:
[omitted]/mingw32/bin/ld.exe: cannot find -llibglfw3.a
or
[omitted]/mingw32/bin/ld.exe: cannot find -lglfw3.dll
Leading me to think that the linker DID find the libraries at first, since it didn't complain about being unable to find the library - but why can't it find sources for the references to GLFW / Vulkan functions? I have no idea what's happening. Is it finding the library files?
I'm using GLFW 3.2.1, Vulkan SDK 1.1.82.1, MingW GCC version 6.3.0, and I'm running on Windows 10 Pro 64-bit.

Turns out my problem was rooted in using an old version of MingW. Originally I downloaded MingW from here, which was recommended by the guide on MingW's wiki. I updated to 8.1.0 from this site and the linker began throwing errors about undefined references to different functions, such as "__imp_CreateDCW" and "__imp_SwapBuffers".
I happened to recognize these as GDI functions from some other research I did. I added GDI32 to the libraries option in the linker and the build went through successfully.
Now my build commands are as follows:
g++ -std=c++11 -fexceptions -g -IC:/glfw-3.2.1/include -IC:/glm-0.9.9.1/glm -IC:/VulkanSDK/1.1.82.1/Include -c "src/main.cpp"
g++ -LC:/glfw-3.2.1/lib-mingw-w64 -LC:/VulkanSDK/1.1.82.1/Lib -o VulkanTest.exe main.o -lglfw3 -lvulkan-1 -lgdi32

Related

How to include and use any of the stuff in the subdirectories of `C:\msys64\mingw64\include`

In my VSCode environment with working msys2 / mingW64 /gcc compilers i'm now trying to use ncurses / curses.
msys is in the default installpath, so gcc / g++ /gdb are in
C:\msys64\mingw64\bin
As i understand it, the headerfiles being used by directives are the ones in
C:\msys64\mingw64\include.
So in general i think this is a question how to properly include and use any of the stuff in the subdirectories of C:\msys64\mingw64\include
For my case with ncurses, in include directory there are two subdirectories with names ncurses and ncursesw with identical content.
To begin, i try with a very simple `helloCurses.cpp file
#include <ncurses/curses.h>
using namespace std;
int main(int argc, char ** argv)
{
// init screen and sets up screen
initscr();
// print to screen
printw("Hello World");
// refreshes the screen
refresh();
// pause the screen output
getch();
// deallocates memory and ends ncurses
endwin();
return 0;
}
obviously the compiler can find the ncurses/curses.h file.
But it still doesn't compile.
I'm getting the error messages:
Executing task: C/C++: MingW g++.exe build active file
Starting build...
C:/msys64/mingw64/bin/g++.exe -fdiagnostics-color=always -g D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.cpp -o D:\GitHub\Cpp-Code-priv\ncurses\helloCurses.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\Mathias\AppData\Local\Temp\cc44BFtA.o: in function `main':
D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:7: undefined reference to `__imp_initscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:10: undefined reference to `__imp_printw'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:13: undefined reference to `__imp_refresh'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_stdscr'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:16: undefined reference to `__imp_wgetch'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/GitHub/Cpp-Code-priv/ncurses/helloCurses.cpp:19: undefined reference to `__imp_endwin'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
As far as I understand the linker will not find the ncurses library. This should be libncurses.a or libncurses.dll.
These files are in the directory C:\msys64\mingw64\lib.
In the end i like to compile with VSCode tasks.json, but to begin with, the commandline
gcc -I C:\msys64\mingw64\include\ncurses -L C:\msys64\mingw64\lib -lncurses helloCurses.cpp -o helloCurses.exe
didn't work either and still gives me the same errormessage.
I have been pointed to
Why does the order in which libraries are linked sometimes cause errors in GCC?
I'm sure this will at some point become valuable information, but right now this isn't about the order of a multitude of probably codependent libraris. This is just about how to include a very first additional library not being basic enough to be includd by a simple
#include <stdio.h>, but still basic enough to be in the full toolchain being installed bypacman -S --needed base-devel mingw-w64-x86_64-toolchain

Freeglut 64 bit program linking errors

I'm trying to compile a 64 bit version of an OpenGL C++ program using freeglut. I followed the exact instructions on this website to set up freeglut with MinGW. I have the header files in C:\MinGW\include\GL, I have the 32 bit libraries in C:\MinGW\lib and 64 bit libraries in C:\MinGW\lib\x64, and I have the 64 bit freeglut.dll in my project directory. However, even the simplest of OpenGL programs don't link successfully...
My code is minimal:
// test.cpp
#include <GL/glut.h>
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
}
And I compile it with the exact commands given on the readme/on the website:
g++ -c -o test.o test.cpp -I"C:\MinGW\include"
g++ -o test.exe test.o -L"C:\MinGW\lib\x64" -lfreeglut -lopengl32 -Wl,--subsystem,windows
(except of course I changed the directories and changed gcc to g++)
The compile runs fine, but linking throws these error messages:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x1c): undefined reference to `_imp____glutInitWithExit#12'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x3f): undefined reference to `_imp____glutCreateWindowWithExit#8'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(.text+0x61): undefined reference to `_imp____glutCreateMenuWithExit#8'
collect2.exe: error: ld returned 1 exit status
I've tried to change -lopengl32 in the command to -lopengl64 and -lopengl, but the linker couldn't find those libraries.
I can't answer your specific case, but another approach would be to use mingw-w64, with MSYS2 as a package manager. The old mingw.org project is not well maintained, unlike mingw-w64 and the MSYS2 package database. There is probably some mismatch between how your precompiled binaries were built and the compiler you are currently using.
On my system I compiled your code with g++ -o gl gl.cpp -lfreeglut and it worked first time .
How to install MinGW-w64 and MSYS2?
Then use pacman -Ss glut to find the freeglut package.

Undefined references in static OpenCV libraries

I have a project in C++ that uses OpenCV 3.1 and works fine using shared libaries. But now I want to compile it using static libraries (located in a folder within the project directory) because I want to be able to export it (and also edit and recompile if necessary) where OpenCV is not installed.
I have recompiled OpenCV this time setting shared libs to NO:
make -DCMAKE_BUILD_TYPE=RELEASE -DBUILD_SHARED_LIBS=NO -DCMAKE_INSTALL_PREFIX=~/Desktop/ocv ..
Then I took my required libraries:
libopencv_core.a libopencv_imgproc.a libopencv_highgui.a
libopencv_video.a libopencv_imgcodecs.a libopencv_videoio.a
and ran g++ a.cpp libopencv_core.a where a.cpp is a sample program to test if everything works:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat a;
printf("hello world\n" );
return 0;
}
My problem is that I can not link the first library (core) because I get lots of undefined references like this:
libopencv_core.a(system.cpp.o): In function `cv::Mutex::Mutex()':
system.cpp:(.text._ZN2cv5MutexC2Ev+0x2c): undefined reference to `pthread_mutexattr_init'
system.cpp:(.text._ZN2cv5MutexC2Ev+0x39): undefined reference to `pthread_mutexattr_settype'
system.cpp:(.text._ZN2cv5MutexC2Ev+0x4c): undefined reference to `pthread_mutexattr_destroy'
libopencv_core.a(system.cpp.o): In function `cv::Mutex::trylock()':
system.cpp:(.text._ZN2cv5Mutex7trylockEv+0x8): undefined reference to `pthread_mutex_trylock'
libopencv_core.a(system.cpp.o): In function `cv::TlsAbstraction::TlsAbstraction()':
system.cpp:(.text._ZN2cv14TlsAbstractionC2Ev+0x9): undefined reference to `pthread_key_create'
libopencv_core.a(system.cpp.o): In function `cv::TlsAbstraction::~TlsAbstraction()':
and so on. I have searched all over and cannot find what's missing. Any help is greatly appreciated.
p.s. G++ and Ubuntu version: g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
You need to link pthread library as well. And pass it as -pthread
g++ a.cpp libopencv_core.a -pthread
You're missing other libraries which contain the required code. There must be a libippicv.a which contains the code for ippicv* functions
g++ a.cpp libopencv_core.a libippicv.a -pthread
It should be somewhere among third_party libs.

Linking GLFW in CodeBlocks

CodeBlocks' GLFW Project is outdated and works only with GLFW 2.7. I am using the latest version, which is 3.0.4, and trying to link it in CodeBlocks statically (I hope that I am using the correct terminology). I would be really happy if someone told me how to do it step by step. I would also like to create an empty project if possible and do everything manually.
This is the code that I am trying to run:
#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);
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
This is what I have in my "Other linker options":
-lmingw32 -lopengl32 -lgdi32
and I also copied GLFW folder that is containing the header files in CodeBlocks/MinGW/include.
This is pretty much all I did and I get the following build log:
-------------- Build: Debug in Initializing OpenGL (compiler: GNU GCC Compiler)---------------
mingw32-g++.exe -o "bin\Debug\Initializing OpenGL.exe" obj\Debug\main.o -lmingw32 -lopengl32 -lgdi32
obj\Debug\main.o: In function `main':
D:/Development/OpenGL/Initializing OpenGL/main.cpp:8: undefined reference to `glfwInit'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:12: undefined reference to `glfwCreateWindow'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:15: undefined reference to `glfwTerminate'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:20: undefined reference to `glfwMakeContextCurrent'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:28: undefined reference to `glfwSwapBuffers'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:31: undefined reference to `glfwPollEvents'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:23: undefined reference to `glfwWindowShouldClose'
D:/Development/OpenGL/Initializing OpenGL/main.cpp:34: undefined reference to `glfwTerminate'
collect2.exe: error: ld returned 1 exit status
Jigger your project config so that the mingw invocation looks something like this:
x86_64-w64-mingw32-g++.exe -Llib -o bin\Debug\glfw-proj.exe obj\Debug\main.o -lglfw3 -lopengl32 -lgdi32
Note the -lglfw3.
Procedure:
Project -> Build options...
Select the top-level configuration (i.e., not Debug or Release)
In the Search directories -> Compiler tab add the path to your GLFW headers (GLFW/glfw3.h and friend)
In the Search directories -> Linker tab add the path to your GLFW library files (libglfw3.a and friends)
In the Linker settings tab add 3 Link libraries: glfw3, opengl32, and gdi32.

Setting up SDL2 with Eclipse and MinGW on Windows

I'm trying to create a SDL2 project with Eclipse Kepler and MinGW on Windows.
I already added SDL2 libs in MinGW (.a) in C:\MinGW\lib, SDL2 include in MinGW(C:\MinGW\include\SDL2) and I also added in projects properties -> C/C++ general -> paths and symbols -> librairies the following lines in that order :
mingw32
SDL2main
SDL2
Then I put '-mwindows' in MinGW C++ linker at the end of the line "Command line pattern"
I also added -Dmain=SDL_main for the entry point...
But the compiler gives me error :
main.cpp:7: undefined reference to `SDL_CreateWindow'
this is the code :
#include <SDL2/SDL.h>
int main(int, char**)
{
SDL_Window *pWindow = nullptr;
pFenetre = SDL_CreateWindow("Test SDL 2.0", 0, 0, 320, 240, SDL_WINDOW_SHOWN);
if (!pWindow)
{
return -1;
}
SDL_DestroyWindow(pWindow);
return 0;
}
And this is the build console :
Info: Internal Builder is used for build
g++ "-LC:\\MinGW\\lib" -o Test.exe main.o -lmingw32 -lSDL2main -lSDL2 -mwindows
main.o: In function `SDL_main':
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:7: undefined reference to `SDL_CreateWindow'
C:\Users\olivi_000\workspace\Test\Debug/../main.cpp:13: undefined reference to `SDL_DestroyWindow'
C:\MinGW\lib/libmingw32.a(main.o):main.c:(.text.startup+0xa7): undefined reference to `WinMain#16'
collect2.exe: erreur: ld a retourné 1 code d'état d'exécution
what's wrong ?
Make sure you're using the right version of the library. You can't mix 64-bit import libraries with 32-bit compiler. For the SDL2 library you downloaded(SDL2-devel-2.0.0-mingw.tar.gz) it comes with both 32-bit and 64-bit. i686-w64-mingw32 is 32-bit and x86_64-w64-mingw32 is for 64-bit.