Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ and mouthwash

Name: Anonymous 2010-04-27 11:35

Having a problem figuring out an error when I compile an object.

I have a .c file that I'm not supposed to modify, but need to use a function from inside a .h+.cpp object file.  The error has to deal with scope as the compiler (g++) complains.

I understand that normally I should use objectname::function, but since the function I need to use is contained outside the object, I'm not sure what the scope is.

Also, I've been trying to compile it like this:
g++ hurr.c derp.h derp.cpp -o derp.o

Perhaps this isn't correct but I'm not sure of another way while retaining the .c file and not simply copying its contents.

Name: Anonymous 2010-04-27 15:36

If you don't have a header file for hurr.c and you need to call some functions in it, either make a header file hurr.h, or just declare the function right before you call it in derp.cpp. Gah.

// hurr.c

int hurrfunction() {
  return 1;
}


// derp.cpp

int hurrfunction();

int main() {
  return hurrfunction();
}


TADA!

This would be a thousand times easier if you would show us the damn prototype instead of calling it 'hurrfunction()'. C++ supports argument overloading so the signature is kind of important.

>>8
If you extern "C" it and hurr.c is compiled as C++ (as he seems to be doing), this will fail to link. This is of course why the best solution is to always have a hurr.h file which hurr.c includes, and hurr.h has extern "C" conditional on __cplusplus. Then you always get C linkage whether you compile hurr.c as C or C++, and you never get any linker errors.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List