g++ giving errors where visual studio was happy

I have some code that runs fine on Visual Studio, and I am now trying to compile it on g++. It is giving me 'undefined reference to SomeClass::someMethod() const' in a bunch of places.

Most commonly, it's the following situation:

for (const SomeListNode *node = owner->some_list; node != 0; node = node->getNext())

In this case, I get 'undefined reference to SomeListNode::getNext() const'. This class's header file is explicitly included. Why is this not legal in g++?

edit for more info

I am building with a makefile as such:

CC=g++
CFLAGS=-c -Wall -DDEBUG -g
LDFLAGS=
SOURCES=main.cpp SomeList.cpp SomeListNode.cpp Location.cpp OutputControl.cpp
OBJECTS=$(SOURCES:.cpp=.o)
EXECUTABLE=theprogram

all: $(SOURCES) $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
    $(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o: $(CC) $(CFLAGS) $< -o $@

The line of code originally listed is in the OutputControl.cpp . It is taking a pointer to a SomeListNode and iterating over it. getNext () returns a pointer to another SomeListNode .

It may also be of note that this is only occurring within static functions.


You can only call const-qualified methods on a const-qualified object. If getNext() is not const, then you should use SomeListNode *node instead of const SomeListNode *node .


Undefined references are linker errors, and these are unrelated to header inclusions. You have to make sure that you have compiled the definitions of those functions into some object file, and that the object file is passed to the linker.

Beware that if you are compiling to static libraries, the order of the libraries in the linker command line affects the result. In particular, if a library depends on other libraries, the dependent should appear in the command line before all the depended on libs.

At any rate, compile each object file separately into a .o or a library and use nm to extract the list of symbols that is defined in each one of the translation units that should tell you whether the definition has been compiled or not and will help you determine the order if you are compiling static libs.


I would suggest that you set up dependencies in your makefile so that objects that depends on others are made in the correct order. Also setup a clean target that deletes .o files. After this run make clean, and make.

This is the best suggestion I can think of with the information you have given us.

链接地址: http://www.djcxy.com/p/85446.html

上一篇: start()需要很长时间

下一篇: g ++给视觉工作室感到高兴的错误