Cannot link with glfw3.lib c++ - c++

I am trying to compile a c++ file on command line with g++
i have this file
#include <iostream>
#include "C:\Users\Shaurya\Documents\Opengl\Dependencies\GLFW\include\GLFW\glfw3.h"
using namespace std;
int main(){
GLFWwindow* window;
if(!glfwInit()){
cout << "Window not initialized";
return -1;
}
window = glfwCreateWindow(600,600,"OpenGL", NULL, NULL);
if(!window){
cout << "Window Not created";
glfwTerminate();
}
glfwMakeContextCurrent(window);
while(glfwWindowShouldClose(window)){
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
I run this command
g++ -o Test main.cpp -L <fullpath>\Dependencies\GLFW\lib-vc2019 -lglfw3.lib
but this throws a gigantic error
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: mode i386pe
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../crt2.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtbegin.o
C:\Users\Shaurya\AppData\Local\Temp\ccxDP2Km.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libstdc++.dll.a)d004332.o
.
.
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libgcc_s.a)d000122.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libgcc.a)_chkstk_ms.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/libgcc.a)_ctors.o
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmingwex.a)fesetenv.o
.
.
.
(c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../libmoldname.a)dagwbt.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/crtend.o
c:/mingw/bin/../lib/gcc/mingw32/6.3.0/../../../../mingw32/bin/ld.exe: cannot find -lglfw3.lib
collect2.exe: error: ld returned 1 exit status
It took me alot of time to realize that the -l is only looking for .o files
So how do i link with .lib files? i got the .lib from glfw.org binaries.
I dont want to use Visual Studio 2019 to do this because my laptop has a hard time running that.

So i solved this. First i was using the wrong libraries. I was provided with multiple versions of Libraries and i was using VC version. But when i used MinGW Version , it worked.
Moreover , i was using relative paths without typing ./ before them.

Related

GLAD link is not providing needed functions

TLDR: The GLAD header file won't let me use the openGL Commands and I don't know exactly why.
Here's a breakdown:
I'm on a Windows 10 Laptop.
I'm coding in C++.
I'm compiling with MinGW's g++ tool.
I'm using GLFW and GLAD.
My file layout is something like this:
OpenGLTest
include
glad
glad.h
GLFW
glfw3.h
glfw3native.h
KHR
khrplatform.h
glad.c
glfw3.dll
GraphicsTest.cpp(Main File)
libglfw3dll.a(I have forgotten what this does, it's a remnant from older attempts at openGL. Relevant?)
Makefile(a single command: "g++ GraphicsTest.cpp -g -L glad.c glfw3.dll")
As far as I can tell, the program will compile and run flawlessly if any and all commands from GLAD and openGL are commented out. All it then does is make a small window.
If said lines are not commented out, the compiler will throw a slew of errors, all following this form:
D:\Documents\Programming\C++\Programs\OpenGLTest/GraphicsTest.cpp:23: undefined reference to `gladLoadGL'
...with gladLoadGL being replaced with the relevant function.
The file itself reads thusly:
#include<iostream>
#include<glad\glad.h>
#include<GLFW\glfw3.h>
static void whatIsGoingOnSeriouslyGuysHelp(int id,const char* desc)
{
std::cout<<desc;
}
int main()
{
glfwSetErrorCallback(&whatIsGoingOnSeriouslyGuysHelp);
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(100,100,"TEST",NULL,NULL);
if (window == NULL)
{
std::cout<<"Window Creation Error";
}
glfwMakeContextCurrent(window);
gladLoadGL();
//glViewport(0,0,100,100);
while (!glfwWindowShouldClose(window))
{
//std::cout<<"?";
glfwPollEvents();
glfwSwapBuffers(window);
//glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
//glClear(GL_COLOR_BUFFER_BIT);
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
I have tried re-installing GLAD, but that didn't do anything. I haven't tried re-installing GLFW, but I don't think that's the problem.
Correct any misconceptions I have or errors I am making.
You need to include glad.c in your compile command as follows: (you had a -L before it which tells gcc to treat glad.c as a directory for libraries)
g++ GraphicsTest.cpp glad.c -g glfw3.dll
, and instead of gladLoadGL, you should use the GLFW loader:
if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress)) {
std::cout << "Failed to initialize OpenGL context" << std::endl;
return -1;
}
Finally, you should invest a tiny bit of time into a decent build system because compiling every file every time will get real slow real fast. Here is the absolute minimal Makefile to get you started:
graphicstest: graphicstest.o glad.o
g++ -g -o $# $^ glfw3.dll

How would I link external libraries to a cpp file through Emacs

I use the Emacs text editor for editing my files, and have been looking into using OpenGL in my programs. However, I have had trouble including the GLFW header into my program.
Here is my code for the #include statements and essentially test if the files linked properly, which comes from the hyperlink later on.
#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
using namespace std;
int main()
{
glewExperimental = true;
if(!glfwInit())
{
cout << "Failed to initialize" << endl;
return -1;
}
return 0;
}
I hit M-x compile then g++ file.cpp -lglut -lGL -lGLEW -lGLU -o file.out and get the following error
g++ anotherGL.cpp -lglut -lGL -lGLEW -lGLU -o anotherGL.out
anotherGL.cpp:5:10: fatal error: GLFW/glfw3.h: No such file or directory
#include <GLFW/glfw3.h>
^~~~~~~~~~~~~~
I followed the instructions for downloading the OpenGL tutorial page, under the section Building on Linux, but the instructions diverge after it moves on to the section where the packages are linked to the IDE.
(http://www.opengl-tutorial.org/beginners-tutorials/tutorial-1-opening-a-window/)
Is there a M-x command I can use in place of what I have to perform the link?
On "debian" and "red hat" systems, the include file GLFW/glfw3.h is under /usr/include a la /usr/include/GLFW/glfw3.h. Your include statement is correct as /usr/include is implicitly searched by gcc/g++. You've just missed installing the glfw development package. On "debian" or apt-based systems this is in libglfw3-dev. On "red hat" or rpm based systems this is glfw-devel. You may also need to add the flag -lglfw to your compile command.

How to link a simple libssh program

Problem: getting a simple program using the libssh library to compile successfully without getting a linker error.
Program:
#include <iostream>
#define LIBSSH_STATIC 1
#include <libssh/libssh.h>
#include <stdlib.h>
void sftpSession()
{
ssh_session test_session = ssh_new();
if (!test_session) {
throw std::runtime_error("Could not create a session?!");
return;
}
ssh_free(test_session);
std::cout << "Session created & freed successfully." << std::endl;
}
int main(int argc, char **argv)
{
std::cout << "SFTP test program" << std::endl;
try {
sftpSession();
}
catch (const std::runtime_error &e) {
std::cout << "thrown: " << e.what() << std::endl;
}
return EXIT_SUCCESS;
}
Machine is 64-bit, running Windows. Using MinGW (via CodeBlocks) and the build log (edited slightly for readability) shows the compiler command as:
x86_64-w64-mingw32-g++.exe
-Wall -fexceptions -O2 -std=c++0x -g -m64 -Dmine_dev=1
-I"C:\Program Files\zlib\zlib-1.2.8-dll\include"
-I"C:\Program Files (x86)\libssh\libssh-0.6.5\include"
-c C:\Users\ ... \SFTP_testing\main.cpp
-o obj\Release\main.o
x86_64-w64-mingw32-g++.exe
-L"C:\Program Files (x86)\Ingres\IngresII\ingres\lib"
-o bin\Release\SFTP_testing.exe
obj\Release\main.o
-s
"C:\Program Files (x86)\libssh\libssh-0.6.5\lib\libssh.dll.a"
obj\Release\main.o: In function `sftpSession()':
C:/Users/ ... /SFTP_testing/main.cpp:9: undefined reference to `ssh_new'
C:/Users/ ... /SFTP_testing/main.cpp:14: undefined reference to `ssh_free'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 2 second(s))
3 error(s), 0 warning(s) (0 minute(s), 2 second(s))
Things I have tried/investigated
I understand that the undefined reference to refers to the linker being able to find the declaration of the functions, but not their definitions.
The compiler is indeed finding the header file and the functions' definitions.
The header file does have the necessary extern "C" code, including the following:
#ifdef __cplusplus
extern "C" {
#endif
...
LIBSSH_API ssh_session ssh_new(void);
...
LIBSSH_API void ssh_free(ssh_session session);
...
#ifdef __cplusplus
}
#endif
The linker is sucessfully finding the library. (Checked via the ProcessMonitor program. It is definitely accessing the libssh.dll.a file.)
Using the nm utility I checked that the object code in libssh.dll.a did indeed contain references for ssh_new and ssh_free
Checked no confusion between 32-bit/64-bit issues: there are two versions of the pre-compiled library supplied. One is ...\libssh-0.6.5\bin\libssh.dll and the other is ...\libssh-0.6.5\lib\libssh.dll.a and the former gives me the following error: i386 architecture of input file 'C:\Program Files (x86)\libssh\libssh-0.6.5\bin\libssh.dll' is incompatible with i386:x86-64 output but the latter gives no such error, so I assume that the libssh.dll.a file is the correct library file to use.
Checked the advice given in this very similar question about libssh2 about the order of libraries linked, but here the call to g++ looks right as the libssh.dll.a argument is right at the end.
Checked the more general advice about linker errors too but could not find anything that seemed to be applicable.
Cleaned before rebuilding to avoid effects from "dead wood".
Tried other older versions of the library. Same error message.
Tried (unsuccessfully) to compile a newer version of libssh.
The libssh is not linked. The correct way to do this is to copy the libssh directory (which contains the headers) under the include directory of you Code Blocks installation. and copy the .dll.a file under the lib directory of your CodeBlocks installation.
Also delete the #define LIBSSH_STATIC 1 line if you are going to do it this way.

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

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.

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.