1

I have been trying to set up my coding environment for GUI development in c++ recently, with little success. I use Manjaro Linux with Visual Studio Code, but for some reason, I always seem to get include errors when including files that I know are there.

Most recently, I tried to set up gtkmm-4.0 by installing the package and the documentation. I double checked in /usr/include/ to ensure the packages were all present, but I still am getting include errors: cannot open source file "gtkmm.h" and gtkmm.h:No such file or directory

At this point, all the code I have is:

#include <gtkmm.h>
#include <iostream>

int main(int argc, char* argv[]){

    return 0;
}

Makefile:

exec = game.out
sources = $(wildcard src/*.cpp)
objects = $(sources:.cpp=.o)
flags = -g $(shell pkg-config gtkmm-4.0 --cflags)
libs = $(shell pkg-config gtkmm-4.0 --libs)


$(exec): $(objects)
    g++ $(objects) $(flags) -o $(exec) $(libs)

%.o: %.cpp include/%.h
    g++ -c $(flags) $< -o $@

install:
    make
    cp ./game.out /usr/local/bin/game

clean:
    -rm *.out 
    -rm *.o
    -rm src/*.o

I have scoured the internet for answers, but everything I found was either for a different os/environment or just didn't

@Galik and @John helped me solve this!

What I had to do was use g++ src/main.cpp -o main $(pkg-config gtkmm-4.0 --cflags --libs) to compile my code, then run the executable.

Thank you both for your help and guidance!!

Christian
  • 41
  • 7
  • You probably need to learn about [GNU make](https://www.gnu.org/software/make/) and [pkg-config](https://en.wikipedia.org/wiki/Pkg-config) and read the documentation of your compiler, usually [GCC](https://gcc.gnu.org/). Be aware that Visual Studio Code is not a compiler (it runs one, usually `g++`) – Basile Starynkevitch Jun 19 '22 at 18:26
  • You need to add the header search path to the compile command and the library search path to the link command. How are you compiling this? – Galik Jun 19 '22 at 18:30
  • I have been (attempting to anyway) compiling my program with a Makefile, but I also tried just using `g++ main.cpp -o main` – Christian Jun 19 '22 at 18:32

1 Answers1

0

You need to install pkg-configand add this to the compiler flags in your Makefile:

flags = -g $(shell pkg-config gtkmm-2.4 --cflags)
libs = $(shell pkg-config gtkmm-2.4 --libs)

# ...

$(exec): $(objects)
    g++ $(objects) $(flags) -o $(exec) $(libs)

The tool pkg-config has a database of the correct paths for supporting libraries.

Depending on your version if gtkmm, you may need to substitute gtkmm-3.0, if you have version 3.0.

Galik
  • 47,303
  • 4
  • 80
  • 117
  • I made these changed to my Makefile, but I am still getting the include errors. I also attempted running the program by just typing `g++ src/main.cpp -o main 'pkg-config gtkmm-4.0 --cflags --libs'`, but still to no avail. I also checked my /usr/include/ directory and found that there was no `gtkmm.h` file there. I could copy it from the `gtkmm-4.0` directory into `/usr/include/` but I am loath to do so as I have seen other people do the same thing only to end up in an eternal loop of having to move more .h files into `/usr/include/` – Christian Jun 19 '22 at 18:49
  • @Christian what does `pkg-config gtkmm-4.0 --cflags` display if you just run it in the console? – john Jun 19 '22 at 18:55
  • @john https://pastebin.com/KT6PJMC2 – Christian Jun 19 '22 at 19:22
  • @Christian Well as you can see it's expecting the header files to be in `/usr/include/gtkmm-4.0` and `/usr/lib/gtkmm-4.0/include` etc. Is that where you have the header files? – john Jun 19 '22 at 19:37
  • @Christian Please also post the latest content of your makefile. There's probably just some simple error in that. – john Jun 19 '22 at 19:39
  • @Christian That command should not use single quote `'`, it should use a back-tick `. As in `g++ src/main.cpp -o main `pkg-config gtkmm-4.0 --cflags --libs`` – Galik Jun 19 '22 at 20:38
  • @Christian I generally use `g++ src/main.cpp -o main $(pkg-config gtkmm-4.0 --cflags --libs)` to be safer – Galik Jun 19 '22 at 20:41
  • 1
    @john I updated my post with the most recent Makefile. Also, I checked `/usr/include/gtkmm-4.0` and `/usr/lib/gtkmm-4.0/include` and some other files and found `gtkmm.h` there... @Galik I ran the command `g++ src/main.cpp -o main $(pkg-config gtkmm-4.0 --cflags --libs)` and the code compiled! There is still the red underline in VSC, but I can live with that. – Christian Jun 19 '22 at 21:00