vendredi 2 juin 2017

static const integral data members in class without optimization [duplicate]

This question already has an answer here:

I know only integral datatypes can be declared in the class, but it doesn't seem to work with gcc without optimization..

I have a large C++11 project with some class that has this code. The static const members are used inside the main function.

class CAudioDm : public IAudioDm, public ILoggingHelper
{
public:
  CAudioDm();
  ~CAudioDm();

  static const uint16_t UDP_PORT_PA { 60000 };
  static const uint16_t UDP_PORT_INTERCOM { 60100 };
  static const uint16_t UDP_PORT_INTERCOM_JOIN { 60200 };

  // THIS GIVES THE SAME BEHAVIOR AS ABOVE
  //static constexpr uint16_t UDP_PORT_PA { 60000 };
  //static constexpr uint16_t UDP_PORT_INTERCOM { 60100 };
  //static constexpr uint16_t UDP_PORT_INTERCOM_JOIN { 60200 };

  //THIS WORKS, PUT GLOBAL INIT IN CPP FILE
  //static const uint16_t UDP_PORT_PA;
  //static const uint16_t UDP_PORT_INTERCOM;
  //static const uint16_t UDP_PORT_INTERCOM_JOIN;
}

Without any gcc optimazation i get this error:

[100%] Linking CXX executable PEI
/usr/bin/cmake -E cmake_link_script CMakeFiles/PEI.dir/link.txt --verbose=1
/usr/bin/c++   -O0 -DNDEBUG   CMakeFiles/http://ift.tt/2s0Mhzd CMakeFiles/http://ift.tt/2qHsvEL CMakeFiles/http://ift.tt/2s0HygE  -o PEI -rdynamic comp/log/liblog.a comp/audio-decision/libaudio-decision.a comp/cfg-io/libcfg-io.a comp/audio-dm/libaudio-dm.a comp/common-net/libcommon-net.a comp/audio-matrix/libaudio-matrix.a comp/update/libupdate.a comp/cfg-eeprom/libcfg-eeprom.a comp/voip/libvoip.a comp/common/libcommon.a -lboost_system -lboost_filesystem -lboost_program_options -lasound -llinphone -lmediastreamer_base -lmediastreamer_voip -lortp -lpthread comp/log/liblog.a comp/common-net/libcommon-net.a
comp/audio-dm/libaudio-dm.a(CAudioDm.o): In function `CAudioDm::listenPA(bool)':
CAudioDm.cpp:(.text+0x1207): undefined reference to `CAudioDm::UDP_PORT_PA'
comp/audio-dm/libaudio-dm.a(CAudioDm.o): In function `CAudioDm::streamPA(bool)':
CAudioDm.cpp:(.text+0x1b69): undefined reference to `CAudioDm::UDP_PORT_PA'
comp/audio-dm/libaudio-dm.a(CAudioDm.o): In function `CAudioDm::listenIC(bool)':
CAudioDm.cpp:(.text+0x28e9): undefined reference to `CAudioDm::UDP_PORT_INTERCOM'
comp/audio-dm/libaudio-dm.a(CAudioDm.o): In function `CAudioDm::streamIC(boost::uuids::uuid const&, bool, bool)':
CAudioDm.cpp:(.text+0x37c0): undefined reference to `CAudioDm::UDP_PORT_INTERCOM'
CAudioDm.cpp:(.text+0x3b02): undefined reference to `CAudioDm::UDP_PORT_INTERCOM_JOIN'
comp/audio-dm/libaudio-dm.a(CAudioDm.o): In function `CAudioDm::joinIC(boost::uuids::uuid const&, bool)':
CAudioDm.cpp:(.text+0x4729): undefined reference to `CAudioDm::UDP_PORT_INTERCOM_JOIN'
collect2: error: ld returned 1 exit status
CMakeFiles/http://ift.tt/2qHCVnM: recipe for target 'PEI' failed
make[3]: *** [PEI] Error 1
make[3]: Leaving directory '/home/jova/projects/33.97.1600/PEI/_build_gcc-default_release'
CMakeFiles/Makefile2:76: recipe for target 'CMakeFiles/PEI.dir/all' failed
make[2]: *** [CMakeFiles/PEI.dir/all] Error 2
make[2]: Leaving directory '/home/jova/projects/33.97.1600/PEI/_build_gcc-default_release'
Makefile:83: recipe for target 'all' failed
make[1]: *** [all] Error 2
make[1]: Leaving directory '/home/jova/projects/33.97.1600/PEI/_build_gcc-default_release'
Makefile:20: recipe for target 'app' failed
make: *** [app] Error 2 

When i set an optimization level : tried with -O1, -O2 and -O3 it compiles without errors. No other flags except for -std=c++11 ofc.

Tried on:

ubuntu 15.04: gcc-4.8.2 and cmake-3.0.2 and ubuntu 16.04: gcc-5.4 and cmake-3.5.1

Why does it work with optimization but not without? I need -O0 for debugging.

Aucun commentaire:

Enregistrer un commentaire