You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++链接错误:undefined reference to Reference_Genome::seq[abi:cxx11] 求助

解决C++多线程程序的链接错误问题

Hey there! Let's break down your linker errors step by step—most of them are actually a common pitfall with C++ static class members, and we can fix the pthread issue at the same time.

1. The Core Issue: Undefined Static Class Members

Looking at your error messages, almost all of the undefined reference errors point to static members of Reference_Genome like seq, idx, mtxLock, and filepaths.

In C++, when you declare a static member inside a class/struct, you're only telling the compiler that this member exists. You must define it outside the class to allocate memory for it—otherwise, the linker can't find the actual variable to link against.

Fix for Static Members

Add these lines after the definition of Reference_Genome (outside the struct):

// Define static members outside the class
long unsigned int Reference_Genome::idx = 0;
map<string, string> Reference_Genome::seq;
pthread_mutex_t Reference_Genome::mtxLock = PTHREAD_MUTEX_INITIALIZER;
vector<string> Reference_Genome::filepaths;
  • For mtxLock, we initialize it with PTHREAD_MUTEX_INITIALIZER to ensure the mutex is properly set up (this is required for pthread mutexes).
  • The other members can be initialized with default values (like idx = 0) or left to their default constructors.

You mentioned adding -pthread didn't work—make sure you're including it correctly in your compile command. Also, since you're using std::filesystem, you need to compile with C17 or later (this feature was added in C17).

Your full compile command should look like this:

g++ testSeq.cpp -o testSeq -std=c++17 -pthread
  • -std=c++17 enables C++17 features (required for std::filesystem).
  • -pthread tells the compiler to link against the pthread library, which is needed for functions like pthread_cancel, pthread_mutex_lock, etc. It must be included in both compilation and linking steps (the g++ driver handles both when you add this flag).

3. About the CXX11 ABI Warning

The [abi:cxx11] tag in your errors indicates your code is using the C11 ABI (which is the default for GCC 5+). The macro issue you referenced usually comes up when mixing libraries compiled with the old ABI and new ABI—but in your case, this is a red herring. Once you fix the static member definitions and pthread linking, this error should disappear because the linker will find the correct C11 ABI-compliant symbols.

Modified Full Code

Here's your code with the static member definitions added:

#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) { }
};

// Define static members here
long unsigned int Reference_Genome::idx = 0;
map<string, string> Reference_Genome::seq;
pthread_mutex_t Reference_Genome::mtxLock = PTHREAD_MUTEX_INITIALIZER;
vector<string> Reference_Genome::filepaths;

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

Final Notes

  • Compile with the command I shared above to ensure all dependencies are linked correctly.
  • Once you fix these issues, you can start implementing the actual logic in writing and distribution functions (right now they're empty, but the linker will no longer complain about missing static members).

内容的提问来源于stack exchange,提问作者CSY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 09:43:14