Apologies for not providing simple runnable failure code. The error is part of the larger codebase that would require a lot of refactoring.
I'm running into a very weird linking problem with my code that so far I can't solve.
I have a class with static constexpr const char *
for some strings and local std::sunique_ptr
. The pointer is to a different templated class which contains another templated class (#2).
The main class is like this (abridged):
class Manager {
public:
Manager();
virtual ~Manager();
private:
// Topic Constants
static constexpr const char* kActuatorsCommand = "ActuatorsCommand";
static constexpr const char* kActuatorsProxy = "ActuatorsProxy";
std::unique_ptr<DataReader> faults_;
};
So the DataReader
constractor takes two const string &
parameters.
If I declare faults_
as a regular old pointer and create it with new
the code runs and links just fine: DataReader *faults_ = new DataReader<uint32_t>(kActuatorsCommand, kActuatorsProxy)
.
However, if I use std::make_unique
the linker complains that there is an undefined reference to those static const char*
strings, even though they are in the header of the class.
Also, If I remove the #2 class everything links fine.
Using gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
I understand it may be rather vague question, but some direction where to look would be appreciated.
Also, this question may be similar to this one. However, in my case everything is on one binary.
Update: finally found how to reproduce it.
class DataReader {
public:
explicit DataReader(const std::string& topic, const std::string& library_name)
: topic_(topic),
library_name_(library_name) {
}
private:
const std::string name_;
const std::string topic_;
const std::string library_name_;
};
#include <memory>
#include "DataReader.h"
class Manager {
public:
Manager();
virtual ~Manager();
private:
// Topic Constants
static constexpr const char* kActuatorsCommand = "ActuatorsCommand";
static constexpr const char* kActuatorsProxy = "ActuatorsProxy";
std::unique_ptr<DataReader> faults_;
};
Manager::Manager() {
faults_ = std::make_unique<DataReader>(kActuatorsCommand, kActuatorsProxy);
}
Manager::~Manager() {}
The code fails to link when compiled with -o0
. With -03
it links fine.
g++ -O0 -Wall -Wconversion -lstdc++ -pthread -std=c++14 -o ex3 src/ex3.cpp
/tmp/ccJebZ18.o: In function `Manager::Manager()':
ex3.cpp:(.text+0x41): undefined reference to `Manager::kActuatorsProxy'
ex3.cpp:(.text+0x48): undefined reference to `Manager::kActuatorsCommand'
collect2: error: ld returned 1 exit status
Makefile:8: recipe for target 'ex3' failed