I have a program were I was given the compiled .o file but I do not have the original .cc file and also I only have a half-way finished header file. The header file has all the method signatures but is lacking the private variable declarations. I am trying to get this .o file to work with the project but get a segmentation fault at the constructor of the class defined by the .o file. The program compiles. How do I get this to work? The program is a homework assignment and the teacher does not want us to see the .cc file. Also my teacher knows about the issue. I am just trying to figure it out on my own (and hopefully with the help of you guys :) ). I thought I had done this before a while ago with another teacher but did not have any problems. There is a makefile that is used to compile the program.
-
How are you trying to compile your project, and what errors are you getting? – ObscureRobot Oct 30 '11 at 01:35
-
1Does the header lack private class members or only other stuff? – Daniel Oct 30 '11 at 01:38
-
Does the class have any static methods? Could you post the header? I think this is solvable. – Beta Oct 30 '11 at 02:57
1 Answers
If you are using a C++ program, and the header file includes class definitions, the class definitions must exactly match those that were used to originally build the file. This is the One Definition Rule. If your professor has removed private variable declarations from the class definitions, you will likely end up with crashes; this is because your different .o files will disagree about the size of the objects defined by those classes.
If your professor wants to hide the implementation of the class, they should use the p/impl pattern. If you want to use the header file, you must remove the mangled class definitions entirely and not attempt to use them (you can use a forward definition as in class Foo;
to satisfy any functions which take/return the class as a pointer parameter, however).

- 224,562
- 31
- 268
- 324
-
Only able to pass the class to functions that take a pointer is rarely possible in C++ (unless you use C style classes) – Daniel Oct 30 '11 at 01:40
-
@Dani, indeed, but if you don't have the real declaration that's the best you can do... – bdonlan Oct 30 '11 at 02:01
-
There is another way: find what size the class is by the methods disassembly, and them pad it to that size. – Daniel Oct 30 '11 at 03:13