Undefined reference - should there be a difference

2019-07-06 05:25发布

问题:

Perhaps this is answered out there somewhere and I just don't know the words to search for it. I am trying to understand if I am seeing a bug or if I don't understand the minus operator. Is the assignment of the constant in the header causing the compiler to trip over the '-'?

My project has the following structure, including generated files:

test
├── src
│   ├── TipCoordinate.cpp
│   └── TipCoordinate.hpp
└── UnitTest
    ├── main.cpp
    ├── main.o
    ├── Makefile
    ├── TipCoordinate.o
    ├── UnitTest.pro
    └── UnitTest.pro.user

I have a constant defined in my class header as follows:

class TipCoordinate{
public:
    TipCoordinate() {};
    ~TipCoordinate() {};
    void Z(const float z);
private:
    static const float sREFERENCE_SPHERE_RADIUS = 12;
};

The following code in my class cpp file will not compile:

#include "TipCoordinate.hpp"
void TipCoordinate::Z(const float z){
    float z_origin_ = -sREFERENCE_SPHERE_RADIUS;
}

I receive the error .../model/TipCoordinate.cpp:143: error: undefined reference to 'TipCoordinate::sREFERENCE_SPHERE_RADIUS'

However, if I just change it as follows then it is perfectly happy:

#include "TipCoordinate.hpp"
void TipCoordinate::Z(const float z){
    float z_origin_ = -1*sREFERENCE_SPHERE_RADIUS;
}

Alternatively, this works also:

#include "TipCoordinate.hpp"
void TipCoordinate::Z(const float z){
    float z_origin_ = 0-sREFERENCE_SPHERE_RADIUS;
}

I think that Adam Rosenfield's answer addresses my question, but in response to some of the comments I have replaced the above excerpts with a minimal, complete example that reproduces the problem. As some surmised, I am using g++. The version is g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2 and it seems likely that the optimizer is affected because I am using "Qt Creator 2.1.0, Based on Qt 4.7.2 (32 bit), Built on Mar 11 2011". Here is my project file:

QT       -= gui
INCLUDEPATH += ../src
TEMPLATE = app
SOURCES += main.cpp \
    ../src/TipCoordinate.cpp
HEADERS += \
    ../src/TipCoordinate.hpp

My main file is simply the following, but changing the project template to lib and removing the main file causes the issue to disappear.

int main() { return 0; }

I suspect that Qt Creator matters because in addition to changing the template, when I change other seemingly irrelevant factors the issue disappears or reappears. For that reason, I have included the Qt Creator generated Makefile below:

#############################################################################
# Makefile for building: UnitTest
# Generated by qmake (2.01a) (Qt 4.7.3) on: Sun Oct 14 15:05:23 2012
# Project:  UnitTest.pro
# Template: app
# Command: /usr/local/Trolltech/QtEmbedded-4.7.3/bin/qmake -spec /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ CONFIG+=debug QMLJSDEBUGGER_PATH=/usr/share/qtcreator/qml/qmljsdebugger -o Makefile UnitTest.pro
#############################################################################

####### Compiler, tools and options

CC            = gcc
CXX           = g++
DEFINES       = -DQT_NETWORK_LIB -DQT_CORE_LIB -DQT_SHARED
CFLAGS        = -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
CXXFLAGS      = -pipe -g -Wall -W -D_REENTRANT $(DEFINES)
INCPATH       = -I/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ -I. -I/usr/local/Trolltech/QtEmbedded-4.7.3/include/QtCore -I/usr/local/Trolltech/QtEmbedded-4.7.3/include/QtNetwork -I/usr/local/Trolltech/QtEmbedded-4.7.3/include -I../src -I.
LINK          = g++
LFLAGS        = -Wl,-rpath,/usr/local/Trolltech/QtEmbedded-4.7.3/lib
LIBS          = $(SUBLIBS)  -L/usr/local/Trolltech/QtEmbedded-4.7.3/lib -lQtNetwork -L/usr/local/Trolltech/QtEmbedded-4.7.3/lib -lQtCore -lpthread 
AR            = ar cqs
RANLIB        = 
QMAKE         = /usr/local/Trolltech/QtEmbedded-4.7.3/bin/qmake
TAR           = tar -cf
COMPRESS      = gzip -9f
COPY          = cp -f
SED           = sed
COPY_FILE     = $(COPY)
COPY_DIR      = $(COPY) -r
STRIP         = strip
INSTALL_FILE  = install -m 644 -p
INSTALL_DIR   = $(COPY_DIR)
INSTALL_PROGRAM = install -m 755 -p
DEL_FILE      = rm -f
SYMLINK       = ln -f -s
DEL_DIR       = rmdir
MOVE          = mv -f
CHK_DIR_EXISTS= test -d
MKDIR         = mkdir -p

####### Output directory

OBJECTS_DIR   = ./

####### Files

SOURCES       = main.cpp \
        ../src/TipCoordinate.cpp 
OBJECTS       = main.o \
        TipCoordinate.o
DIST          = /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/g++.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/unix.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/linux.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/qws.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qconfig.pri \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/modules/qt_webkit_version.pri \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_functions.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_config.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/exclusive_builds.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_pre.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/debug.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_post.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/warn_on.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/unix/thread.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/moc.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/resources.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/uic.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/yacc.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/lex.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/include_source_dir.prf \
        UnitTest.pro
QMAKE_TARGET  = UnitTest
DESTDIR       = 
TARGET        = UnitTest

first: all
####### Implicit rules

.SUFFIXES: .o .c .cpp .cc .cxx .C

.cpp.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cc.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.cxx.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.C.o:
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o "$@" "$<"

.c.o:
    $(CC) -c $(CFLAGS) $(INCPATH) -o "$@" "$<"

####### Build rules

all: Makefile $(TARGET)

$(TARGET):  $(OBJECTS)  
    $(LINK) $(LFLAGS) -o $(TARGET) $(OBJECTS) $(OBJCOMP) $(LIBS)

Makefile: UnitTest.pro  /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++/qmake.conf /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/g++.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/unix.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/linux.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/qws.conf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qconfig.pri \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/modules/qt_webkit_version.pri \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_functions.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_config.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/exclusive_builds.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_pre.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/debug.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_post.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/warn_on.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/unix/thread.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/moc.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/resources.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/uic.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/yacc.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/lex.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/include_source_dir.prf \
        /usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtNetwork.prl \
        /usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtCore.prl
    $(QMAKE) -spec /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ CONFIG+=debug QMLJSDEBUGGER_PATH=/usr/share/qtcreator/qml/qmljsdebugger -o Makefile UnitTest.pro
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/g++.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/unix.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/linux.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/common/qws.conf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qconfig.pri:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/modules/qt_webkit_version.pri:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_functions.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt_config.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/exclusive_builds.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_pre.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/debug.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/default_post.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/warn_on.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/qt.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/unix/thread.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/moc.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/resources.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/uic.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/yacc.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/lex.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/features/include_source_dir.prf:
/usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtNetwork.prl:
/usr/local/Trolltech/QtEmbedded-4.7.3/lib/libQtCore.prl:
qmake:  FORCE
    @$(QMAKE) -spec /usr/local/Trolltech/QtEmbedded-4.7.3/mkspecs/qws/linux-x86-g++ CONFIG+=debug QMLJSDEBUGGER_PATH=/usr/share/qtcreator/qml/qmljsdebugger -o Makefile UnitTest.pro

dist: 
    @$(CHK_DIR_EXISTS) .tmp/UnitTest1.0.0 || $(MKDIR) .tmp/UnitTest1.0.0 
    $(COPY_FILE) --parents $(SOURCES) $(DIST) .tmp/UnitTest1.0.0/ && $(COPY_FILE) --parents ../src/TipCoordinate.hpp .tmp/UnitTest1.0.0/ && $(COPY_FILE) --parents main.cpp ../src/TipCoordinate.cpp .tmp/UnitTest1.0.0/ && (cd `dirname .tmp/UnitTest1.0.0` && $(TAR) UnitTest1.0.0.tar UnitTest1.0.0 && $(COMPRESS) UnitTest1.0.0.tar) && $(MOVE) `dirname .tmp/UnitTest1.0.0`/UnitTest1.0.0.tar.gz . && $(DEL_FILE) -r .tmp/UnitTest1.0.0


clean:compiler_clean 
    -$(DEL_FILE) $(OBJECTS)
    -$(DEL_FILE) *~ core *.core


####### Sub-libraries

distclean: clean
    -$(DEL_FILE) $(TARGET) 
    -$(DEL_FILE) Makefile


check: first

mocclean: compiler_moc_header_clean compiler_moc_source_clean

mocables: compiler_moc_header_make_all compiler_moc_source_make_all

compiler_moc_header_make_all:
compiler_moc_header_clean:
compiler_rcc_make_all:
compiler_rcc_clean:
compiler_image_collection_make_all: qmake_image_collection.cpp
compiler_image_collection_clean:
    -$(DEL_FILE) qmake_image_collection.cpp
compiler_moc_source_make_all:
compiler_moc_source_clean:
compiler_uic_make_all:
compiler_uic_clean:
compiler_yacc_decl_make_all:
compiler_yacc_decl_clean:
compiler_yacc_impl_make_all:
compiler_yacc_impl_clean:
compiler_lex_make_all:
compiler_lex_clean:
compiler_clean: 

####### Compile

main.o: main.cpp 
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o main.o main.cpp

TipCoordinate.o: ../src/TipCoordinate.cpp ../src/TipCoordinate.hpp
    $(CXX) -c $(CXXFLAGS) $(INCPATH) -o TipCoordinate.o ../src/TipCoordinate.cpp

####### Install

install:   FORCE

uninstall:   FORCE

FORCE:

I see a scroll box has appeared, but if there is a way to make that appear more concise, I welcome any tips.

回答1:

The difference in behavior you're seeing is to the optimizer. For whatever reason, the optimizer decides to optimize out the math and replace it with a floating-point constant in some of the cases, but not all of the cases. When it does that, no error occurs, because the variable sREFERENCE_SPHERE_RADIUS is never referenced anywhere.

What should be happening is that you ought to be getting the "undefined reference" error in all of the cases, because the variable sREFERENCE_SPHERE_RADIUS needs to be given an explicit definition outside of the class definition. The C++ standard only allows you to give an initializer of a static class member if it's of integral or enumerated type:

// Header file
class TipCoordinate
{
    static const float sREFERENCE_SPHERE_RADIUS;  // declaration
};

// Source file
const float TipCoordinate::sREFERENCE_SPHERE_RADIUS = 12;  // definition

From C++03 §9.2/4:

A member-declarator can contain a constant-initializer only if it declares a static member (9.4) of const integral or const enumeration type, see 9.4.2.

In the latest version of the C++ language, C++11, the rules are a little looser, and you can define floating-point constants in the declaration, but you have to use constexpr instead of const.



回答2:

(I had to edit this answer quite a bit due to a serious thinko! So, just hoping this doesn't invalidate any comments.)

Assuming the code you've shown is really all there is (a complete minimal example would clarify things!), then it is a compiler bug that it compiles at all!

You can work around it various ways.

First, since you have a header + an implementation file, then simply remove the initialization in the in-class declaration, and add this in the cpp file:

float const TipCoordinate::sREFERENCE_SPHERE_RADIUS = 12.0;

Note the for a constant of integral type you can initialize it in the in-class declaration. Also, with C++11 you can use constexpr to allow initialization for the in-class declaration. Anyway, the declaration in the class is technically a pure declaration, while the one in the cpp file, if any, is a definition (even without an initializer, as could be the case for a constant of integral type).

For a header-only module you can instead do e.g. …

static double referenceSphereRadius() { return 12.0; }

or e.g. …

template< class Dummy >
class TipCoordinate_constants
{
protected:
    static double const referenceSphereRadius;
};

template< class Dummy >
double const TipCoordinate_constants<Dummy>::referenceSphereRadius = 12.0;

class TipCoordinate
    private TipCoordinate_constants<void>
{
    // ...
};

These techniques work fine also with a C++03 compiler.

Note the non-use of SHOUTING UPPERCASE. Reserve that for macro names, please. C++ is not Java.