Setting up SDL2 with Eclipse and MinGW on Windows - c++

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.

Related

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 to GLFW / Vulkan although libraries seem to be linked

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

Eclipse CDT undefined reference (package that was downloaded via MYSYS2)

So I basically only coded in Java at College, this time
I want to start my new Project in C++ and want to keep the Eclipse IDE that I have always used. I need the openCV and Tesseract packages (import). I have googled and researched for quite a time and I seem to be doing it right? but maybe some of you can tell me otherwise.
What I did:
Downloaded Eclipse CDT
Downloaded MYSYS2
Followed this instructions (MinGW Compiler)
Open MSYS2 shell from start menu
Run pacman -Sy pacman to update the package database
Re-open the shell, run pacman -Syu to update the package database and core system packages
Re-open the shell, run pacman -Su to update the rest
(Reference)
For 64 bits, run pacman -S mingw-w64-x86_64-toolchain
Select which package to install, default is all
You may also need make, run pacman -S make
Installed the libraries/tools that i need
OpenCV
pacman -S mingw64/mingw-w64-x86_64-opencv
Tesseract
pacman -S mingw64/mingw-w64-x86_64-tesseract-ocr
Included the MinGW to PATH (system environment variables)->restart computer
Started a new Eclipse Project-> C++ -> choose MinGW GCC as Toolchain
Basic Hello World -> Works fine
Basic OpenCV example -> doesnt work
It seems to Resolve the Inclusions correctly.
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
No Errors there.
FullCode:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
What Eclipse says:
16:54:43 **** Incremental Build of configuration Debug for project hello ****
Info: Internal Builder is used for build
g++ -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\hello.o" "..\\src\\hello.cpp"
g++ -o hello.exe "src\\hello.o"
src\hello.o: In function `main':
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:17: undefined reference to `cv::imread(cv::String const&, int)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:25: undefined reference to `cv::namedWindow(cv::String const&, int)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:26: undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
C:\Users\Vaio\Desktop\EclipseProjekte\hello\Debug/../src/hello.cpp:28: undefined reference to `cv::waitKey(int)'
src\hello.o: In function `cv::String::String(char const*)':
C:/msys64/mingw64/include/opencv2/core/cvstd.hpp:602: undefined reference to `cv::String::allocate(unsigned long long)'
src\hello.o: In function `cv::String::~String()':
C:/msys64/mingw64/include/opencv2/core/cvstd.hpp:648: undefined reference to `cv::String::deallocate()'
src\hello.o: In function `cv::Mat::~Mat()':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:692: undefined reference to `cv::fastFree(void*)'
src\hello.o: In function `cv::Mat::release()':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:804: undefined reference to `cv::Mat::deallocate()'
src\hello.o: In function `cv::Mat::operator=(cv::Mat&&)':
C:/msys64/mingw64/include/opencv2/core/mat.inl.hpp:1371: undefined reference to `cv::fastFree(void*)'
collect2.exe: error: ld returned 1 exit status
16:54:46 Build Finished (took 2s.908ms)
It can't find the librarys????
Whats the point of downloading it through MSYS2 if it doesnt connect the library like it does with iostream
Do I need to add all the library objects to the linker settings C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries
Okay so I guess that its obv but for those who have the same mistake, MSYS2 just added all opencv and tesseract files.
If you want to use them you need to specify it to the Linker.
filename: libopencv_core.dll.a
you need to exclude the lib at the beginning and the .dll and .a
linker: opencv_core
all libs can be found on the mingw path: (C:\msys64\mingw64\lib)
in the end you link it with -lopencv_core
or through the Eclipse GUI C/C++ Build -> Settings -> Tool Settings -> GCC C++ Linker -> Libraries -> add library -> opencv_core

gtest and MinGW linkage

I've made a simple main to check if gtest was correctly linked and got "undefined reference" errors. The first line in the main function throws an undefined reference to `testing::InitGoogleTest(int*, char**)'.
#include <gtest/gtest.h>
int main(int argc, char* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
I have successfully built a gtest Code::Block project with the help of cmake and without the pthread library. Then, I compiled everything with MinGW. I took the libgtest.a libgmock.a and linked them in my Code::Block project with no problems. I also specified the include directories and the binary directories. Is it possible that I need to include gtest_main.a too?
The build log (I added "..." to hide long chains of folders):
"obj\Debug gtest\MainTest.o" -lsfml-graphics-s-d -lsfml-window-s-d -lsfml-network-s-d -lsfml-audio-s-d -lsfml-system-s-d -lglew -lgtest -lgmock -lws2_32 -lopenal32 -lsndfile -lgdi32 -lwinmm -lfreetype -ljpeg -lglu32 -lopengl32
obj\Debug gtest\MainTest.o: In function main':
D:/.../Templates/SFML 2_2 BasicScene/MainTest.cpp:5: undefined reference totesting::InitGoogleTest(int*, char**)'
obj\Debug gtest\MainTest.o: In function Z13RUN_ALL_TESTSv':
D:/.../googletest/googletest/include/gtest/gtest.h:2237: undefined reference totesting::UnitTest::GetInstance()'
D:/.../googletest/googletest/include/gtest/gtest.h:2237: undefined reference to `testing::UnitTest::Run()'
I finally found it! It was tricky: as I am using Windows 7 and installed multiple compilers (3 versions of MinGW and 2 of TDM), my PATH got all mixed up. When I was using cmake-gui 3.3, I was using the default compiler, which was MinGW 4.7 when I installed Code::Blocks. After installing all the compilers mentionned above, the default compiler path changed for a higher version of MinGW. Linking with a library that was compiled with a newer version of MinGW didn't cause the library to not be found, but instead caused linking errors.
In conclusion, I was using two differents versions of the same compiler.

Compile OpenGL application from Windows on Linux

My friend made OpenGL graphic engine, but he is working on Windows. I want to compile project with it.
I installed all required libs with headers, but now problem is with linking (project in Code::Blocks). I found paths for /usr/lib/libSOIL.a and /usr/local/lib/libglfw3.a, but what about:
C:\Program Files (x86)\CodeBlocks\MinGW\lib\libopengl32.a
C:\Program Files (x86)\CodeBlocks\MinGW\lib\assimp_debug-dll_win32\assimp.lib
Also, what I must modify in project file to compile it? It requires: glfw3, glm, gl3w.h, assimp, SOIL (this is what I get from .hpp files). I installed all headers (downloaded sources and make && make install)...
I tried to compile it from terminal with g++, but I don't know switches for libraries.
Current situation:
$ g++ Camera.o Entity.o Frustum.o gl3w.o Light.o Material.o Mesh.o Model.o ModelPart.o Shader.o Texture.o Utilities.o ../main.o -o main -L/usr/local/lib/libglfw3.a -lX11 -lXext -lXt -lSM -lGLU -lglut -lSOIL
/usr/bin/ld: gl3w.o: undefined reference to symbol 'glXGetProcAddress'
/usr/bin/ld: note: 'glXGetProcAddress' is defined in DSO /usr/lib/nvidia-313-updates/libGL.so.1 so try adding it to the linker command line
/usr/lib/nvidia-313-updates/libGL.so.1: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
(i added too much libraries to command line, I know)
EDIT
Added -lGL and -ldl and some problems comes out. Now, I'll trying compile it with makefile...
libopengl32 -> libGL.a
assimp -> libassimp.a ?
You gotta provide the Makefile you're compiling it with.