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:
Anonymous2010-04-27 11:37
i think you would be comfortable with vb.net
Name:
Anonymous2010-04-27 11:47
Including the files through gcc does not mean instant concatenation
Name:
Anonymous2010-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.
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)?
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:
Anonymous2010-04-27 14:47
put the function declaration for hurrfunction in derp.cpp
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.
>>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:
Anonymous2010-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'
>>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:
Anonymous2010-04-27 16:19
☆???????????????????? ????????????????????????☆
Name:
Anonymous2010-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.
>>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:
Anonymous2010-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:
Anonymous2010-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:
Anonymous2010-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:
Anonymous2010-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?
>>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:
Anonymous2010-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:
Anonymous2010-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:
Anonymous2010-04-28 7:56
>>36
/sarcasm
You haven't heard about MODERN C++ with features like friend members, templates, and templates!