0

I'm currently trying to write an app for Raspberry Pi 3B under Rasbpian with aid of Linux Development plugin in Visual Studio 2017 Community. I managed to successfully deploy 'Blink' example, nobly attached by Microsoft folks, according to tutorial, and that went well. I even made some transmission over SPI thanks to wiringPi library. Then I would like to add some GUI to my app, so that one could, for example, make some transmission on click of a button on screen.

IntelliSense hinted me, that, in fact, there is gtk-3.0 library present in toolset. It seems that libraries are being copied from target device on every connection or so and I installed gtk on my Raspberry. So I added a simple line to this Blink example:

#include <gtk-3.0/gtk/gtk.h>

On compilation attempt, of course there was nearly 4k errors. Well, enough said, with a little hint from this old tutorial and a bit of trial and error, I managed to add this set of links under Debugging/Project properties/Configuration properties/VC++ directories/Header files directories:

enter image description here

Everything goes in promising direction, as errors number diminished from 4k to just one:

gtk-3.0\gtk\gtk.h: No such file or directory

No matter that this file is ACTUALLY in this location:

enter image description here

Regardless of combination of links in configuration above and using statement composition, compiler (?) can't find this damn file.

Please Halp

EDIT

I just confirmed, that it is indeed problem with target configuration. This is bad or good, depending on point of view. Good, because there is probably all good with VS setup. Bad, because I don't know a thing about compiling things under Linux.

On target (Raspberry Pi 3B) all ingredients for compilation are copied by Linux Development plugin. So in Terminal I executed line:

g++ main.cpp -o Blink2onRPi

and got

main.cpp:4:21: fatal error: gtk/gtk.h: no such file or directory

Now, I altered include line in main.cpp on target RPi, to this:

#include <gtk-3.0/gtk/gtk.h>

And now its missing <gdk/gdk.h>! When this change is made on host windows device - same result, but in VS.

As I dealt with similar problem in VS, upon setting links for IntelliSense (now apparently they're for this purpose), now probably similar dependencies have to be set somewhere on Raspbian. But where?

EDIT2

Upon execution of:

g++ main.cpp -o Blink2onRPi `pkg-config --cflags --libs gtk+-3.0`

on target RPi there is no more GTK-related errors, just wiringPi (also present in project) undefined references. It raises two possible questions:

1) How can I setup wiringPi on RPi so that the project could be manually compiled on target and

2) How/where add above line to Visual Studio, so it execute remotely with all GTK dependencies added properly on target

Researching stock present wiringPi library (as this is Blink led example for cross-compile Linux Development) I've found, that in Project Properties/Linker/Input/Library Dependencies there is mysterious entry:

wiringPi

Just that, nothing more. After removing this entry, on compilation pops out same errors as before on target (which apparently lacks proper wiringPi setup) - undefined references (not mensioned any missing headers). Can this be relevant for the case? If so, how could I add there such entry which would deal with missing GTK dependencies?

zaguoba
  • 33
  • 7
  • Please decide if you want to cross-compile or develop on the target. You can't do both. – liberforce Sep 19 '18 at 13:48
  • @liberforce as I commented below your answer, I did compilation on RPi just to determine that, in fact, its build configuration is invalid. [I have found here in comment of user Stefan and following](https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/) that they dealt with similar problem in past, but the descritpion is too enigmatic for me – zaguoba Sep 19 '18 at 14:17
  • Ok, I now understand a bit better the situation. That Linux Development plugin merely copies and builds **on the target**, doing a remote build, and not a cross-build using VC compiler. This make the whole thing overcomplicated, where pkg-config was made to just fix that problem and get the dependencies for you. – liberforce Sep 19 '18 at 14:24
  • That comment hints that you should be able to use pkg-config in the compiler and linker options, and don't have to mess with setting include dirs by hand. – liberforce Sep 19 '18 at 14:25
  • I updated my answer, now I understand a bit better what that damn plugin does. – liberforce Sep 19 '18 at 14:30
  • The old tutorial you followed doesn't apply, it's done to build on Windows for a Windows target, and you're really using Visual Studio just as an editor, as all the building is done on the Linux target, which is a completely different case. For configuration, use tutorials that assume you're building on Linux, and transpose the arguments to your VC configuration. – liberforce Sep 19 '18 at 14:39
  • @ liberforce I tried sticking randomly phrase `pkg-config –libs gtk+-3.0` (with backticks, which don't show here...) into few different locations both in compiler and linker settings, but without luck, still searching for some reliable source on that. As of this old tutorial, I just deduced some analogies, and I was well aware that this is not just old but also for windows only case. – zaguoba Sep 19 '18 at 14:46
  • You may replace backticks by `$()` like in `$(pkg-config –libs gtk+-3.0)` (or double the backticks on stackoverflow to escape them, like this: ``pkg-config –libs gtk+-3.0``). Randomly doing things won't help you undestand the issue. First, you forgot one hyphen before `libs`. Long options use double hyphens: `--libs`. Second, that command gives you the command line argument to give to the linker, so search your *linker* configuration in VS and replace all configuration by this simple command. Do the same with `$(pkg-config –cflags gtk+-3.0)` and the *compiler* configuration. – liberforce Sep 19 '18 at 15:01
  • @liberforce I'm a little confused whether first sentence of above comment is about posting inline code in stackoverflow comments, or is it actually about putting pkg-config statement into VS settings? Putting statement with dollar sign results in immediate evaluation-something error (cannot save configuration). [I did something like this](https://i.stack.imgur.com/MJzVf.jpg), also without backticks, in linker section - not working. Also, I have no idea where could I possibly put phrase intended for compiler. – zaguoba Sep 19 '18 at 15:45
  • The [`$()`](http://www.tldp.org/LDP/abs/html/abs-guide.html#CSPARENS) is a `bash` syntax that spawn a subprocess and gets its output. It's also supported by other shells. I saw in your MSDN link that the guys use the backticks, so thought that it could work too. Seems it doesn't in VS, so it's not equivalent in your case (but usually is). In that same post, someone succesfully [added the pkg-config command for the linker in the **Additional dependencies**](https://blogs.msdn.microsoft.com/vcblog/2016/03/30/visual-c-for-linux-development/#comment-159535), just below the one field tried. – liberforce Sep 19 '18 at 16:09
  • 1
    This answer, https://stackoverflow.com/questions/45570103/g-cant-link-curl-lib-files/45578921#45578921 , explains how to use `pkg-config` with VSLinux . VS will not add third-party libraries to the Linux target for you, you must set them up seperately, see https://stackoverflow.com/questions/43552526/visual-studio-2017-c-linux-app-headers-not-copied-to-target/43577361#43577361. – stanthomas Oct 01 '18 at 01:04

2 Answers2

1

TL;DR

Use screenshot below to know where to add pkg-config calls in VS configuration so that it forwards it to the compiler and linker on the target. Thanks to @zaguoba for providing these.

ORIGINAL ANSWER:

The list of directories to include is provided by pkg-config. For example pkg-config --cflags-only-I gtk+-3.0 will give you the list of include directories required. Those are the ones you need to add to the directories where VC++ wil look at include files. If you add the relative path you use in the #include, to one of those paths, you are able to find the file.

Example:

If you add to the directories C:\Program Files\foo\bar\gtk+-3.0

and have in your C file: #include <gtk/gtk.h>

then the compiler will look for C:\Program Files\foo\bar\gtk+-3.0\gtk\gtk.h.

EDIT:

This all means the 'file not found' errors are because you're really building on the target and the target has no idea what C:\Program Files\... means. Those should be paths on the target filesystem, where the compiler is called. And this is exactly what pkg-config provides.

The copy of those files on the Windows machine filesystems is merely for Intellisense use, not for compiler use.

EDIT 2:

So that's that Visual Studio 2017 Community Linux Development plugin is what need to be undestood. It's not for cross compilation from Windows to Linux, istead it merely synchronizes code to the Windows host (for Intellisense use), but builds on the target. This means that all the paths and commands are Linux paths and commands, run on the target.

Here's the OP working configuration:

Where to put pkg-config calls in Visual Studio compiler and linker configuration

liberforce
  • 11,189
  • 37
  • 48
  • I have all these three lines in my code exactly like in your example, but compiler (on host OR target) still can't find gtk.h, despite it's in that very location. I also ran pkg-config command you suggested on my RPi target ([here's result](https://pastebin.com/bSpCJb3Y)) and there is way more dependencies listed than I added to VC++ additional directories; some of them are listed twice, some of them weren't copied by VS to my windows host - should I copy them manually and add to list? Is order on dependencies list important? I really know nothing about Linux and pkg-config. – zaguoba Sep 19 '18 at 12:13
  • Only other thing that looks a bit odd is the entries you have in screenshot of the include directories should normally show up enclosed in quotes `"c:\Program Files\ ...." – nos Sep 19 '18 at 12:31
  • @nos but added lines worked without quotes, that is it removed IntelliSense errors. I confirmed, that this is in fact target Raspbian configuration problem, and edited original post, could you see it? – zaguoba Sep 19 '18 at 12:46
  • AFAIK, you're cross-compiling from Windows to your RPi target, and GTK+ development files are installed on your Windows machine, so I don't see the point in running g++ on the RPi. Either you develop on the RPi, or under Windows, but not on both. – liberforce Sep 19 '18 at 13:37
  • 1
    BTW, you should **always** use `#include `. This is to make it easier to migrate to another major version of GTK+ (`gtk+-3.0` dir would become `gtk+-4.0`). Every GTK+ program does this. Instead of changing all the existing code to fix all the includes, you just change the include dir in your build configuration. So don't change that include, it makes no sense. Fix you build configuration instead. – liberforce Sep 19 '18 at 13:39
  • 1
    Some [doc about pkg-config](https://people.freedesktop.org/~dbn/pkg-config-guide.html). – liberforce Sep 19 '18 at 13:42
  • @liberforce, I attempted compilation on RPi because I tried to determine which configuration, host or target, is incorrect, and it turned out it was target's (see edited OP). Thankfully, I did not come up with headers alteration idea, as there is quite a lot of them. I deduced already, that build configuration is invalid, but how could I get it working under Visual Studio for Linux Development? (BTW +1 for this `` clarification) – zaguoba Sep 19 '18 at 13:57
  • @liberforce I added `pkg-config --cflags --libs gtk+-3.0` (with backticks around) to Configuration Properties/C/C++/Command Line [**AND.IT.COMPILED**](https://vignette.wikia.nocookie.net/walkingdead/images/d/d2/Tumblr_inline_n5j75fhP2e1qbhw0j.gif)! Could you please add this to your post, so I can mark it an answer – zaguoba Sep 19 '18 at 16:14
  • 1
    It may compile, but I'm not sure it's the best alternative. You should pass the `pkgconfig` calls with `--cflags` to the compiler and `--libs` to the linker. *Command line* in that context could really mean anything, and I'm not sure it won't bite you later. – liberforce Sep 19 '18 at 16:19
  • Could you please post a screenshot of that Command Line screen? Thanks. I'm suprised the word "compiler" is absent from those settings, but maybe those MS guys are just hiding that. – liberforce Sep 19 '18 at 16:23
  • @liberforce [here's screenshot](https://i.stack.imgur.com/VxAbg.jpg). You were right, because it compiled with mere gtk/gtk.h included, with no other code. When I added [sample code from here](https://prognotes.net/2015/06/gtk-3-c-program-using-glade-3/), I had to split these commands as in screenshot to get compilation and of course, it's not end of problems. I can start compiled app on RPi and it shows blank window as it should, but remote debugger from Win host can't . Also, something's wrong with signal callback (?) and closing window doesn't end process. But it's for another question. – zaguoba Sep 20 '18 at 06:46
  • @zaguoba Oh right, so the `pkg-config` calls are used as additional options to the compiler and for the linker, so that looks like the right fix. I'm updating my answer with your screenshot that makes it clear what the solution is. Feel free to ask an new question about your application problems. – liberforce Sep 20 '18 at 08:20
0

With that setup, you should

#include <gtk\gtk.h>

instead of

#include <gtk-3.0\gtk\gtk.h>

Alternatively remove all those VC++ directories/Header files directories, and just keep one of them that ends with include/ instead of listing up all the sub directiores.

nos
  • 223,662
  • 58
  • 417
  • 506
  • Yeah, I was afraid that last sentence could be unclear. I have tried all of these: #include , , with different links in headers directories configuration. Also, solo include/ is apparently not good enough, as it was present before I added rest of directories and there was an avalanche of errors. – zaguoba Sep 18 '18 at 21:25
  • I assume that these directories are required due to sort of 'hard links' in headers, for example if in header there is #include line, this file has to be searched in relation to its dependencies, otherwise it could not be found, at least in my case. – zaguoba Sep 18 '18 at 21:34
  • Please use slashes and not backslashes in the includes: https://stackoverflow.com/a/5790259/518853 – liberforce Sep 19 '18 at 08:44
  • @liberforce ok as I understand forward slashes are better because of compatibility issues, but changing to them doesn't seem to make a difference. – zaguoba Sep 19 '18 at 12:19
  • @zaguoba: Yeah, my comment was for nos. It's just that it's bad practice, so shouldn't be advertised, but is not related to your problem. – liberforce Sep 19 '18 at 13:46