undefined reference error in VScode Version 1.69.1 - visual-studio-code

Below is simple program where i have configured VSCode on Azure VM with all applicable extensions to run c program
But when i try to compile i am getting below error
Any hints or missing extensions will assist... if i define func() at top prior main() it works but would be interested to know how do i make it work the way i am trying too ..any hints will be appreciated..

Place your entire void meow(void) function definition:
void meow(void) {
printf("meow\n");
}
outside of the main() function definition brackets { } like so:
int main(void) {
...
...
}
void meow(void) {
printf("meow\n");
}

Related

Flutter : A member named 'read' is defined in extensions 'ReadContext' and 'BuildContextX' and neither is more spesific

I have a button in my application that return :
onPressed: () {
return context
.read(FavoriteIds.provider.notifier)
.toggle(doa.id.toString());
},
In this case, i used a riverpod provider. But when i want to import a flutter_bloc package, the read keyword will be error with this message
A member named 'read' is defined in extensions 'ReadContext' and 'BuildContextX' and neither is more specific. Try using an extension override to specify the extension you want to to be chosen.
Please help me solve this problem. thank you :)
Here the problem is read() is defined in both ReadContext & BuildContextX extensions. So the compiler is not getting which extension to use.
To solve the error, use : ReadContext(context).read if you wanna access bloc or BuildContextX(context).read() as per your need.
This means you are importing 2 extensions that both supply the same method read. Consider this example:
extension Ext1 on String {
void foo() => print("from extension 1");
}
extension Ext2 on String {
void foo() => print("from extension 2");
}
void main() {
String s = "hello";
s.foo();
}
What should this code print? There isn't an obvious answer, and to avoid accidental programming errors, Dart prohibits this.
You could try "go-to definition" (ctrl/cmd click in most IDEs) on the read method to navigate to one of the files that it is defined in, and then delete the corresponding import statement.
However, it might be quicker to just delete all the import statements in that file and add them back with autocomplete

Flutter & Dart: How to check/know which class has called a function?

I am trying to know which class has called a specific function. I've been looking through the docs for this, but without success. I already know how to get the name of a class, but that is something different of what I'm looking for. I found already something related for java but for dart I haven't. Maybe I'm missing something.
Let's say for example that I have a print function like so:
class A {
void printSomethingAndTellWhereYouDidIt() {
// Here I would also include the class where this function is
// being called. For instance:
print('you called the function at: ...');
//This dot-dot-dot is where maybe should go what I'm looking for.
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
The output should be something like:
you called the function at: B
Please let me know if there are ways to achieve this. The idea behind is to then use this with a logger, for instance the logging package. Thank you in advance.
You can use StackTrace.current to obtain a stack trace at any time, which is the object that's printed when an exception occurs. This contains the line numbers of the chain of invocations leading up to the call, which should provide the information you need.
class A {
void printSomethingAndTellWhereYouDidIt() {
print(StackTrace.current);
}
}
class B {
A a = A();
void test() {
a.printSomethingAndTellWhereYouDidIt();
}
}
If you are doing this for debugging purposes, you can also set a breakpoint in printSomethingAndTellWhereYouDidIt to check where it was called from.

Does Dart main() function need to be void main() or can void type be omitted?

As a complete beginner learning Dart, I want to understand if the void type is required for a main function. In the official language tour: https://dart.dev/guides/language/language-tour#a-basic-dart-program the examples do not include the void keyword/type.
But in other places we seed void main() { ...
The following two snippets of code have the same output:
main() {
print('Hello World!');
}
Runs fine on Dart Pad: https://dartpad.dartlang.org/fa6f6e5a7b9406e88b31a17e82655ef8
(we don't see any compiler warnings or advice suggesting the void should be added)
void main() {
print('Hello World!');
}
Is the void a convention that nobody questions or can we exclude it without any consequences?
Note: I'm aware of the history of void keyword/type, I just want to understand if I can safely omit the void from more advanced programs or if it's required.
https://en.wikipedia.org/wiki/Void_type
https://medium.com/flutter-community/the-curious-case-of-void-in-dart-f0535705e529
https://medium.com/dartlang/dart-2-legacy-of-the-void-e7afb5f44df0
The Dart 2.2 language specification says:
18.4 Scripts
A script is a library whose exported namespace (18.2) includes a top-level
function declaration named main that has either zero, one or two required arguments.
The spec imposes requirements on the name and the arity (and types) of its arguments. There is no requirement on its return type, so using a different type (such as dynamic, which is what it would be if you omit void) would have no effect.
Declaring no type is identical to declaring void.
I'm not sure if Flutter/Dart want the void there for some sort of "identification" but I doubt it. If it runs, it should be 100% the same.
The Dart linter has the following rule:
always_declare_return_types (ref)
DO declare method return types.
When declaring a method or function always specify a return type. Declaring return types for functions helps improve your codebase by allowing the analyzer to more adequately check your code for errors that could occur during runtime.
BAD:
main() { }
_bar() => _Foo();
class _Foo {
_foo() => 42;
}
GOOD:
void main() { }
_Foo _bar() => _Foo();
class _Foo {
int _foo() => 42;
}

How to control argument passing policy in pybind11 wrapping of std::function?

I have a class in c++ that I'm wrapping into python with pybind11. That class has a std::function, and I'd like to control how the arguments to that function are dealt with (like return value policies). I just can't find the syntax or examples to do this...
class N {
public:
using CallbackType = std::function<void(const OtherClass*)>;
N(CallbackType callback): callback(callback) { }
CallbackType callback;
void doit() {
OtherClass * o = new OtherClass();
callback(o);
}
}
wrapped with
py::class_<OtherClass>(...standard stuff...);
py::class_<N>(m, "N")
.def(py::init<N::CallbackType>(),
py::arg("callback"));
I all works: I can do this in python:
def callback(o):
dosomethingwith(o)
k = N(callback)
, but I'd like to be able to control what happens when callback(o); is called - whether python then will take ownership of the wrapped o variable or not, basically.
I put a printout in the destructor of OtherClass, and as far as I can tell, it never gets called.
OK, I think I figured it out:
Instead of std::function, use a pybind11::function:
using CallbackType = pybind11::function
and then
void doit(const OtherClass &input) {
if (<I want to copy it>) {
callback(pybind11::cast(input, pybind11::return_value_policy::copy));
} else {
callback(pybind11::cast(input, pybind11::return_value_policy::reference));
}
}
I see nothing in pybind11/functional that allows you to change the ownership of the parameters at the point of call, as the struct func_wrapper used is function local, so can not be specialized. You could provide another wrapper yourself, but in the code you can't know whether the callback is a Python function or a bound C++ function (well, technically you can if that bound C++ function is bound by pybind11, but you can't know in general). If the function is C++, then changing Python ownership in the wrapper would be the wrong thing to do, as the temporary proxy may destroy the object even as its payload is stored by the C++ callback.
Do you have control over the implementation of class N? The reason is that by using std::shared_ptr all your ownership problems will automagically evaporate, regardless of whether the callback function is C++ or Python and whether it stores the argument or not. Would work like so, expanding on your example above:
#include <pybind11/pybind11.h>
#include <pybind11/functional.h>
namespace py = pybind11;
class OtherClass {};
class N {
public:
using CallbackType = std::function<void(const std::shared_ptr<OtherClass>&)>;
N(CallbackType callback): callback(callback) { }
CallbackType callback;
void doit() {
auto o = std::make_shared<OtherClass>();
callback(o);
}
};
PYBIND11_MODULE(example, m) {
py::class_<OtherClass, std::shared_ptr<OtherClass>>(m, "OtherClass");
py::class_<N>(m, "N")
.def(py::init<N::CallbackType>(), py::arg("callback"))
.def("doit", &N::doit);
}

Using a Function pointer inside a class crashes the compiler (but works inside a function)

Reference class
class commandsListClass
{
public:
std::string name;
std::string description;
std::vector<std::string> commands;
columnHeaders headersRequired;
void (*function)(System::Object ^ );
std::string recoveryFileHeader;
void reset()
{
name = "";
description = "";
commands.clear();
headersRequired.reset();
recoveryFileHeader = "";
function = dummyFunc; // dummyFunc uses the same members as the intended - this is to ensure it is defined. DummyFunc is empty, returns void etc
}
commandsListClass()
{
reset();
}
};
Currently, if I run the below code, the compiler crashes
// This crashes the compiler
System::Threading::ThreadPool::QueueUserWorkItem(gcnew System::Threading::WaitCallback(global::commandsList[index].function ), ti);
1>------ Build started: Project: MyProject, Configuration: Release x64 ------
1> project.cpp
1>c:\users\guy\documents\visual studio 2012\projects\MyProject\MyProject\Form1.h(807): fatal error C1001: An internal error has occurred in the compiler.
1> (compiler file 'msc1.cpp', line 1443)
1> To work around this problem, try simplifying or changing the program near the locations listed above.
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If I declare a member inside the same function as I am making the call, and set it to the global::commandsList[index].function, it compiles and runs correctly
// This runs correctly
void (*func)(System::Object ^);
func = global::commandsList[index].function;
System::Threading::ThreadPool::QueueUserWorkItem(gcnew System::Threading::WaitCallback(func ), ti);
global::commandsList is a vector of type commandsListClass
Any ideas? Browsing Google and SO suggest changing the compiler to not optimize, which I've tried with no success. The code is written in such a way that:
That point in the code cannot be reached if index does not point to a valid member of the global::commandsList vector
The function variable is guaranteed to be set, either to the dummyFunc on creation, or the correct (requested) function as set elsewhere in the code.
Any help would be greatly appreciated.
Edit 1: This is using Visual Studio 2012, Windows 7 x64
Here's a simplified repo:
public delegate void MyDel(Object^);
void g(Object^) {}
struct A {
static void(*fs)(Object^);
void(*f)(Object^);
gcroot<MyDel^> del;
};
void(*fg)(Object^);
void h()
{
void (*f)(Object^);
A a;
gcnew MyDel(f);
gcnew MyDel(fg);
gcnew MyDel(a.fs);
a.del = gcnew MyDel(g);
//gcnew MyDel(a.f); // this line fails
// work around
f = a.f;
gcnew MyDel(f);
}
Only the non-static member variable fails. Seems like a compiler bug. Work around it by using a local intermediate.
Or better is Lucas's suggestion to use gcroot.