Rectangle 27 77

The functions you define are defined in an onload function, so whereas before they were referenceable, because they are defined in that function they can only be referenced from within that function. You reference them as globals in your HTML. You have three options

a) ( easiest, quickest, not ideal ) - change function blah(){} to window.blah = function(){}; making the functions global.

b) ( ideal way ) - use unobtrusive Javascript to attach behaviour to DOM elements from within the JS solely, meaning separate HTML from JS.

c) Make the jsfiddle not wrap the stuff onload. Change onLoad to no wrap ( body or head ).

<p onclick="lol()" id="foo">
var e = document.getElementById('foo'); e.onclick = lol;

thank you. how is it then that the code works on a live site?

Because the functions are not defined within another function on the live site. The jsfiddle throws your JS inside a function. View source on fiddle.jshell.net/mjmitche/afPrc/show

thank you very much for your help

Thanks. It's pretty absurd that this workaround is required, though.

html - JavaScript not running on jsfiddle.net - Stack Overflow

javascript html jsfiddle
Rectangle 27 2147

I think about the best I can do is give you a bunch of examples to study. Javascript programmers are practically ranked by how well they understand scope. It can at times be quite counter-intuitive.

  • A globally-scoped variable // global scope var a = 1; function one() { alert(a); // alerts '1' }
  • Local scope // global scope var a = 1; function two(a) { // local scope alert(a); // alerts the given argument, not the global value of '1' } // local scope again function three() { var a = 3; alert(a); // alerts '3' }
  • Intermediate: No such thing as block scope in JavaScript (ES5; ES6 introduces let) a. var a = 1; function four() { if (true) { var a = 4; } alert(a); // alerts '4', not the global value of '1' } b. var a = 1; function one() { if (true) { let a = 4; } alert(a); // alerts '1' because the 'let' keyword uses block scoping }
  • Intermediate: Object properties var a = 1; function Five() { this.a = 5; } alert(new Five().a); // alerts '5'
  • Advanced: Closure var a = 1; var six = (function() { var a = 6; return function() { // JavaScript "closure" means I have access to 'a' in here, // because it is defined in the function in which I was defined. alert(a); // alerts '6' }; })();
  • Advanced: Prototype-based scope resolution var a = 1; function seven() { this.a = 7; } // [object].prototype.property loses to // [object].property in the lookup chain. For example... // Won't get reached, because 'a' is set in the constructor above. seven.prototype.a = -1; // Will get reached, even though 'b' is NOT set in the constructor. seven.prototype.b = 8; alert(new seven().a); // alerts '7' alert(new seven().b); // alerts '8'

This will print out undefined and 10 rather than 5 and 10 since JavaScript always moves variable declarations (not initializations) to the top of the scope, making the code equivalent to:

var x = 5;

(function () {
    var x;
    console.log(x);
    x = 10;
    console.log(x); 
})();
var e = 5;
console.log(e);
try {
    throw 6;
} catch (e) {
    console.log(e);
}
console.log(e);

This will print out 5, 6, 5. Inside the catch clause e shadows global and local variables. But this special scope is only for the caught variable. If you write var f; inside the catch clause, then it's exactly the same as if you had defined it before or after the try-catch block.

Not even close to being comprehensive, but this is maybe the must-know set of Javascript scope tricks one needs to effectively even READ modern javascript.

A highly rated answer, not sure why. It's just a bunch of examples without proper explanation, then seems to confuse prototype inheritance (i.e. property resolution) with the scope chain (i.e. variable resolution). A comprehensive (and accurate) explanation of scope and property resolution is in the comp.lang.javascript FAQ notes.

@RobG It is highly rated because it is useful and comprehensible to a wide range of programmers, minor catachresis notwithstanding. The link you have posted, while useful to some professionals, is incomprehensible to most people writing Javascript today. Feel free to fix any nomenclature issues by editing the answer.

@triptychI only edit answers to fix minor things, not major. Changing "scope" to "property" will fix the error, but not the issue of mixing inheritance and scope without a very clear distinction.

If you define a variable in the outer scope, and then have an if statement define a variable inside the function with the same name, even if that if branch isn't reached it is redefined. An example - jsfiddle.net/3CxVm

What is the scope of variables in JavaScript? - Stack Overflow

javascript variables scope
Rectangle 27 2114

I think about the best I can do is give you a bunch of examples to study. Javascript programmers are practically ranked by how well they understand scope. It can at times be quite counter-intuitive.

  • A globally-scoped variable // global scope var a = 1; function one() { alert(a); // alerts '1' }
  • Local scope // global scope var a = 1; function two(a) { // local scope alert(a); // alerts the given argument, not the global value of '1' } // local scope again function three() { var a = 3; alert(a); // alerts '3' }
  • Intermediate: No such thing as block scope in JavaScript (ES5; ES6 introduces let) a. var a = 1; function four() { if (true) { var a = 4; } alert(a); // alerts '4', not the global value of '1' } b. var a = 1; function one() { if (true) { let a = 4; } alert(a); // alerts '1' because the 'let' keyword uses block scoping }
  • Intermediate: Object properties var a = 1; function Five() { this.a = 5; } alert(new Five().a); // alerts '5'
  • Advanced: Closure var a = 1; var six = (function() { var a = 6; return function() { // JavaScript "closure" means I have access to 'a' in here, // because it is defined in the function in which I was defined. alert(a); // alerts '6' }; })();
  • Advanced: Prototype-based scope resolution var a = 1; function seven() { this.a = 7; } // [object].prototype.property loses to // [object].property in the lookup chain. For example... // Won't get reached, because 'a' is set in the constructor above. seven.prototype.a = -1; // Will get reached, even though 'b' is NOT set in the constructor. seven.prototype.b = 8; alert(new seven().a); // alerts '7' alert(new seven().b); // alerts '8'

This will print out undefined and 10 rather than 5 and 10 since JavaScript always moves variable declarations (not initializations) to the top of the scope, making the code equivalent to:

var x = 5;

(function () {
    var x;
    console.log(x);
    x = 10;
    console.log(x); 
})();
var e = 5;
console.log(e);
try {
    throw 6;
} catch (e) {
    console.log(e);
}
console.log(e);

This will print out 5, 6, 5. Inside the catch clause e shadows global and local variables. But this special scope is only for the caught variable. If you write var f; inside the catch clause, then it's exactly the same as if you had defined it before or after the try-catch block.

Not even close to being comprehensive, but this is maybe the must-know set of Javascript scope tricks one needs to effectively even READ modern javascript.

A highly rated answer, not sure why. It's just a bunch of examples without proper explanation, then seems to confuse prototype inheritance (i.e. property resolution) with the scope chain (i.e. variable resolution). A comprehensive (and accurate) explanation of scope and property resolution is in the comp.lang.javascript FAQ notes.

@RobG It is highly rated because it is useful and comprehensible to a wide range of programmers, minor catachresis notwithstanding. The link you have posted, while useful to some professionals, is incomprehensible to most people writing Javascript today. Feel free to fix any nomenclature issues by editing the answer.

@triptychI only edit answers to fix minor things, not major. Changing "scope" to "property" will fix the error, but not the issue of mixing inheritance and scope without a very clear distinction.

If you define a variable in the outer scope, and then have an if statement define a variable inside the function with the same name, even if that if branch isn't reached it is redefined. An example - jsfiddle.net/3CxVm

What is the scope of variables in JavaScript? - Stack Overflow

javascript variables scope
Rectangle 27 2114

I think about the best I can do is give you a bunch of examples to study. Javascript programmers are practically ranked by how well they understand scope. It can at times be quite counter-intuitive.

  • A globally-scoped variable // global scope var a = 1; function one() { alert(a); // alerts '1' }
  • A globally-scoped variable var a = 1; // global scope function one() { alert(a); }
  • Local scope var a = 1; function two(a) { alert(a); } // local scope again function three() { var a = 3; alert(a); }
  • Local scope // global scope var a = 1; function two(a) { // local scope alert(a); // alerts the given argument, not the global value of '1' } // local scope again function three() { var a = 3; alert(a); // alerts '3' }
  • Intermediate: No such thing as block scope in JavaScript (ES5; ES6 introduces let) a. var a = 1; function four() { if (true) { var a = 4; } alert(a); // alerts '4', not the global value of '1' } b. var a = 1; function one() { if (true) { let a = 4; } alert(a); // alerts '1' because the 'let' keyword uses block scoping }
let
var a = 1;

function four() {
  if (true) {
    var a = 4;
  }

  alert(a); // alerts '4', not the global value of '1'
}
  • Intermediate: Object properties var a = 1; function Five() { this.a = 5; } alert(new Five().a); // alerts '5'
var a = 1;

function five() {
  this.a = 5;
}
  • Advanced: Closure var a = 1; var six = (function() { var a = 6; return function() { // JavaScript "closure" means I have access to 'a' in here, // because it is defined in the function in which I was defined. alert(a); // alerts '6' }; })();
  • Advanced: Prototype-based scope resolution var a = 1; function seven() { this.a = 7; } // [object].prototype.property loses to // [object].property in the lookup chain. For example... // Won't get reached, because 'a' is set in the constructor above. seven.prototype.a = -1; // Will get reached, even though 'b' is NOT set in the constructor. seven.prototype.b = 8; alert(new seven().a); // alerts '7' alert(new seven().b); // alerts '8'
// These will print 1-8
one();
two(2);
three();
four();
alert(new five().a);
six();
alert(new seven().a);
alert(new seven().b);

This will print out undefined and 10 rather than 5 and 10 since JavaScript always moves variable declarations (not initializations) to the top of the scope, making the code equivalent to:

var x = 5;

(function () {
    var x;
    console.log(x);
    x = 10;
    console.log(x); 
})();
var e = 5;
console.log(e);
try {
    throw 6;
} catch (e) {
    console.log(e);
}
console.log(e);

This will print out 5, 6, 5. Inside the catch clause e shadows global and local variables. But this special scope is only for the caught variable. If you write var f; inside the catch clause, then it's exactly the same as if you had defined it before or after the try-catch block.

Not even close to being comprehensive, but this is maybe the must-know set of Javascript scope tricks one needs to effectively even READ modern javascript.

A highly rated answer, not sure why. It's just a bunch of examples without proper explanation, then seems to confuse prototype inheritance (i.e. property resolution) with the scope chain (i.e. variable resolution). A comprehensive (and accurate) explanation of scope and property resolution is in the comp.lang.javascript FAQ notes.

@RobG It is highly rated because it is useful and comprehensible to a wide range of programmers, minor catachresis notwithstanding. The link you have posted, while useful to some professionals, is incomprehensible to most people writing Javascript today. Feel free to fix any nomenclature issues by editing the answer.

@triptychI only edit answers to fix minor things, not major. Changing "scope" to "property" will fix the error, but not the issue of mixing inheritance and scope without a very clear distinction.

If you define a variable in the outer scope, and then have an if statement define a variable inside the function with the same name, even if that if branch isn't reached it is redefined. An example - jsfiddle.net/3CxVm

What is the scope of variables in JavaScript? - Stack Overflow

javascript variables scope
Rectangle 27 2110

I think about the best I can do is give you a bunch of examples to study. Javascript programmers are practically ranked by how well they understand scope. It can at times be quite counter-intuitive.

  • A globally-scoped variable // global scope var a = 1; function one() { alert(a); // alerts '1' }
  • Local scope // global scope var a = 1; function two(a) { // local scope alert(a); // alerts the given argument, not the global value of '1' } // local scope again function three() { var a = 3; alert(a); // alerts '3' }
  • Intermediate: No such thing as block scope in JavaScript (ES5; ES6 introduces let) a. var a = 1; function four() { if (true) { var a = 4; } alert(a); // alerts '4', not the global value of '1' } b. var a = 1; function one() { if (true) { let a = 4; } alert(a); // alerts '1' because the 'let' keyword uses block scoping }
  • Intermediate: Object properties var a = 1; function Five() { this.a = 5; } alert(new Five().a); // alerts '5'
  • Advanced: Closure var a = 1; var six = (function() { var a = 6; return function() { // JavaScript "closure" means I have access to 'a' in here, // because it is defined in the function in which I was defined. alert(a); // alerts '6' }; })();
  • Advanced: Prototype-based scope resolution var a = 1; function seven() { this.a = 7; } // [object].prototype.property loses to // [object].property in the lookup chain. For example... // Won't get reached, because 'a' is set in the constructor above. seven.prototype.a = -1; // Will get reached, even though 'b' is NOT set in the constructor. seven.prototype.b = 8; alert(new seven().a); // alerts '7' alert(new seven().b); // alerts '8'

This will print out undefined and 10 rather than 5 and 10 since JavaScript always moves variable declarations (not initializations) to the top of the scope, making the code equivalent to:

var x = 5;

(function () {
    var x;
    console.log(x);
    x = 10;
    console.log(x); 
})();
var e = 5;
console.log(e);
try {
    throw 6;
} catch (e) {
    console.log(e);
}
console.log(e);

This will print out 5, 6, 5. Inside the catch clause e shadows global and local variables. But this special scope is only for the caught variable. If you write var f; inside the catch clause, then it's exactly the same as if you had defined it before or after the try-catch block.

Not even close to being comprehensive, but this is maybe the must-know set of Javascript scope tricks one needs to effectively even READ modern javascript.

A highly rated answer, not sure why. It's just a bunch of examples without proper explanation, then seems to confuse prototype inheritance (i.e. property resolution) with the scope chain (i.e. variable resolution). A comprehensive (and accurate) explanation of scope and property resolution is in the comp.lang.javascript FAQ notes.

@RobG It is highly rated because it is useful and comprehensible to a wide range of programmers, minor catachresis notwithstanding. The link you have posted, while useful to some professionals, is incomprehensible to most people writing Javascript today. Feel free to fix any nomenclature issues by editing the answer.

@triptychI only edit answers to fix minor things, not major. Changing "scope" to "property" will fix the error, but not the issue of mixing inheritance and scope without a very clear distinction.

If you define a variable in the outer scope, and then have an if statement define a variable inside the function with the same name, even if that if branch isn't reached it is redefined. An example - jsfiddle.net/3CxVm

What is the scope of variables in JavaScript? - Stack Overflow

javascript variables scope
Rectangle 27 52

Since they say "timing", I guess it's because they want their init functions to be able to call virtual functions on the object. This doesn't always work in a constructor, because in the constructor of the base class, the derived class part of the object "doesn't exist yet", and in particular you can't access virtual functions defined in the derived class. Instead, the base class version of the function is called, if defined. If it's not defined, (implying that the function is pure virtual), you get undefined behavior.

The other common reason for init functions is a desire to avoid exceptions, but that's a pretty old-school programming style (and whether it's a good idea is a whole argument of its own). It has nothing to do with things that can't work in a constructor, rather to do with the fact that constructors can't return an error value if something fails. So to the extent that your colleagues have given you the real reasons, I suspect this isn't it.

The first one is indeed a valid reason. It's also known a two-phase-construction. If I need something like this, I hide it in the innards of an object. I would never expose this to the users of my class.

As for the 2nd reason, the constructor can still be used with helper functions to manage the resources where there can be an exception (if exceptions are enabled). If the constructor fails, then the destructor will not be called on the partially constructed object. This is why sometimes you see manual init/cleanup code.

@brita_: yes, although arguably that's a problem with their code that they should fix by throwing more RAII at it. If the constructor fails then the destructors of the class's non-static data members are all called (just those which have already been initialized, in the case the failure occurs during initialization of one of them). Therefore, if you use instance data and/or variables within the constructor to capture anything that needs cleaning, then you wouldn't need any manual cleanup for an exception. Sometimes easier said than done, though.

c++ - Why use an initialization method instead of a constructor? - Sta...

c++ constructor initialization
Rectangle 27 9

Closures have to do with how javascript is scoped. To say it another way, because of the scoping choices (i.e. lexical scoping) the javascript designers made, closures are possible.

The advantage of closures in javascript is that it allows you to bind a variable to an execution context.

var closedIn = {};

var f = function(){
   closedIn.blah = 'blah'; // closedIn was just "closed in" because I used in the function, but it was defined outside the function.
}

in that example, you have a normal object literal called closedIn. It is accessed in a function. Because of that, javascript knows it has to bring closedIn everywhere it brings the function f, so it is available to f.

The this keyword is tricky. this is always a reference to the execution scope. You can capture the this of one context to use in another context as follows:

var that = this;
var f = function(){
    that.somethingOnThat();
   // `this` means the scope f, `that` means whatever 'this' was when defined outside of the function
}

This trick can be very useful somethings, if you are coding object oriented javascript and want a callback to have access to some external scope.

To quote from a Javascript book:

"Functions in JavaScript are lexically rather than dynamically scoped. This means that they run in the scope in which they are defined, not the scopee from which they are executed. When a function is defined, the current scope chain is saved and becomes part of the internal state of the function."

So the clear advantage is that you can bring any object (functions, objects, etc) along with the scope chain as far as is necessary. This is can also be considered a risk, because your apps can easily consume lots of memory if you are not careful.

Regarding the quote from the book, it should be noted that the scope chain is capture when the function is defined, with the exception of the value of 'this'. 'this' is very dependent on how the function is executed.

As someone who is doing some heavy javascript single page application. I need to use closures to save "this" context ALL THE TIME. If you are using jquery for example when you bind a function to an event the "this" keyword is bound to the HTML element that triggered the event, changing the context inside.

javascript closure advantages? - Stack Overflow

javascript closures
Rectangle 27 2

Looks like you're passing a function f2 into neville.m. Try using Q=neville(x,xi,@f2); Also, if xi is a vector, you should use element wise multiplication (and raising it to the nth power) using a dot (.) before the operation, else it will give an error. i.e., 1/(1+xxx.^2).

So, the idea is that in neville_driver.m I want to use f2 as fi, but in neville.m I want to have a function at argument. How exactly do I do this?

fi

fi is a function parameter. In this particular situation, I am passing f2 in the place of fi.

I also get this error message:Attempt to execute SCRIPT neville as a function. What is going on???

it means that you have not defined neville as a function. at the moment, it's just a plain matlab script, which you're probably trying to execute as a function either by passing variables or by using a function handle. this will result in an error. you should rewrite neville as a function.

Matlab - input argument is not defined - Stack Overflow

matlab
Rectangle 27 2

You have to pass the data returned in the success handler:

success:function(data){
    displayProfile(data);
},

That is because you are referring to the data directly in the success handler and data is defined in the function's params.

While in your first case you didn't passed the data returned from the ajax. So, results in undefined and Does not work.

And as above in the comments Rory mentioned to use the function as a reference instead, then it will be able to resolve the data as it is defined in the function's param:

success: displayProfile,

javascript - Why do ajax returns a TypeError undefined error if it cal...

javascript jquery json ajax
Rectangle 27 1

Sometimes it's handy to have a few local helper functions in an unnamed namespace (aka anonymous namespace) within the CPP file. If so, I'd recommend having those functions on top (within the CPP file), just to be sure that they are defined before any other function that would call them.

c++ - order of functions in cpp file - Stack Overflow

c++ function standards
Rectangle 27 9

You're being led astray by a wrong assumption of where the word "closure" comes from.

In a language-theoretic context, the point of a closure is that the function can refer to variables declared outside its own definition. It is immaterial whether it has internal variables, or that the internal variables are not visible from outside. In other words it is about seeing out from the function to its definition environment, not about seeing in from outside the function.

Why the weird word, then? Look at the function in your last example:

bar.baz = function() { return a }

This function contains a mention of the variable a which is not defined in the function body itself. It is a "free" variable of the function body, sort of a "hole" in the definition. We cannot execute the function without knowing, by some extraneous means, what variable the identifier a in the body refers to. Forming a closure at run-time pairs this "open" function body with a reference to the appropriate variable, thereby closing the hole in the definition. And that's where the name comes from.

(If you want the completely technical explanation, the underlying concept is that of a "closed" term in the lambda-calculus, which means one that has no free variables. Only closed term have independent meanings. A closure is then the combination of a (usually compiled) non-closed piece of source code, together with the contextual information that lets it behave like it was a closed term, and therefore be executable).

Addendum: In the common idiom

function() {
   var blah;
   // some code here
}();

the point is not to get a closure (you will get one, of course, but it doesn't do anything interesting for you), but to create a local scope for the blah variable. A local scope is conceptually quite a different thing from a closure -- in fact most C-lookalikes other than Javascript will create them at every {} block, whereas they may or may not have closures at all.

So ... which of my four examples would you qualify as "examples of closures"?

Every function expression creates a closure when it is evalutated. Sometimes (such as in your first three examples) the "environment" part of the closure is trivial or unused, but from a formal point of view, every user-defined Javascript function becomes a closure before it can be called.

Awesome; that was my understanding too. However, I'm very curious if the voters of SO agree ...

Also, do you have any reference you can point to for where you get your definition of "closure"? Just since there seems to be so many floating around ...

No particular reference, sorry. I learned this somewhen during 2+3 years of postgraduate study in programming language theory, and don't remember any particular source. For the record, both of the definitions you quote in the question are completely correct and not in conflict with each other, not am I saying anything different from them. I'm merely explaining to you how you're supposed to understand those descriptions.

javascript - Does a (JS) Closure Require a Function Inside a Function ...

javascript closures
Rectangle 27 6

From the "user" point of view, a class method in Python is a method that receives its class as its first parameter - unlike "ordinary" methods which receive an instance of the class as its first parameter - which by convention is called self.

If you retrieve an "ordinary" method from a class, instead of from an instace of that class, you get an "unbound method" - i.e. an object that is a wrapper around a function, but that does not automatically adds either the class itself, nor any instance as the first parameter when it is called. Threfore if you are to call the "unbound method" you have to manually pass an instance of its class as its first parameter.

If you manually call a class method, on the other hand, the class is filled in as the first parameter for you:

>>> class A(object):
...   def b(self):
...      pass
...   @classmethod
...   def c(cls):
...      pass
... 
>>> A.b
<unbound method A.b>
>>> A.c
<bound method type.c of <class '__main__.A'>>
>>> A.c()
>>> A.b()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method b() must be called with A instance as first argument (got nothing instead)
>>>

Under the hood what goes is more or less like this - with "new style classes":

When one defines a class body, the methods are just ordinary functions - when the class body is over, Python calls the class's metaclass (which ordinarily is the builtin type ) - and pass to it as parameters the name, base classes, and class body dictionary. This call yields a class - which in Python is, an object which is a class, since everything is an object.

Now, Python has some nifty ways of customizing attribute access - the so called "descriptors". A descriptor is any object that defines a method named __get__ (or __set__ or __del__ but we don't care about those here). When one access an attribute of a class or object in Python, the object referred by that attribute is returned - except if it is a class attribute, and the object is a descriptor. In that case, instead of returning the object itself, Python calls the __get__ method on that object, and returns its results instead. For example, the property built-in is just a class that implements both __set__, __get__ and __del__ as appropriate.

Now, what happens when the attribute is retrieved, is that any function (or class method or unbound method, as the data model states) on its body, does have a __get__ method, which makes it a descriptor. Basically, a descriptor that at each attribute access to retrieve the object named as the function as it is defined on the function body, creates a new object around that function - an object that when called will have the first parameter automatically filled in - which is to say, a method.

>>> class B(object):
...    def c(self):
...      pass
...    print c
... 
<function c at 0x1927398>
>>> print B.c
<unbound method B.c>
>>> b = B()
>>> b.c
<bound method B.c of <__main__.B object at 0x1930a10>

If you want to retrieve the function object, without conversion to a method object, you can do so through the class's __dict__ attribute, which does not trigger the descriptor:

>>> B.__dict__["c"]
<function c at 0x1927398>
>>> B.__dict__["c"].__get__
<method-wrapper '__get__' of function object at 0x1927398>
>>> B.__dict__["c"].__get__(b, B)
<bound method B.c of <__main__.B object at 0x1930a10>>
>>> B.__dict__["c"].__get__(None, B)
<unbound method B.c>

As for "class methods", these are just different type of objects, which are explicitly decorated with the builtin classmethod - The object it returns when its __get__ is called is a wrapper around the original function that will fill in the cls as the first parameter on call.

so a bound/unbound user-defined method is "Method of an instance", while a classmethod is "Method of an class, right?

In other words, if you try call unbound method directly from class you are get exception, and for call this method you must create object at first, and then call method "from" object. If you want call method directly from class you can use decorator @staticmethod

@Denis Well, when getting the attribute which is a method decorated with @classmethod, can we say that it is a bound method to the class?

Yes - it is then a classmethod, bound to the class.

oop - Python Data Model Document : an unbound user-defined method obje...

python oop object methods python-datamodel
Rectangle 27 55

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - What is an undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external
Rectangle 27 54

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - How to fix undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external
Rectangle 27 54

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - How to fix undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external
Rectangle 27 54

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - How to fix undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external
Rectangle 27 54

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - How to fix undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external
Rectangle 27 54

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - How to fix undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external
Rectangle 27 54

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - How to fix undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external
Rectangle 27 54

Symbols were defined in a C program and used in C++ code.

The function (or variable) void foo() was defined in a C program and you attempt to use it in a C++ program:

void foo();
int main()
{
    foo();
}
extern "C" void foo();
int main()
{
    foo();
}

Equivalently, instead of being defined in a C program, the function (or variable) void foo() was defined in C++ but with C linkage:

extern "C" void foo();

and you attempt to use it in a C++ program with C++ linkage.

If an entire library is included in a header file (and was compiled as C code); the include will need to be as follows;

extern "C" {
    #include "cheader.h"
}

Or conversely, If you develop a C library, a nice rule is to protect the header file(s) by surrounding all exported declarations with #ifdef __cplusplus [\n] extern"C" { [\n] #endif and #ifdef __cplusplus [\n] } [\n] #endif ([\n] being real carriage return but I can't write this properly in comment).

c++ - How to fix undefined reference/unresolved external symbol error ...

c++ linker-errors undefined-reference c++-faq unresolved-external