Can't access SDL library when trying to compile using g++ - c++

I'm getting started with SDL -- that is, I have no experience and am basically just trying to get something to compile at this point.
I have a basic program:
#include "sdl/SDL.h"
int main(){
// Fire up SDL, this starts all subsystems; audio video etc.
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
// Now Shut it down
atexit(SDL_Quit);
return 0;
}
And I have this which I'm typing into the terminal:
g++ 'sdl-config --cflags --libs' -o sdltest sdltest.cpp
where sdltest.cpp is the name of the file.
This gives me the error:
g++: error: stl-config --cflags --libs: No such file or directory
Now, previously I have (I think) installed SDL like so:
sudo apt-get install libsdl2-dev
The issue might have to do with a faulty installation -- this might not be the package I was supposed to install, or I might be completely forgetting a step here.
I'm using Ubuntu, and compiling using g++, if it wasn't apparent from context.

Related

Unable to display image opencv (c++)

I finally managed to build the opencv4.5.4 library from source but now I'm facing errors that I'm unable to fix.
I'm using this medium article as my guide https://medium.com/analytics-vidhya/how-to-install-opencv-for-visual-studio-code-using-ubuntu-os-9398b2f32d53
When I try to execute a simple program that prints the version of opencv installed, it executes without errors.
#include <opencv2/opencv.hpp>
#include <iostream>
int main()
{
std::cout << "OpenCV Version: "<< CV_VERSION << std::endl;
return 0;
}
makefile:
CC = g++
PROJECT = new_output
SRC = new.cpp
LIBS = `pkg-config --cflags --libs opencv4`
$(PROJECT) : $(SRC)
$(CC) $(SRC) -o $(PROJECT) $(LIBS)
Output:
username#Inspiron-7591:~/SeePluPlu/opencv-test$ sudo make
g++ new.cpp -o new_output `pkg-config --cflags --libs opencv4`
username#Inspiron-7591:~/SeePluPlu/opencv-test$ sudo ./new_output
OpenCV Version: 4.5.4-dev
Now when I try to run another program to display an image things get out of hand really quick.
#include <iostream>
#include <opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
using namespace cv;
using namespace std;
// Driver code
int main(int argc, char** argv)
{
// Read the image file as
// imread("default.jpg");
Mat image = imread("lena.jpg",IMREAD_GRAYSCALE);
// Error Handling
if (image.empty()) {
cout << "Image File "
<< "Not Found" << endl;
// wait for any key press
cin.get();
return -1;
}
// Show Image inside a window with
// the name provided
imshow("Window Name", image);
// Wait for any keystroke
waitKey(0);
return 0;
}
Output:
username#Inspiron-7591:~/SeePluPlu/opencv-test$ ./new_output
Gtk-Message: 18:49:40.321: Failed to load module "atk-bridge"
Gtk-Message: 18:49:40.324: Failed to load module "canberra-gtk-module"
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.4-dev) /home/username/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 120542625076320 bytes in function 'OutOfMemoryError'
Aborted (core dumped)
but when I give root privilages...
username#Inspiron-7591:~/SeePluPlu/opencv-test$ sudo ./new_output
Gtk-Message: 18:49:31.985: Failed to load module "canberra-gtk-module"
terminate called after throwing an instance of 'cv::Exception'
what(): OpenCV(4.5.4-dev) /home/username/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 112126257730176 bytes in function 'OutOfMemoryError'
Aborted
when I rerun the binary file (./new_output) over and over again I end up getting assertion errors as well.
I searched for loading the canberra-gtk-module and atk-bridge but whatever I found was not of any help
https://askubuntu.com/a/565789
https://askubuntu.com/a/1300284
https://askubuntu.com/questions/342202/failed-to-load-module-canberra-gtk-module-but-already-installed (all solutions in this thread)
Note: I'm positive that the image is being read and I'm able to print its size using image.size() function and I think it has something to do with the imshow() function... Not sure.
Any help or detail is very much appreciated. Thanks in advance!
When I tried to build opencv4 again, I was able to figure out that cmake was unable to find the gtk+-3.0 module that's installed in my system.
username#Inspiron-7591:~$ pkg-config --modversion gtk+-3.0
Package gtk+-3.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `gtk+-3.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'gtk+-3.0' found
even though it was already installed...
username#Inspiron-7591:~$ sudo apt-get install libgtk-3-dev
[sudo] password for username:
Reading package lists... Done
Building dependency tree
Reading state information... Done
libgtk-3-dev is already the newest version (3.24.20-0ubuntu1).
libgtk-3-dev set to manually installed.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
I stumbled upon this gem of a thread while googling my issue https://stackoverflow.com/a/50038996/15341103 and I was able to make pkgconfig detect gtk+-3.0
I rebuilt opencv4 again this time and it works!
The error is not related to the image per se, its possibly an unitialized variable passed to a memory allocation routine.
what(): OpenCV(4.5.4-dev) /home/username/opencv/modules/core/src/alloc.cpp:73: error: (-4:Insufficient memory) Failed to allocate 112126257730176 bytes in function 'OutOfMemoryError'
Put a break point in alloc.cpp to figure out who is requesting allocate 112126257730176 bytes

C++ Include path on x86_64-w64-mingw32-g++ with OpenGL

I am trying to get an OpenGL program working on both linux and windows.
Here's my code [file=main.cc]:
#include <iostream>
#include "GL/glew.h"
using namespace std;
int main(int argc, char *argv[]){
cout << "Hello World\n";
return 0;
}
Simple enough. I'm on Linux and using
g++ main.cc -lGL -lGLEW -lSDL2
to compile my program. It works perfectly fine and if I run ./a.out I get a Hello World on my screen.
Then I try to compile it on Linux for Windows using the command
x86_64-w64-mingw32-g++ main.cc -lGL -LGLEW -LSDL2
Then however i get the error:
main.cc:3:21: fatal error: GL/glew.h: No such file or directory
#include "GL/glew.h"
^
compilation terminated.
I've already tried adding the -I/inclulde/path option with paths like /usr/include /usr/include/GL usr/include and the like, yet nothing wants to compile.
The Libraries that I'm using (or planning to) were installed using
#apt install libgl-dev libglew-dev libsdl2-dev
Any help would be very much appreciated (although I feel this is an incredibly easy fix that I'm just too stupid to figure out on my own)

unidentified reference to glutInit

I tried to compile a basic OpenGL program, simply just a blank window.
I'm using CodeLite with the g++ compiler on Linux Mint 18.1 (Ubuntu 16.04 LTS).
The code so far is:
#include <stdio.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
int main(int argc, char **argv)
{
glutInit(&argc, argv);
printf("hello world\n");
return 0;
}
At first my compiler (G++ in CodeLite) gave the error
/home/USER/Projects/CodeLite/Graphical/main.cpp:7: undefined reference to `glutInit'
I downloaded all the up to date GLEW and GLUT includes from their respective websites and unpacked them to /usr/include/GL, I edited the project settings linker like this.
Now it gives the error message:
/usr/bin/ld: cannot find -lglut
Makefile:4: recipe for target 'All' failed
What can I do to fix this?
You should install the libraries from the package manager instead: libglew-dev, freeglut3-dev, libgl1-mesa-dev and libglu1-mesa-dev. The includes and binary files will be placed in the appropriate location.
If the linker cannot find the .so files, locate them with dpkg -L freeglut3-dev and add this directory in the linker command line
g++ -L/path/to/libglut.so *.o -o programname

Compiling Allegro 5 Program through command line on a Mac

I followed to steps to build and install Allegro 5 from their wiki (found here: https://wiki.allegro.cc/index.php?title=Main_Page) and seemingly succeeded with no problems.
allegro was installed to the following (as the wiki suggests) /usr/local/include and usr/local/lib and I have confirmed allegro is there.
I then wrote the following code in vim:
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv)
{
ALLEGRO_DISPLAY *display = NULL;
if(!al_init())
{
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display)
{
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
I am new to using Unix and have only ever compiled c++ programs with g++ that were simple hello world files with no libraries needed.
Therefore after searching around on the internet I tried the following commands:
g++ hello.cpp -o hello `pkg-config --libs allegro-5`
resulting in the following:
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
(maybe you meant: __al_mangled_main)
ld: symbols not found for architecture x86_64
clang: error: linker command failed with exit code 1
BTW, I used homebrew to install dependencies instead of macports
brew install pkg-config
brew install zlib
etc...
It seems like a linking problem.
What am I doing wrong?
try install allegro with homebrew and use gcc -o main main.c -lallegro -lallegro_main
because the allegro_main is a compatibility library that allows the main function to work on all compilers. Required only from OS X.

undefined reference to glfwSetErrorCallback

I'm trying to use the GLFW library, but am having difficulty with compiling a simple program. I went to the GLFW website and download the latest release, then using "How to build & install GLFW 3 and use it in a Linux project" I built and installed it.
Here's my code:
#include <GLFW/glfw3.h>
#include <iostream>
using std::cout;
using std::endl;
void GLFW_error(int error, const char* description)
{
fputs(description, stderr);
}
void run()
{
cout << "pooch" << endl;
}
int main()
{
glfwSetErrorCallback(GLFW_error);
if (!glfwInit()) exit(EXIT_FAILURE);
run();
glfwTerminate();
return 0;
}
Using the command line:
bulletbill22#ROBOTRON ~/Desktop $ g++ -std=c++11 -lglfw source.cpp
yields
source.cpp:function main: error: undefined reference to 'glfwSetErrorCallback'
glfwSetErrorCallback is taken from their tutorial for "Setting an error callback".
Inclusion of -glfw3 results in /usr/bin/ld: error: cannot find -lglfw3
Even though everything seemed to be installed correctly, I suspect the problem may lie somewhere with the installation of the GLFW library because I'm not used to CMake and don't entirely understand how it works. I'm frustrated because the answer must be simple, but I'm not sure which keywords are really relevant when googling the problem; mostly the results are people who were incorrectly compiling with CMake, which I'm not compiling with in this case.
It seems that the directories for the glfw3.h header and libglfw3.so (and/or libglfw3.a) library are not in the default path.
You can check with by adding the -v option to the g++ options. Locate the directory where the glfw3.h header is found - call this $GLFW_INCDIR - it typically ends with .../GLFW. Locate the directory where the library is found - call this $GLFW_LIBDIR. Try:
g++ -std=c++11 -I$GLFW_INCDIR source.cpp -o pooch -L$GLFW_LIBDIR -lglfw3
If all the library dependencies are satisfied, this hopefully results in a program called pooch.
One other thing: GLFW3 is a C library, and the callback function arguments are expected to be C functions. So your callback should have 'C' linkage, i.e.,
extern "C" void GLFW_error(int error, const char* description) ...
Also, if you're having trouble with cmake, you may have ccmake installed. Try ccmake . in the top-level directory of the GLFW3 package for 'interactive' configuration.