0

I am new to C++ development and I am trying to learn how to link libraries using g++ compiler. For example I am trying to link GLFW library into my project. I get this error:

main.cpp:1:24: fatal error: GLFW/glfw3.h: No such file or directory
#include <GLFW/glfw3.h>
                        
compilation terminated.

My project tree looks like this:

├── GLFW
│   └── glfw3.h
├── lib
│   ├── glfw3.dll
│   └── glfw3.lib
└── main.cpp

I want to be able to build my projects from terminal without using Visual Studio or an IDE in general. Furthermore, are both static and shared libraries necessary?. I am using Windows at the moment but Linux answers are acceptable too.

Gver
  • 1
  • IIRC you can see in VS what command it uses to build your app – Carlos Mar 01 '21 at 18:03
  • 2
    The libs are probably not a static version of the lib plus a dynamic one, but a dll + the corresponding import library; the import library is statically linked to your lib and is responsible for "making the connection to the dll". Also the issue you mention has nothing to do with the library not being found. You're not at the linking stage yet. This is a compilation error that's caused by the compiler not finding the include with the given path. – fabian Mar 01 '21 at 18:03
  • Looks like you're missing a `-I` on the command line. The error message says you haven't gotten to linking yet. You'll also need a `-l` and a `-L`. – user4581301 Mar 01 '21 at 18:06
  • It is clearly not a linking problem, but a preprocessor problem. Seeing your project tree try to add `-I` (likely `-I.`) which will ask g++ to search includes path from. And `-L./lib` to add `lib` folder as library source. Then add `-lglfw3` to the linker. (see [Vikas explanation](https://stackoverflow.com/questions/12654013/how-to-make-g-search-for-header-files-in-a-specific-directory) ) – frachop Mar 01 '21 at 22:04
  • What is your g++ command? – stegzzz Mar 02 '21 at 07:02
  • I have managed to make it work by moving the glfw3.dll file into the directory where the main.cpp file is. I can get it to compile with the following command ```g++ -o main main.cpp -I. -L. -lglfw3 -lopengl32```. But when the glfw3.dll is in the lib folder, it seems to compile with this command ```g++ -o main main.cpp -I. -L./lib -lglfw3 -lopengl32``` but when I try to run the executable I get an error saying that the glfw3.dll is not found. Why is that? – Gver Mar 03 '21 at 08:16

0 Answers0