Problems including GLFW header in c++ program - opengl

I need to include GLFW header in my c++ program. The installation was fine and the GLFW folder exists in my usr\include folder and g++ does look for header files in that folder. Despite that it throws an error telling me that the GLFW directory doesnt exist.
I am using sublime text as my editor and IDE and my system is Ubuntu-20.04
FOllowing is the code, the terminal command i used to compile and the error message i encountered:
#include <GLFW\glfw3native.h>
#include <iostream>
int main(int argc, char const *argv[])
{
std::cout << "All DOne!!" << std::endl;
return 0;
}
g++ -g -o bin/debug/main src/*.cpp -x64 -std=c++17 -Wall -I -include -lglfw3 -lGL -lm -lXrandr -lXi -lX11 -lXxf86vm -lpthread && ./bin/debug.main
src/main.cpp:1:10: fatal error: GLFW\glfw3.h: No such file or directory
1 | #include <GLFW\glfw3.h>
| ^~~~~~~~~~~~~~
compilation terminated.
I cannot tell where the problem lies. please help.

and my system is Ubuntu-20.04
On Linux, the path delimiter is /, not \, so
#include <GLFW/glfw3native.h>
Note that while windows primarily uses backslash, it also accepts the forwad slash, so always using / is also the best option for cross-platform programming.

Related

Geany, g++ and SDL errors in compilation

So, I was following a simple C++ with SDL tutorial for linux but i encounter some errors on my way.
First of all I'm using Geany and i downloaded the corresponding SDL2 libs, here is the thing:
in my project folder there is a main.cxx file, which i open with geany as i mentioned before:
I included this libraries:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
First i encountered a pelculiar error, compilation performs sucessfully but when it comes to build i got this error:
main.cxx: undefined reference to `SDL_Init'
After searching a bit i found out that i had to add the tag -lSDL to my geany build options so they would end up being somethinf like this:
Compile:
g++ -Wall -c -lSDL "%f"
Build:
g++ -Wall -o -lSDL "%e" "%f"
But there is a problem, now when I execute the build command i get a:
G ++: error: main: There is no such file or directory
Why am i getting this error, am I including a wrong library or g++ has problems with .cxx files?
I already tried converting between .cxx and .cpp.
Thanks in advance.
g++ -Wall -c -lSDL2 "%f"
There is absolutely no need to specify libraries during compilation phase. Remove -lSDL.
g++ -Wall -o -lSDL2 "%e" "%f"
It invokes compiler, implies linking (no -c or other operation-specific flags), and sets output file name to -lSDL2. That is, linker will output resulting binary in a file named -lSDL2 in current working directory. Then, when it comes what files to link, it goes main, which supposed to be -o main, but since you've broken flags order it is now just ordinary file name that linker will try to link into resulting binary. It so happens that this file doesn't exist.
Long story short, make correct linking line - g++ -o "%e" %f -lSDL2 (libraries comes last, library order is also important).

Compiling a program that is dependent on boost libraries with only .so or .a files

I have a small test case program just to see if boost works on some system.
#include <iostream>
#include <boost/math/distributions/normal.hpp>
#include <boost/math/distributions/chi_squared.hpp>
using namespace std;
int main(int argc, char* argv[]) {
// Boost test
boost::math::normal std_normal;
double x = 1.5;
cout << boost::math::cdf(std_normal, x) << endl;
boost::math::normal non_std_normal(1.5, 2);
cout << boost::math::cdf(non_std_normal, x) << endl; // should output 1/2
// Test the chi-squared inverse
int degree_of_freedom = 19;
boost::math::chi_squared chi_dist(degree_of_freedom);
cout << boost::math::quantile(complement(chi_dist, 0.05)) << endl;
return 0;
}
I ssh to some server and they only allow me to use boost libraries through some directory full of shared objects and archive files (i.e. libboost_log.so, libboost_math_c99.a, etc.).
To be honest, I have no clue how to use these files.
I tried (for both g++ and gcc)
g++ test.cpp -o test -l /share/apps/boost/1.55.0/lib
g++ test.cpp -o test -l /share/apps/boost/1.55.0/lib -lboost_system -lboost_filesystem
g++ -std=c++11 -pedantic test.cpp -I/share/apps/boost/1.55.0/include/ -o test
g++ test.cpp -o test -I /share/apps/boost/1.55.0/include -lboost_system -lboost_filesystem
where /share/apps/boost/1.55.0/lib is the directory for the .so and .a files
and /share/apps/boost/1.55.0/include is the directory for the .hpp files.
I was denied permission for the 3rd command with the following output:
In file included from
/share/apps/boost/1.55.0/include/boost/math/special_functions/detail/round_fwd.hpp:11:0,
from /share/apps/boost/1.55.0/include/boost/math/special_functions/math_fwd.hpp:26,
from /share/apps/boost/1.55.0/include/boost/math/special_functions/erf.hpp:13,
from /share/apps/boost/1.55.0/include/boost/math/distributions/normal.hpp:19,
from test.cpp:12: /share/apps/boost/1.55.0/include/boost/config.hpp:30:29: fatal error:
/share/apps/boost/1.56.0/build/boost_1_56_0/boost/config/user.hpp:
Permission denied
I received the error for the fourth command:
fatal error: boost/math/distributions/normal.hpp: No such file or
directory.
you want to use
g++ -isystem /share/apps/boost/1.55.0/include -L /share/apps/boost/1.55.0/lib test.cpp -o test -lboost_system -lboost_filesystem
-isystem tells the compiler where to look for system header files. And -L tells the linker where to look for libraries. It's not obvious if you need the boost filesystem or system libraries based on your code snippet.
If you're unable to read the boost headers or shared libraries on the remote server, this is unrelated to your question. Contact your sys admin for help.

Problems setting up Ncurses and C++

I'm fairly new to C++ and I'm trying to set up ncurses, but I can't get it to work.
Here's the code:
#include <iostream>
#include <string>
#include <ncurses.h>
int main(){
initscr();
printw("Hello World !!!");
refresh();
getch();
endwin();
return 0;
}
With this file I get 'undefined reference' errors
And here is the makefile:
main.o: main.cpp ncurses.h
g++ main.cpp -o crawler -lncurses
The error I get with the makefile is :
make: *** No rule to make target `ncurses.h', needed by `main.o'. Stop.
Thanks for your help!
Note: I am using Ubuntu 12.04 with Geany and g++
You should remove ncurses.h dependency from Makefile. You Makefile should look like this:
main.o: main.cpp
g++ main.cpp -o crawler -lncurses
make tries to find ncurses.h in current working directory, but it is not available there. So make indicates error.
Also, there is no need of iostream and string headers in your code, because string header is included by iostream and you are not using any functions from both of headers.

Compiling issues with boost

I'm having problems with compiling a program which includes "boost/asio.hpp".
Compiling this program(taken from boost site):
example.cpp:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
with
c++ -I path/to/boost_1_55_0 example.cpp -o example
works fine.
But when the program includes:
boost/asio.hpp
And I'm trying to compile it with:
g++ -I /usr/local/boost_1_55_0 example.cpp -o example -lboost_system -lboost_thread
an executable is generated ,but I'm getting this error when trying to execute "example":
./example: error while loading shared libraries: libboost_system.so.1.55.0: cannot open shared object file: No such file or directory
The file "libboost_system.so.1.55.0" is located at "/usr/local/lib".
I also tried to compile the program with :
g++ -I /usr/local/boost_1_55_0 -L/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
And got the same error.
How can I fix this?
You need to tell the linker where to find the library it needs. I prefer RPATH for this:
g++ -I /usr/local/boost_1_55_0 -Wl,-rpath=/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
That bakes /usr/local/lib into the executable so ld can find it later. You can see what ld will load by running ldd example after building. I bet right now it says "not found" and after adding RPATH it will find the library.
Another option is to set /usr/local/lib as a system search path in your /etc/ld.so.conf, but that's quite a bit more heavyweight.
set up LD_LIBRARY_PATH as export LD_LIBRARY_PATH= path to boost

Setting up OpenGL NetBeans project with GLFW on Ubuntu

I am trying to setup OpenGL developing environment on Ubuntu.I installed all the libs including GLFW as I don't want to use GLUT.GLEW lib has been installed too.I am trying to set it all in NetBeans.I have never used it before and currently I am getting :
undefined reference to `glfwInit' error while running this simple code:
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <GL/glew.h>
#include <GL/glfw.h>
#include "glm.hpp"
using namespace std;
using namespace glm;
int main(int argc, char** argv) {
/* init GLFW */
if(!glfwInit()){
fprintf(stderr,"failed to init GLFW");
}
return 0;
}
I am sure it is linker related problem.So I have added libglfw.a to Linker->Libraries and then got even more errors like these:
make[2]: Entering directory /home/sasmaster/NetBeansProjects/OpenGLDemo'
mkdir -p dist/Debug/GNU-Linux-x86 g++ -o dist/Debug/GNU-Linux-x86/opengldemo build/Debug/GNU-Linux-x86/main.o /usr/lib/libglfw.a
/usr/lib/libglfw.a(window.o): In functionglfwOpenWindow':
/usr/lib/libglfw.a(x11_init.o): In function _glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function_glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function _glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function_glfwPlatformInit':
/usr/lib/libglfw.a(x11_init.o): In function _glfwPlatformTerminate':
/usr/lib/libglfw.a(x11_init.o): In function_glfwPlatformTerminate':
/usr/lib/libglfw.a(x11_window.o): In function translateKey':
/usr/lib/libglfw.a(x11_window.o): In functiontranslateKey':
/usr/lib/libglfw.a(x11_window.o): In function translateKey':
/usr/lib/libglfw.a(x11_window.o): In functiontranslateChar':
...........
........................
What else should I link or install?
Update:
Here it is said one should add LIBRARIES='-pthread -lglfw -lGL -lX11 -lGLU -lXxf86vm'
But where should I add those in NetBeans properties? I tried put into the MakeFile and "linker options" too and nothing helped.
Update1
Compiling the main.cpp manually using this command:
**g++ main.cpp -o Game -lglfw -lGL -lGLU -lX11 -lpthread -lXxf86vm -lm**
Compiles fine.How do I link all these libs via NetBeans???
I figured out the solution.For those interested:
Openg Project properties.Then "Linker" ->"Libraries".
In the Libraries dialog:
Click "Add Option" -> "Other Option".
In the textfield insert these params:
-lGLEW -lglfw -lGL -lGLU -lX11 -lpthread -lXxf86vm -lm
I guess lGLU is needed only if you use the old OpenGL version.