PDA

View Full Version : c++ stuff/OpenGL



02-23-2003, 05:06 PM
working with making an opengl dll for win32, I find the functions will not properly export unless I put

extern "C"

in front of their declarations in the dll's .h file. why is this; what does extern "C" do to functions? is it a link thing?

also, is there really any difference between calling by reference and passing a pointer (other than a missing asterisk or ampersand in one case or the other) to functions? I call a function that draws my vertex arrays by passing a pointer to a structure; would I gain anything by using a reference or not using pointer arguments at all?

blood.angel
02-23-2003, 05:35 PM
extern tells the compiler to ignore what you are calling (if it hasnt been defined yet) as it will be defined later on (you hope).

So you probably arent defining your functions before you are calling them.

02-23-2003, 08:46 PM
yeah plain extern just tells the compiler to trust me that what I'm referring to exists, it will just be up to the linker to decide; but what does

extern "C"
{
}

do. i see it from time to time and have no idea what it's for.

SThomas
02-23-2003, 09:12 PM
extern "C" specifies that the function or variable should use C linkage rather than C++ linkage. it's usually used when mixing C and C++ to allow the use of C headers within a C++ program.

mikael_aronsson
02-24-2003, 04:57 AM
Using & (reference) and * (pointer) is the same thing, the generated machine code is usually identical, the reson for it's existence is that it had to be added to make the operator overloading to work.

Mikael

starman
02-24-2003, 09:15 AM
Prefer references to pointers where possible. If you use a pointer, make it a class member that gets allocated (new()'d) in the constructor and delete()'d in the destructor to avoid memory leaks.

Also, pass by const reference if you aren't modifying the structure in the function.

void myFunc(const StructType & s) { ... }

Lot's of good C++ info here:
http://www.parashift.com/c++-faq-lite/

147-2
02-26-2003, 12:30 PM
No no no... you got it almost wrong :P

Anyways, what extern C does is tells the compiler to look for the function in C terms. For example, in C
double sqrt(double) is called sqrt internally. in C++ the same thing ends up being __double_sqrt because of name beautification (helps not with OPERATOR overloading, but FUNCTION overloading).

Don't confuse this with functions inside classes, where the function has a refrence to the class prepended to the argument list, as well as all the name beautification.

As far as pass by refrence or pass by ptr, well, I usually pass by ptr (99% of the time) because Im used to working with pointers. Heh, mebbe its cause im an old C dog.

[This message has been edited by 147-2 (edited 02-26-2003).]

roffe
02-26-2003, 03:44 PM
Originally posted by knoxville:
I find the functions will not properly export unless I put extern "C"
in front of their declarations in the dll's .h file.

What kind of code is going to be calling this dll? C or C++ code?

This link will help you understand the problem:
http://msdn.microsoft.com/library/defaul...executables.asp (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore/html/_core_export_c.2b2b_.functions_for_use_in_c.2d.lan guage_executables.asp)