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.