首页 > 解决方案 > 预编译头文件clang重新定义错误

问题描述

尝试在这里创建一个最小可重现的示例:

#include "test.hpp"

int main() {
    printMe();
    return 0;
}

假设 test.hpp 如下:

#include <iostream>
#include <vector> 
#include <unordered_map>
#include <unordered_set>
#include <string>

void printMe(){
    std::cout<<"hey me you"<<std::endl;
}

现在在我运行的终端中

clang++ -std=c++2a test.hpp

哪个成功并创建了 test.hpp.gch,但是当我运行时:

clang++ main.cpp -o main -std=c++2a -stdlib=libc++ -include-pch test.hpp.gch

我得到:

In file included from main.cpp:1:
./test.hpp:7:6: error: redefinition of 'printMe'
void printMe(){
     ^
./test.hpp:7:6: note: previous definition is here
void printMe(){
     ^

为什么会这样?

标签: c++clang

解决方案


推荐阅读