首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >链接器错误:对`Reference_Genome::seq[abi:cxx11 11]的未定义引用

链接器错误:对`Reference_Genome::seq[abi:cxx11 11]的未定义引用
EN

Stack Overflow用户
提问于 2020-06-29 15:41:28
回答 1查看 1.3K关注 0票数 1

我对c和c++非常陌生,所以请尝试解释更具体的我应该做什么。该程序尝试使用多线程从目录中读取文件,并将这些信息存储在地图中,以便以后使用。

我一直在寻找类似的职位。不过,我想不出。

https://github.com/kaldi-asr/kaldi/issues/938中,它说:“如果您收到关于未定义的符号引用的链接器错误,这些符号涉及std::__cxx11命名空间中的类型或标记abi:cxx11 11,那么它可能表明您试图将使用_GLIBCXX_USE_CXX11_ABI宏不同值编译的对象文件链接在一起。”

undefined reference to `pthread_cancel'的解决方案(添加"-pthread“标志)也不起作用。

我的代码是

代码语言:javascript
复制
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <map>
#include <algorithm>
#include <random>
#include <unistd.h>
#include <cmath>
#include <stdlib.h>
#include <mutex>
#include <sys/wait.h>
#include <filesystem>
#include <string>
#include <pthread.h>

#define time_str(s) (s < 60 ? (to_string(s) + " second(s)") : (s < 3600 ? (to_string((s) / 60) + " minute(s)") : (to_string((s) / 3600) + " hour(s) and " + to_string(((s) % 3600) / 60) + " minute(s)")))
using namespace std;
namespace fs = std::filesystem;

struct MyGenom
{
    vector<string> filepaths;
    map<string, string> seq;
};

void check_rv(int rv) {
    if (rv != 0) {
        printf("Error: Value is %d\n", rv);
        exit(1);
    }
}

struct Reference_Genome {
    static long unsigned int idx;
    static map <string, string> seq;
    static pthread_mutex_t mtxLock;
    static vector <string> filepaths;

    static void writing(string path) {
 
    }

    static void *distribution(void *var) {
        
    }

    Reference_Genome(string dir, unsigned int n_threads) {
    
    }
};

int main(int argc, char const *argv[]) {
    string dir = "./data/ex_seq";
    unsigned int n_threads = 5;
    Reference_Genome ref(dir, n_threads);
    cout << "chr6: " << ref.seq["chr6"] << endl;
    cout << "chr9: " << ref.seq["chr9"] << endl;
    cout << "chr13: " << ref.seq["chr13"] << endl;
}

gcc版本是“线程模型: posix gcc版本9.3.0 (Ubuntu9.3.0-10ubuntu2)”。

错误是

代码语言:javascript
复制
testSeq.cpp:97: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:98: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:99: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::writing(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
/testSeq.cpp:46: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:48: undefined reference to `Reference_Genome::seq[abi:cxx11]'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::distribution(void*)':
testSeq.cpp:55: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:55: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:56: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:57: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:57: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:58: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:58: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:59: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: /tmp/cctfwVX2.o: in function `Reference_Genome::Reference_Genome(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int)':
testSeq.cpp:68: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
/usr/bin/ld: testSeq.cpp:70: undefined reference to `Reference_Genome::idx'
/usr/bin/ld: testSeq.cpp:72: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:85: undefined reference to `Reference_Genome::mtxLock'
/usr/bin/ld: testSeq.cpp:88: undefined reference to `Reference_Genome::filepaths[abi:cxx11]'
collect2: error: ld returned 1 exit status
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-29 15:43:57

当您在类中声明静态变量时,还必须在类之外声明它一次。在本例中,您可以将其放在C++文件的底部或main()函数与class Reference_Genome定义之间:

代码语言:javascript
复制
long unsigned int Reference_Genome::idx;
map <string, string> Reference_Genome::seq;
pthread_mutex_t Reference_Genome::mtxLock;
vector <string> Reference_Genome::filepaths;

其思想是您可以将类定义放入一个头文件中,包含在多个不同的编译单元中,但是静态变量只定义一次,即在您选择的一个.cpp文件中。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62641743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档