Reputation: 410
I'm using the C++ Project template from here, it uses CMake/Make to build the project and has lots of batteries included (I am rather new to C++ but not programming in general).
I started working on my project by implementing everything in header files and now want to try out my code. However, when I try to compile the code (with ./configure
and make
) I'm running into the following error during Linking.
[ 40%] Linking CXX executable ../../bin/projectname
/usr/bin/ld: CMakeFiles/projectname.dir/main.cxx.o: in function `Coordinator::get_price_at(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, boost::gregorian::date)':
/usr/include/c++/8.2.1/bits/unordered_map.h:977: undefined reference to `Coordinator::stocks[abi:cxx11]'
/usr/bin/ld: CMakeFiles/projectname.dir/main.cxx.o: in function `RandomStrategy::rebalance(boost::gregorian::date)':
/usr/include/c++/8.2.1/bits/hashtable.h:1256: undefined reference to `Coordinator::stocks[abi:cxx11]'
/usr/bin/ld: CMakeFiles/projectname.dir/main.cxx.o: in function `Coordinator::read_stock_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/c++/8.2.1/bits/fs_path.h:183: undefined reference to `std::filesystem::__cxx11::path::_M_split_cmpts()'
/usr/bin/ld: CMakeFiles/projectname.dir/main.cxx.o: in function `Coordinator::read_stock_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/c++/8.2.1/bits/fs_dir.h:356: undefined reference to `std::filesystem::__cxx11::directory_iterator::directory_iterator(std::filesystem::__cxx11::path const&, std::filesystem::directory_options, std::error_code*)'
/usr/bin/ld: CMakeFiles/projectname.dir/main.cxx.o: in function `Coordinator::read_stock_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/jan/StockAnalyzer/src/Coordinator.h:55: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator*() const'
/usr/bin/ld: CMakeFiles/projectname.dir/main.cxx.o: in function `Coordinator::read_stock_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/include/c++/8.2.1/bits/unordered_map.h:977: undefined reference to `Coordinator::stocks[abi:cxx11]'
/usr/bin/ld: CMakeFiles/projectname.dir/main.cxx.o: in function `Coordinator::read_stock_data(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/home/jan/StockAnalyzer/src/Coordinator.h:55: undefined reference to `std::filesystem::__cxx11::directory_iterator::operator++()'
collect2: error: ld returned 1 exit status
After lots of searching I tried out add_definitions(-D_GLIBCXX_USE_CXX11ABI=0)
in the CMakeLists.txt
file in the base directory. I checked by also adding -v
, this gets passed to gcc
. However, it changes literally nothing. In another thread I found that you can get the same error when you forget the ClassName::
before accessing
static_variables
, so I checked that to (does not happen in the relevant lines).
The relevant parts of the code:
main.cxx
:
#include <cstdlib>
#include <boost/program_options.hpp>
#include "app.h"
#include "Coordinator.h"
#include "RandomStrategy.h"
namespace po = boost::program_options;
po::options_description getDescription() {
po::options_description desc("Options");
desc.add_options()
("help,h", "Display this message")
("version,v", "Display the version number");
return desc;
}
void start() {
std::string RESOURCES_PATH = "/home/jan/StockAnalyzer/resources/";
long double investment_money = 50000;
Coordinator::read_stock_data(RESOURCES_PATH);
RNGType rng;
boost::uniform_real<> zero_to_one( 0.0, 1.0 );
RandomStrategy r(investment_money, rng, zero_to_one);
Strategy* cur_strat = &r;
}
int main(int argc, char **argv) {
start();
return 0;
}
Coordinator.h
:
#ifndef SRC_COORDINATOR_H_
#define SRC_COORDINATOR_H_
#include <vector>
#include <unordered_map>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/optional.hpp>
#include <boost/tokenizer.hpp>
#include <fstream>
#include <string>
#include <iostream>
#include <filesystem>
#include "StockData.h"
#include "Utility.h"
class Coordinator {
private:
static std::unordered_map<std::string, StockData> stocks;
public:
static StockData get_price_history(const std::string& stock_name) {
return Coordinator::stocks[stock_name];
}
static boost::optional<StockDataPoint> get_price_at(const std::string& stock_name, boost::gregorian::date date) {
StockData sd = Coordinator::stocks[stock_name];
for(StockDataPoint sdp : sd.get_price_history()) {
if(sdp.get_date() == date) {
return boost::optional<StockDataPoint>(sdp);
}
}
return boost::optional<StockDataPoint>();
}
static std::vector<std::string> get_all_stocks() {
return get_keys(Coordinator::stocks);
}
static void read_stock_data(const std::string& path_name) {
for (auto& file_name : std::filesystem::directory_iterator(path_name)) {
std::string file_name_string = file_name.path().string();
StockData cur_data;
std::vector<StockDataPoint> price_history;
std::ifstream stock_file(file_name_string);
std::string line;
while (std::getline(stock_file, line)) {
std::vector<std::string> split_input;
boost::split(split_input, line, [](char c){return c == ',';});
price_history.push_back(StockDataPoint(split_input));
}
cur_data.price_history = std::move(price_history);
Coordinator::stocks[file_name_string] = cur_data;
}
}
};
#endif
I'm using both the Boost Libraries as well as Qt in my Project - Boost was already included and since the repo is 4 years old I suspect it might have this problem with Cxx11 ABI. But maybe I'm just overlooking something simple in Coordinator.h
. My .cpp
files only contain an #include "<File>.h"
Upvotes: 1
Views: 5251
Reputation: 13690
If you declare a static data member in a class, it's similar to declare a variable as extern. This code
class Foo {
static int data;
};
declares Foo::data. You need to define the variable as this
int Foo:data;
in the compilation unit.
Your data member Coordinator::stocks
requires similar definition:
class Coordinator {
private:
typedef std::unordered_map<std::string, StockData> stocks_t;
static stocks_t stocks; // declaration
};
Coordinator::stocks_t Coordinator::stocks;// definition.
This will fix the error
undefined reference to `Coordinator::stocks'
I used the DRY (don't repeat yourself) pattern and introduced a typedef to avoid typing the STL map twice.
Edit:(To complete the answer, based on the comment given from OP).
The unresolved externals
undefined reference to `std::filesystem::...
can be resolved by linking the stdc++fs
library. For this purpose stdc++fs
should be added to the TARGET_LINK_LIBRARIES in the CMakeFile.
Upvotes: 1