我正在使用来自这里的这里项目模板,它使用CMake/Make来构建这个项目,并且包含了大量的电池(我对C++相当陌生,但一般不编程)。
我开始通过实现头文件中的所有内容来完成我的项目,现在我想尝试一下我的代码。但是,当我试图编译代码(使用./configure和make)时,在链接过程中会遇到以下错误。
[ 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"
发布于 2018-10-16 18:02:52
如果在类中声明静态数据成员,则类似于将变量声明为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.这将修复错误。
未明确提及“协调员::库存”
我使用了DRY (不要重复自己)的模式,并引入了ty胡枝子,以避免两次输入STL映射。
编辑:(根据OP给出的评论来完成答案)。
未解决的外部因素
未定义的对``std::文件系统的引用::.
可以通过链接stdc++fs库来解析。为此,应该将stdc++fs添加到CMakeFile中的图书馆中。
https://stackoverflow.com/questions/52838081
复制相似问题