How do I use Gio library in Vala? - gtk

I'm building a Gtk application in Vala and would like to use the Gio library and more specifically the g_app_info_get_all() function. I am able to achieve this in C as follows:
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <gio/gdesktopappinfo.h>
int main(int argc, char *argv[])
{
...
GDesktopAppInfo *app_info;
GList *app_list, *l;
gtk_init(&argc, &argv);
...
app_list = g_app_info_get_all();
...
}
How do I achieve the same in Vala?
ChatGPT suggested me to use Gio.AppInfo.get_all()
and even though
pkg-config --modversion gio-2.0 returns 2.74.3 but using Gio; throws an error saying the namespace 'Gio' could not be found even when I'm compiling with --pkg gio-2.0 flag.

The namespace is GLib, this is shown as {} Glib in Valadoc. The GLib namespace is used by default, so the following will work:
void main () {
var result = AppInfo.get_all ();
foreach (var app in result) {
print (#"$(app.get_display_name())\n");
}
}
Compile with:
valac example.vala --pkg gio-2.0

Related

Undefined reference to `ulocdata_open_63'

I am using ICU lib for Exemplar, I am getting undefined ref for `ulocdata_open_63'
I am not using "ulocdata_open_63" function but "ulocdata_open". Library is linked properly.
Why it is looking for ulocdata_open_63 definition even though I am not calling it.
Sample code:-
#include <stdio.h>
#define UNICODE
#include <unicode/uloc.h>
#include <unicode/ulocdata.h>
#include <unicode/urename.h>
typedef unsigned short U16;
int main()
{
char localeID[ULOC_FULLNAME_CAPACITY+ULOC_KEYWORD_AND_VALUES_CAPACITY] = "en_US";
UErrorCode icuStatus = U_ZERO_ERROR;
ULocaleData* uld = ulocdata_open("en", &icuStatus);
...
}
It looks you're linking against a ICU library that was built with ICU version suffixes, which is the default. To build a library without version suffixes you'll have to add the flag --disable-renaming to the configure build step of ICU4C.
See https://unicode-org.github.io/icu/userguide/icu4c/build.html#icu-as-a-system-level-library.

Platformio Error does not name a type

I have very simply code:
main.cpp
#include <Arduino.h>
#include "config.h"
Config c;
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
config.h
#ifndef Config_h
#define Config_h
class Config{
public:
Config(){};
};
#endif
config.cpp
#include "config.h"
When I try to build project, I got the below error:
src\main.cpp:4:1: error: 'Config' does not name a type
What is wrong here?
I have found solution config.h => myconfig.h

How to access a variable in mumtiple cpp files contain main section in each file?

I want to access a variable in multiple .cpp files. I looked into several resources. I could not solve it though. I am using cmake to build all the codes in this project. Following is an example that exactly matches with my problem. Basically, I want val to print 42 in both code1.cpp and code2.cpp. When, I build these three files it complains: undefined reference to 'he::val' collect2: error:ld returned 1 exit status for both the .cpp files.
header1.h
#ifndef HEADER1_H
#define HEADER1_H
#include <iostream>
namespace he {
extern int val;
}
#endif // HEADER1_H
code1.cpp
#include "header1.h"
#include <iostream>
using namespace he;
int func()
{
std::cout << val << std::endl;
}
int main()
{
val=20;
func();
return 0;
}
code2.cpp
#include <iostream>
#include "header1.h"
using namespace he;
int main()
{
std::cout << val << std::endl;
}
extern int val;
Is a variable declaration. You need, somewhere (in one of your cpp files), to define the variable:
int val;
Actually, since you have two main() functions, these are two separate programs. Then your variable definition needs to be in both, like this:
namespace he {
int val;
}
But your extern declaration in the header file makes your variable global, which is usually frowned upon. It all depends, of course on what your purpose is.
Also, since two main() functions mean two separate programs, there is no variable sharing.

The rule of The Big Three

Iam confused with the below question I did the program as per my understanding but it crashes what am I doing wrong? If someone can please assist me it would be much appreciated.
my main.cpp looks like this:
#include <iostream>
#include <iomanip>
#include "Number.h"
using namespace std;
int main()
{
Number n1(10);
Number n2 = n1;
n2.printNum();
n2.addOne();
n1 = n2;
n1.printNum();
return 0;
}
Then my header file looks like this:
#include <iostream>
using namespace std;
class Number
{
int *p;
public:
Number(int);
void addOne();
void printNum();
};
And the below parts for the constructor I need to complete there where it shows comments that's the part I should complete:
#include <iostream>
#include "Number.h"
using namespace std;
Number::Number(int a1)
{
*p = a1;//write the code needed to initialise the value of the member variable with a1
}
void Number::printNum()
{
cout << "The number is " << *p << endl;
}
void Number::addOne()
{
*p++;//write the code needed to increment the value of the member variable by one.
}
Then the question asks the below what should I do to the code to use the BIG THREE?
Consider the following program. Complete the class definition (where you are asked to) and check the output. You can see that that program works without error once it is completed. However, experts suggest that in any class that uses pointers and the new operator it is better to follow the rule of The Big Three. Modify the class definition to follow the rule of The Big Three and submit the new program and the output. Demonstrate the use of this pointer.
Thank you
Rohan

Why VS2015 intellisense shows error on C++11 user defined literals (UDL)

The below code can be compiled and run, but VS2015 intellisense shows error. g++ & eclipse has the same issue (compiled & run but shows error)
Does anyone know how to fix it? I tried searching on google but hopeless.
The error is a little annoying.. :-)
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
using namespace std::literals;
using namespace chrono_literals;
int main()
{
this_thread::sleep_for(5s);
cout << "test \n";
return 0;
}
Error message: "Invalid suffix 's' on integer literal"
Thanks a lot!
You should add some #include statements and namespace references:
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
using namespace std::literals::chrono_literals;
std::this_thread::sleep_for(5s);
std::cout << "test \n";
return 0;
}
In your code, the compiler is not been told to use namespace std. The 5s does not work without std::literals