首页 > 解决方案 > 错误:包含时“带有 C 链接的模板”和“带有 C 链接的模板特化”

问题描述

问题背景:

ProjectA中,agent_multi_threading.hpp包含在agent.h.
ProjectB中,我试图包含agent.hprocessing_core.h.

ProjectA是用 C 编写的,而ProjectB是用 C++ 编写的。

下面的代码显示了包括的层次结构:

/* File: agent_multi_threading.hpp ProjectA */

#ifdef __cplusplus

 #include <atomic>
 using namespace std;

#else
/* File: agent.h in ProjectA */

#include "agent_multi_threading.hpp"
/* File: processing_core.h ProjectB */

extern "C" {
#include "agent.h"
}

错误信息:

In file included from /<compiler_path>/gnu/gcc/8.2.0/rhesys/include/c++/8.2.0/atomic:41,
            from /<dir_path>/ProjectB/src/agent_multi_threading.hpp:30,
            from /<dir_path>/ProjectB/src/agent.h:10,
            from /<dir_path>/ProjectA/src/processing_core.cpp:5:
/<compiler_path>/gnu/gcc/8.2.0/rhesys/include/c++/8.2.0/bits/atomic_base.h:109:3: error: template with C linkage
    template<typename _Tp>
    ^~~~~~~~

/<dir_path>/ProjectA/src/processing_core.cpp:5:1: note: 'extern "C"' linkage started here
  extern "C" {
  ^~~~~~~~~~
          from /<dir_path>/ProjectB/src/agent_multi_threading.hpp:30,
          from /<dir_path>/ProjectB/src/agent.h:10,
          from /<dir_path>/ProjectA/src/processing_core.cpp:5:
/<compiler_path>/gnu/gcc/8.2.0/rhesys/include/c++/8.2.0/atomic:62:3: error: template specialization with C linkage
    template<>
    ^~~~~~~~

Q1。“带有 C 链接的模板”和“带有 C 链接的模板特化”是什么意思?

Q2。为什么在我的代码中会发生这种情况?

标签: c++templatesincludelinkage

解决方案


推荐阅读