在小型项目上使用abi:cxx11链接问题

时间:2018-10-16 14:41:35

标签: c++ linker abi

我正在使用here中的C ++ Project模板,它使用CMake / Make来构建项目,并且包含很多电池(我对C ++还是陌生的,但总体上不编程)。

我开始通过在头文件中实现所有内容来开始我的项目,现在想尝试一下我的代码。但是,当我尝试使用./configuremake编译代码时,在链接期间遇到以下错误。

[ 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

经过大量搜索,我在基本目录的add_definitions(-D_GLIBCXX_USE_CXX11ABI=0)文件中尝试了CMakeLists.txt。我还通过添加-v进行了检查,这被传递给gcc。但是,它实际上没有任何改变。在另一个线程中,我发现在访问之前忘记ClassName::时会遇到相同的错误 static_variables,所以我检查了一下(在相关行中没有发生)。

代码的相关部分:

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

我在我的项目中同时使用了Boost库和Qt-Boost已包含在内,并且由于该回购协议已有4年历史,所以我怀疑Cxx11 ABI可能存在此问题。但是也许我只是忽略了Coordinator.h中的一些简单内容。我的.cpp文件仅包含#include "<File>.h"

1 个答案:

答案 0 :(得分:1)

如果在类中声明静态数据成员,则将变量声明为extern类似于。这段代码

class Foo {
    static int data;
};

声明Foo :: data。您需要将变量定义为此

int Foo:data;

在编译单元中。

您的数据成员Coordinator::stocks需要类似的定义:

class Coordinator {
private:
  typedef std::unordered_map<std::string, StockData> stocks_t; 
  static stocks_t stocks;      // declaration
};

Coordinator::stocks_t Coordinator::stocks;// definition.

这将纠正错误

  

对“ Coordinator :: stocks”的未定义引用

我使用了DRY(不要重复自己)模式,并引入了typedef以避免两次输入STL映射。

编辑:(要完成答案,请根据OP中的评论)。

未解决的外部因素

  

对`std :: filesystem :: ......的未定义引用

可以通过链接stdc++fs库来解决

。为此,应将stdc++fs添加到CMakeFile的TARGET_LINK_LIBRARIES中。