简体   繁体   English

Linker 错误:未定义对“Reference_Genome::seq[abi:cxx11]”的引用

[英]Linker error: undefined reference to `Reference_Genome::seq[abi:cxx11]'

I am pretty new to c and c++, so please try explain more specific what I should do.我对 c 和 c++ 很陌生,所以请尝试更具体地解释我应该做什么。 The program tries to read files from a directory using multithreads, and store the information in a map so that it can be used later.该程序尝试使用多线程从目录中读取文件,并将信息存储在 map 中,以便以后使用。

I have been looking for similar posts.我一直在寻找类似的帖子。 However, I am not able to figure out.但是,我无法弄清楚。

In https://github.com/kaldi-asr/kaldi/issues/938 , it said that "If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro."https://github.com/kaldi-asr/kaldi/issues/938中,它说“如果您收到 linker 错误,关于未定义的符号引用涉及 std::__cxx11 命名空间或标签 [abi:cxx1111 ] 那么它可能表明您正在尝试将使用 _GLIBCXX_USE_CXX11_ABI 宏的不同值编译的 object 文件链接在一起。”

The solution for undefined reference to `pthread_cancel' (add "-pthread" flag does not work either. 对 `pthread_cancel' 的未定义引用的解决方案(添加“-pthread”标志也不起作用。

My code is我的代码是

#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) {
        ifstream rsf(path);
        string line, cur_chr;
        while (getline(rsf, line)) {
            if (line.substr(0, 1) == ">") {
                cur_chr = line.substr(1);
                seq[cur_chr] = "";
            } else
                seq[cur_chr] += line;
        }
        rsf.close();
    }

    static void *distribution(void *var) {
        string path;
        while (idx < filepaths.size()) {
            check_rv(pthread_mutex_lock(&mtxLock));
            path = filepaths[idx];
            idx++;
            check_rv(pthread_mutex_unlock(&mtxLock));
            writing(path);
        }
        return NULL;
    }

    Reference_Genome(string dir, unsigned int n_threads) {
        cout << "Reading the reference genome from \'" << dir << "\'" << endl;
        for (const auto &entry : fs::directory_iterator(dir))
            filepaths.push_back(entry.path());
        time_t t1 = time(NULL);
        idx = 0;
        // init lock
        int rv = pthread_mutex_init(&mtxLock, NULL);
        check_rv(rv);
        pthread_t workers[n_threads];
        int rs;
        struct MyGenom param;
        for (unsigned int th = 0; th < n_threads; th++) {
            rs = pthread_create(&workers[th], NULL, distribution, (void *)((unsigned long)th));
            check_rv(rs);
        }
        for (unsigned int th = 0; th < n_threads; th++) {
            rs = pthread_join(workers[th], NULL);
            check_rv(rs);
        }
        pthread_mutex_destroy(&mtxLock);

        time_t t2 = time(NULL);
        cout << filepaths.size() << " sequences were read in " << time_str((long) t2 - (long) t1)
             << ".\n----------------\n";
    }
};

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;
}

The gcc version is "Thread model: posix gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)". gcc 版本是“线程 model:posix gcc 版本 9.3.0(Ubuntu 9.3.0-10ubuntu2)”。

The error is错误是

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

When you declare static variables inside a class, you must also declare it exactly once outside of the class.当您在 class 中声明 static 变量时,您还必须在 class 之外仅声明一次。 In this case, you could put this in the bottom of your C++ file or in between the main() function and the class Reference_Genome definition:在这种情况下,您可以将其放在 C++ 文件的底部或main() function 和class Reference_Genome定义之间:

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

The idea is that you can put the class definition inside a header file, to be included in multiple different compilation units, but the static variables are only defined once, in one.cpp file of your choosing.这个想法是,您可以将 class 定义放在 header 文件中,以包含在多个不同的编译单元中,但 static 变量仅在您选择的一个 pp 文件中定义一次。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未定义对 `version[abi:cxx11]' g++ std=C++11 的引用 - undefined reference to `version[abi:cxx11]' g++ std=C++11 对 `sc_dt::sc_uint_base::to_string[abi:cxx11] 的未定义引用 - Undefined reference to `sc_dt::sc_uint_base::to_string[abi:cxx11] 对 google::protobuf::internal::empty_string_[abi:cxx11] 的未定义引用 - Undefined reference to google::protobuf::internal::empty_string_[abi:cxx11] 使用abi:cxx11的GCC未定义引用 - GCC undefined references with abi:cxx11 使用GCC 4.8.5构建的应用程序中对cxx11函数的未定义引用 - Undefined reference to cxx11 functions in app built with GCC 4.8.5 编译affdex linux示例应用程序时对进程(std :: __ cxx11 :: basic_string ...)的未定义引用 - Undefined reference to process(std::__cxx11::basic_string … ) when compiling affdex linux sample applications 在Travis CI上的Boost中对`std :: __ cxx11 :: basic_string的未定义引用 - undefined reference to `std::__cxx11::basic_string in Boost on Travis CI 未定义对`readVector(std :: __ cxx11 :: basic_string的引用) <char, std::char_traits<char> ,std :: allocator <char> &gt;)” - undefined reference to `readVector(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)' 在 Ubuntu 15.10 上使用 g++-4.9 编译时出现运行时错误 [abi:cxx11] - Runtime error [abi:cxx11] when compile with g++-4.9 on Ubuntu 15.10 在小型项目上使用abi:cxx11链接问题 - Link problems with abi:cxx11 on small project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM