简体   繁体   中英

GDB: There is no member named ""

I'm writing some code that is calling some classes from a much larger project. Let's call the large project SampleProject . I have the static library of the SampleProject called libSampleProject.a. I cannot show the actual code, but I will provide some examples:

Let's say the SampleProject has a Class named SamplePointers in a source file called SamplePointers.cpp.

In the SamplePointers.cpp, there are pointers to other classes within the SampleProject. So for example, there is a class named ReadXml, and ReadXml is a member of the SamplePointers class as a pointer.

class SamplePointers {
    public:
        ReadXml * readXmlObject;
        <MANY OTHER POINTERS TO CLASSES>
}

FYI: the pointers are initialized in the constructor of the SamplePointers class.

In my CPP file, main.cpp, where I'm calling said Class looks like this following:

#include <iostream>
#include <SampleClass.hpp>

int main() {
    SamplePointers * sampleObject = new SamplePointers;
    sampleObject->read("sampleFile.xml");
    std::cout << sampleObject->readXmlObject->xmlDataField << "\n";
}

SamplePointers also has a read function which reads in XML Fields. The fields will be available in the ReadXml class via accessing member variables. Running this executable, the value for xmlDataField prints out. However, when debugging on GDB, I set a breakpoint on say the read line, and then I type in this path, sampleObject->readXmlObject->xmlDataField, hit enter, and then gdb says that there is no member named readXmlObject.

This is curious, since I'm able to print it out, with std::cout, but gdb cannot physically access the object member variables.

Any ideas why gdb wouldn't be able to access the members?

Sample GDB output:

(gdb) file out
(gdb) b 7
(gdb) run
(gdb) <breaks at line 7>
(gdb) p sampleObject->readXmlObject->xmlDataField
There is no member named readXmlObject

Also, here is a look at what the Makefile looks like:

INCLUDES = -I/path/to/SamplePointersHeader -I/path/to/ReadXmlHeader -I/many/more/headers
main:
    g++ main.cpp $(INCLUDES) -L/path/to/SampleProjectLib -lSampleProject -o out
debug:
    g++ -g main.cpp $(INCLUDES) -L/path/to/SampleProjectLib -lSampleProject -o out

Thanks to @GM, for the suggestion. I believe the problem may have been that when I made the library, I compiled it without the debug flag, ie the -g flag. I re-compiled the library, made the library out of the object files, and even changed the optimization level from -O3 to -O0 which will help with debugging.

I then recompiled my main.cpp file with the newly made library, passed it through GDB and it was able to find the Class members.

Thanks for all of the input!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM