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

Pages: 1-4041-

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 11:37

i think you would be comfortable with vb.net

Name: Anonymous 2010-04-27 11:47

Including the files through gcc does not mean instant concatenation

Name: Anonymous 2010-04-27 12:17

>>2
I know I would be, but better to have experience in smashing my head against a sharp object now than later.

>>3
How else should I approach this?

Name: Anonymous 2010-04-27 12:57

I dont know.

Assuming G++ doesn't treat the .c as C , and compiles to a C++ object, the linking shouldn't be a problem...

Post exact error or gtfo

Name: Anonymous 2010-04-27 12:58

What the fuck? How are you putting a call to this function inside a file you aren't supposed to modify, anyway?

Name: Anonymous 2010-04-27 13:15

Error:
derp.cpp:31: error: âhurrfunctionâ was not declared in this scope

I'm calling a function that is inside hurr.c from inside a derp.cpp function.  The function inside derp.cpp is:

void derp::loadhurr(){
hurrfunction(); //line 31 form the error
}

I'm not sure what the scope I should be using is for the hurrfunction call.

G++ can actually be used to compile C code, though it does give warnings.

Name: Anonymous 2010-04-27 14:02

OK, so hurrfunction is defined in hurr.c, but where is it declared?

You may have to declare it yourself, like this:
extern "C" {
    void hurrfunction (void);
}


Oh no! ???? ???????????????????????? ????????????!

Name: Anonymous 2010-04-27 14:09

Hmm, where would I put that extern declaration?  Inside the derp.h (ie. class derp{ public: extern-etc }) or within derp.cpp somewhere (maybe within loadhurr)?

Thanks for the help so far.

Name: Anonymous 2010-04-27 14:26

>>9
MY ANUS

Name: Anonymous 2010-04-27 14:33

>>9
You DO have #include "derp.h" in derp.cpp?
Actually, what are the contents of hurr.c, derp.h, derp.cpp?

Name: Anonymous 2010-04-27 14:33

>>10
ಠ_ಠ

Name: Anonymous 2010-04-27 14:45

>>11
Yes, derp.h is included in derp.cpp

The contents are not too important as they can work without the hurr.c's function, but I still need to somehow use the hurrfunction.  This is my only error right now, and my googling has turned up a lot of almost-but-still-not related results.

Perhaps my google-fu is weak, but I just can't find much of anything on correctly calling a function from an outside, unchangeable .c file within a c++ object file.  I find loads of results for calling within a main, but an object does not have a main, and those methods just seem to not work.

When I try to use the extern stuff, I get this error:
error: expected unqualified-id before string constant

Not sure what this means...

Name: Anonymous 2010-04-27 14:47

put the function declaration for hurrfunction in derp.cpp

Name: Anonymous 2010-04-27 15:09

Don't put the extern declaration in the middle of a class or namespace. It should be at global scope.

Name: Anonymous 2010-04-27 15:18

>>13
The contents are not too important
They are. Where is your definition of hurrfunction, where are the declarations?

Name: Anonymous 2010-04-27 15:31

???????????????? ????????????????????????

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.

Name: Anonymous 2010-04-27 15:53

>>18
>>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.


Good point.

Name: Anonymous 2010-04-27 16:12

>>18
derp.cpp does not have a main function.  It's an object file, to be compiled to .o

hurr.c has no header, no prototypes, and no main; it has typical std c includes and then goes straight into declaring what the function is.  I'm not supposed to change anything in it, but a prototype for it is: void hurrfunction(); (it just does stuff on its own)

Sorry for frustrating you guys :(  I'm more trying to get an answer to something extremely similar than just have you guys write the whole thing, you know?

If I just plain declare it within derp.h or derp.cpp, I get this error:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../lib64/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'

Name: Anonymous 2010-04-27 16:18

>>20
Did you even read that error? It's saying you haven't defined main, which has nothing to do with any of this.

Name: Anonymous 2010-04-27 16:19

☆???????????????????? ????????????????????????☆

Name: Anonymous 2010-04-27 16:27

>>21
Yes, it's saying I don't have a main, problem here is that I'm not supposed to have a main at all for this object.

I'm just trying to compile a derp object file to be used in my main bidoof program later on.  This derp object needs to use the hurrfunction found in hurr.c at some point.

Name: Anonymous 2010-04-27 16:33

g++ {hurr,derp}.{c,cxx,h,hxx} -o myanus
Gotta read this one aloud.

Name: Anonymous 2010-04-27 16:34

derp
Back to the imageboards, please

Name: Anonymous 2010-04-27 16:36

>>24
>>25
:'(

Name: Anonymous 2010-04-27 16:38

>>3
Find out how to compile a static library.

Name: Anonymous 2010-04-27 16:38

>>27
Not >>3, friggin' >>23, gawd

Name: Anonymous 2010-04-27 16:50

>>23
You really need to learn how to operate your compiler. -c tells GCC to produce an object file without linking. But based on this thread's history, I don't think you know C or C++ so I'm not sure what good this knowledge will do you.

Name: Anonymous 2010-04-27 17:05

>>29
You're right, it should be painfully obvious I'm a newbie at this. 

Used -c, removed -o derp.o, and it compiled without errors.  Created two object files.

Time to look more into this.  Thanks for the help. ٩(•̮̮̃•̃)۶

Name: Anonymous 2010-04-27 17:34

>>20
Learn how compilation in C and C++ works - I tried writing a short explanation but there's no easy way to do this. Also, it seems that you're wrongly using the term object. "Object file" doesn't have anything at all to do with OOP and C++, and I've no idea why you'd call derp an object.

Name: Anonymous 2010-04-27 17:53

>>31
I realize that now, I meant that derp.h and derp.cpp would become the object derp.o upon compilation, which would then be used in bidoof.cpp, which has the main function.  But you're right, this is quite new to me, and I have a lot to read and try. 

I'm actually converting a working C program into a C++ program (with limitations, like hurr.c) and needed to use objects.  Was pretty blind to the compiler before, but this thread has made me investigate it further.  Pretty much a homework problem, but I didn't want to just ask you guys to write everything for me.  I still have more to do but this was one step forward.

Name: Anonymous 2010-04-27 19:20

I meant that derp.h and derp.cpp would become the object derp.o upon compilation
No. The resultant files are called object files, but that doesn't have anything to do with objects.
needed to use objects
What do you mean? You needed to make the program object-oriented?

Name: Anonymous 2010-04-27 20:37

>>33
You're coming on a bit strong, and you might be misinterpreting him. Either way you sound like a bit of a spaz.

Name: Anonymous 2010-04-28 3:51

>>34
Sorry, didn't mean to be mean. Still, I believe that >>32-kun has some wrong assumptions about how high-level C++ is, which will make it harder for him to understand what's really happening.

For example, the phrase
I meant that derp.h and derp.cpp would become the object derp.o upon compilation
sounds like he believes that since derp.cpp defines a class, the result of the compilation is an object.

Name: Anonymous 2010-04-28 6:44

>>32
I'm actually converting a working C program into a C++ program (with limitations, like hurr.c) and needed to use objects.
Why on earth would you do such a thing? C++'s object system is worse than C's; the only thing it really gives you is syntactic sugar for dynamic dispatch.

Name: Anonymous 2010-04-28 7:56

>>36
/sarcasm
You haven't heard about MODERN C++ with features like friend members, templates, and templates!

Did I mention templates?

Name: Anonymous 2010-04-28 8:45

>>37
Don't forget generics, parametric types oh and uh macro-free metaprogramming.

Name: Anonymous 2010-04-28 8:55

>>38

those are templates

Name: Anonymous 2010-04-28 9:09

>>39
You don't say.

Name: Anonymous 2010-04-28 9:28

Fuck, missed a 40 get

Name: Anonymous 2010-04-28 10:48

>>41
For a while I was tempted to reply with a MISS MY ANUS

Name: Anonymous 2010-04-28 11:07

>>42
TEMPT MY ANUS

Name: Anonymous 2010-04-28 12:13

>>43
template my anus

Name: Anonymous 2010-04-28 13:59

>>44
No need, your anus already accepts anything at all. void **

Name: Anonymous 2010-04-28 20:15

>>45
void **my anus

Name: Anonymous 2011-02-02 23:56

Name: Anonymous 2011-02-03 2:28

Name: Anonymous 2011-02-04 14:37

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